Skip to content

Commit c9f89ef

Browse files
committed
MySQL id, datetime, charset
1 parent 788009b commit c9f89ef

8 files changed

+62
-43
lines changed

Diff for: db/DB_table.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ import javax.validation.constraints.NotNull;
2727

2828
### 表前加库名可以方便后人接手
2929

30-
虽然可以在`information_schema.TABLES`
30+
虽然可以在`information_schema.TABLES`
31+
32+
33+
### datetime(6) 用最 6 位小数或和 java 一样 3 位小数,否则默认没有小数
34+
35+
### 创建时间和更新时间使用数据库自动生生成避免程序与数据库时钟差,创建与更新时不要写这两个字段
36+
37+
### 除非数据量较小时不用 id,否则 InnoDB 用 id 当主键以提升 B+ 树存储效率

Diff for: db/MySQL_charset.md

+38-30
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ show variables like 'collation%';
1515
vim /etc/mysql/my.cnf
1616

1717
[client]
18-
default-character-set=utf8mb4_bin
18+
default-character-set=utf8mb4
1919

2020
[mysqld]
2121
default-storage-engine=INNODB
22-
character-set-server=utf8mb4_bin
22+
character-set-server=utf8mb4
2323
collation-server=utf8mb4_bin
2424

2525
/etc/init.d/mysql stop
@@ -28,42 +28,50 @@ collation-server=utf8mb4_bin
2828

2929
```mysql
3030
-- 服务端
31-
set global character_set_server=utf8mb4_bin;
32-
set global character_set_database=utf8mb4_bin;
31+
set global character_set_server = utf8mb4;
32+
set global character_set_database = utf8mb4;
3333

3434
-- 客户端
35-
set global character_set_client=utf8mb4_bin;
36-
set global character_set_connection=utf8mb4_bin;
37-
set global character_set_results=utf8mb4_bin;
35+
set global character_set_client = utf8mb4;
36+
set global character_set_connection = utf8mb4;
37+
set global character_set_results = utf8mb4;
3838

39-
create database if not exists `db1` /*!40100 default character set utf8mb4_bin */;
39+
create database if not exists `db1` /*!40100 default character set utf8mb4 */;
4040

41-
-- drop table if exists `db1_table1`;
42-
create table if not exists db1_table1
41+
-- show create table db1_table0;
42+
drop table if exists `db1_table0`;
43+
create table if not exists db1_table0
4344
(
44-
id bigint unsigned auto_increment comment '自增主键',
45-
create_time datetime not null comment '创建时间',
46-
update_time datetime null comment '更新时间',
47-
constraint db1_table1_pk primary key (id),
48-
constraint db1_table1_id_unique_index unique (id)
45+
id bigint(20) unsigned not null auto_increment comment '自增主键',
46+
create_time datetime(6) not null default current_timestamp(6) comment '创建时间',
47+
update_time datetime(6) not null default current_timestamp(6) on update current_timestamp(6) comment '更新时间',
48+
constraint db1_table1_pk primary key (id),
49+
constraint db1_table1_id_unique_index unique (id)
4950
) engine = InnoDB
50-
default charset = utf8mb4_bin comment ='表名';
51+
default charset = utf8mb4
52+
collate = utf8mb4_bin comment ='表名';
5153

52-
-- 主键用id
54+
-- show create table db1_table1;
55+
drop table if exists `db1_table1`;
5356
create table if not exists db1_table1
5457
(
55-
id bigint unsigned auto_increment primary key unique comment '自增主键',
56-
create_time datetime not null comment '创建时间',
57-
update_time datetime null comment '更新时间'
58-
) ENGINE = InnoDB
59-
DEFAULT CHARSET = utf8mb4_bin COMMENT ='表名';
58+
id bigint(20) unsigned not null auto_increment primary key unique comment '自增主键',
59+
create_time datetime(6) not null default current_timestamp(6) comment '创建时间',
60+
update_time datetime(6) not null default current_timestamp(6) on update current_timestamp(6) comment '更新时间'
61+
) engine = InnoDB
62+
default charset = utf8mb4
63+
collate = utf8mb4_bin comment ='表名';
6064

61-
-- show create table db1_table1;
62-
CREATE TABLE `db1_table1` (
63-
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
64-
`create_time` datetime NOT NULL COMMENT '创建时间',
65-
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
66-
PRIMARY KEY (`id`),
67-
UNIQUE KEY `id` (`id`)
68-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4_bin COMMENT='表名'
65+
-- show create table db1_table2;
66+
drop table if exists `db1_table2`;
67+
create table if not exists `db1_table2`
68+
(
69+
`id` bigint(20) unsigned not null auto_increment comment '自增序号',
70+
`create_time` datetime(6) not null default current_timestamp(6) comment '创建时间',
71+
`update_time` datetime(6) not null default current_timestamp(6) on update current_timestamp(6) comment '更新时间',
72+
primary key (`id`),
73+
unique key `id` (`id`)
74+
) engine = InnoDB
75+
default charset = utf8mb4
76+
collate = utf8mb4_bin comment ='表名';
6977
```

Diff for: db/SQL.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
```sh
2020
# -v 显示日志 -U 禁止无 where 更新
21-
mysql -v -U -uroot -proot -h $IP -P 3306 --default-character-set=utf8mb4_bin < mysql_run.sql
22-
set character set utf8mb4_bin;
21+
mysql -v -U -uroot -proot -h $IP -P 3306 --default-character-set=utf8mb4 < mysql_run.sql
22+
set character set utf8mb4;
2323

2424
# 避免 Oracle 乱码
2525
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"

Diff for: db/comment/MySQL_comment.sql

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
-- 创建时添加注释
2-
CREATE TABLE `COMMON_SEQ`
2+
CREATE TABLE `db1_table0`
33
(
4-
`APP_CODE` char(6) NOT NULL COMMENT '应用编码',
5-
UNIQUE KEY `COMMON_SEQ_APP_CODE_uindex` (`APP_CODE`)
4+
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
5+
`create_time` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
6+
`update_time` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) COMMENT '更新时间',
7+
PRIMARY KEY (`id`),
8+
UNIQUE KEY `db1_table1_id_unique_index` (`id`)
69
) ENGINE = InnoDB
7-
DEFAULT CHARSET = utf8mb4_bin COMMENT ='流水号表';
10+
DEFAULT CHARSET = utf8mb4
11+
COLLATE = utf8mb4_bin COMMENT ='表名';
812

913
-- 添加表注释
1014
ALTER TABLE COMMON_SEQ COMMENT '流水号表';
@@ -30,7 +34,7 @@ WHERE TABLE_SCHEMA not in ('information_schema', 'performance_schema', 'mysql',
3034
# 查询列在哪个表与注释
3135
SELECT c.TABLE_SCHEMA, c.TABLE_NAME, t.TABLE_COMMENT, COLUMN_NAME, COLUMN_COMMENT, COLUMN_TYPE, IS_NULLABLE
3236
FROM information_schema.COLUMNS c JOIN information_schema.TABLES t ON t.TABLE_NAME = c.TABLE_NAME AND t.TABLE_SCHEMA = c.TABLE_SCHEMA WHERE 1=1
33-
# AND TABLE_SCHEMA = '数据库名'
34-
AND TABLE_NAME = '表名'
37+
# AND c.TABLE_SCHEMA = '数据库名'
38+
AND c.TABLE_NAME = '表名'
3539
# AND c.COLUMN_NAME = '列名'
3640
# AND c.COLUMN_COMMENT = '列注释'

Diff for: db/comment/MySQL_comment_column.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
echo '<style> table {border-collapse: collapse;} th, td {border: 1px solid lightgray;padding-left: 5px;padding-right: 5px;}</style><table>' >\
22
APP_NAME-not_comment_column.html
33

4-
mysql -h ___IP___ -P 3306 -u 用户名 -p 密码 --default-character-set=utf8mb4_bin -e \
4+
mysql -h ___IP___ -P 3306 -u 用户名 -p 密码 --default-character-set=utf8mb4 -e \
55
"SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA not in ('information_schema','performance_schema','mysql','sys') AND COLUMN_COMMENT = '';"\
66
| sed 's/\t/<\/td><td>/g; s/^/<tr><td>/; s/$/<\/td><\/tr>/;' >>\
77
APP_NAME-not_comment_column.html

Diff for: db/comment/MySQL_comment_table.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
echo '<style> table {border-collapse: collapse;} th, td {border: 1px solid lightgray;padding-left: 5px;padding-right: 5px;}</style><table>' >\
22
APP_NAME-not_comment_table.html
33

4-
mysql -h ___IP___ -P 3306 -u 用户名 -p 密码 --default-character-set=utf8mb4_bin -e \
4+
mysql -h ___IP___ -P 3306 -u 用户名 -p 密码 --default-character-set=utf8mb4 -e \
55
"SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA not in ('information_schema','performance_schema','mysql','sys') AND TABLE_COMMENT = '';"\
66
| sed 's/\t/<\/td><td>/g; s/^/<tr><td>/; s/$/<\/td><\/tr>/;' >>\
77
APP_NAME-not_comment_table.html

Diff for: db/sql_sh/loop_run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ do
4242
echo \#\#\# start ${USER_arr[$i]}/***@$IP:1521/ORCL @oracle_run.sql
4343
# sqlplus ${USER_arr[$i]}/${PASS_arr[$i]}@$IP:1521/ORCL @oracle_run.sql
4444
# -v 显示日志 -U 禁止无 where 更新
45-
# mysql -v -U -u${USER_arr[$i]} -p${PASS_arr[$i]} -h $IP -P 3306 --default-character-set=utf8mb4_bin < mysql_run.sql
45+
# mysql -v -U -u${USER_arr[$i]} -p${PASS_arr[$i]} -h $IP -P 3306 --default-character-set=utf8mb4 < mysql_run.sql
4646
echo \#\#\# end ${USER_arr[$i]}/***@$IP:1521/ORCL @oracle_run.sql
4747
cd ..
4848
done

Diff for: db/sql_sh/mysql_run.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set character set utf8mb4_bin;
1+
set character set utf8mb4;
22
source /home/admin/db/appName/create_table.sql;
33
source /home/admin/db/appName/insert_data.sql;
44
source /home/admin/db/appName/update_data.sql;

0 commit comments

Comments
 (0)