内容提要:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
语法:
create table 表名(
字段名1 类型 (宽度) 约束条件,
字段名2 类型 (宽度) 约束条件,
字段名3 类型 (宽度) 约束条件);
特别注意:
补充:1、这里的宽度是对存储数据的一种限制,在不同MySQL版本里面可能会出现不同的结果,这里只是不同版本的默认约束条件不一样导致,在最新版本5,7中如果输入数据
宽度超出限制,则会报错。
2、类型与约束条件的区别:类型是限制字段必须以什么样的数据类型存储,二约束条件是在这一类型之上添加一种额外的限制。
验证整型字段有无符号及范围int类型默认是有符号的,也就是说默认支持输入负数到正数mysql> reate table t1(id tinyint);mysql> insert into t1 values(128),(-129);#结果:ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1# 因为tinyint类型最大正整数限制为127,输入128超出范围。报错create table t2(id tinyint unsigned); # unsigned表示这里类型设置为无符号(无正负号)mysql> insert into t2 values (0),(255);#这里类型约束为无符号,所以输入负数就会报错,输入超过255的数也会报错。tinyint无符号范围是0-255。以下同理:create table t3(id int unsigned);mysql> insert into t3 values (4294967296);ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1mysql> insert into t3 values (4294967295);Query OK, 1 row affected (0.01 sec)强调:对于整型来说,数据类型之后的宽度并不是存储限制,而是现实宽度限制,所以在创建表的时候,如果字段采用的是整型类型,完全无需指定显示宽度,因为默认的显示宽度(11),一般情况下,就足够显示完整的数据。
# 存储限制float(255,30)double(255,30)decimal(65,30)# 精确度验证mysql> create table t9(x float(255,30));mysql> create table t10(x double(255,30));mysql> create table t11(x decimal(65,30));mysql> insert into t9 values(1.111111111111111111111111111111);mysql> insert into t10 values(1.111111111111111111111111111111);mysql> insert into t11 values(1.111111111111111111111111111111);mysql> select * from t9;+----------------------------------+| x |+----------------------------------+| 1.111111164093017600000000000000 |+----------------------------------+1 row in set (0.00 sec)mysql> select * from t10;+----------------------------------+| x |+----------------------------------+| 1.111111111111111200000000000000 |+----------------------------------+1 row in set (0.00 sec)mysql> select * from t11;+----------------------------------+| x |+----------------------------------+| 1.111111111111111111111111111111 |+----------------------------------+1 row in set (0.00 sec)从上面的查询创建的表中数据可以看出:精度对比:decimal的精度最高,其次是double,最后是float,但是就算是精度最低的float它的精度也有16位,所以一般使用完全能够应付99%的数据场景。
char:mysql> create table t12(name char(4));mysql> insert into t12 values (‘hello‘);ERROR 1406 (22001): Data too long for column ‘name‘ at row 1 # 超出限制范围会报错。mysql> insert into t12 values (‘hell‘);Query OK, 1 row affected (0.01 sec)mysql> insert into t12 values (‘he‘);Query OK, 1 row affected (0.00 sec)mysql> select * from t12;+------+| name |+------+| hell | # 4个字符正好填满| he | #不够4个字符空格补全+------+--------------------------------------------------------------varchar:mysql> create table t13(name varchar(4));mysql> insert into t13 values (‘hello‘);ERROR 1406 (22001): Data too long for column ‘name‘ at row 1 # 超出范围,报错mysql> insert into t13 values (‘hell‘);Query OK, 1 row affected (0.00 sec)mysql> insert into t13 values (‘he‘);Query OK, 1 row affected (0.01 sec)mysql> select * from t13;+------+| name |+------+| hell | # 正好4个,填满| he | # 不够4个,按实际宽度占位。(这里看不出来,但是一定要记住,varchar是变长,所以不够4个肯定按实际宽度存储)+------+#####TIP:select char_length(name) from t12select char_length(name) from t13 # 仍然无法查看到真正的结果"""首先应该肯定的是在硬盘上存的绝对是真正的数据,但显示的时候mysql会自动将末尾的空格取掉"""# 如果不想让mysql帮你做自动去除末尾空格的操作,需要再添加一个模式set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";# 退出客户端重新登陆select char_length(x) from t12; #4select char_length(y) from t13; #1# 针对char类型,mysql在存储时会将数据用空格补全存放到硬盘中。但是会在读出结果的时候自动取掉末尾的空格####
mysql> create table stu(id int,name char(16),birth_d date,study_time time,reg_time datetime); # 创建表mysql> insert into stu values(1,‘jason‘,‘2019-05-09‘,‘11:11:11‘,‘2019-11-11 11:11"11‘); #插入表数据Query OK, 1 row affected (0.00 sec)mysql> select * from stu; # 显示表内容+------+-------+------------+------------+---------------------+| id | name | birth_d | study_time | reg_time |+------+-------+------------+------------+---------------------+| 1 | jason | 2019-05-09 | 11:11:11 | 2019-11-11 11:11:11 |+------+-------+------------+------------+---------------------+1 row in set (0.00 sec)
enum枚举:mysql> create table usr(id int,name char(16),gender enum(‘male‘,‘female‘,‘others‘));Query OK, 0 rows affected (0.05 sec)mysql> insert into usr values(1001,‘jsson‘,‘xxx‘);ERROR 1265 (01000): Data truncated for column ‘gender‘ at row 1mysql> insert into usr values(1001,‘jsson‘,‘male‘);Query OK, 1 row affected (0.01 sec)mysql> insert into usr values(1002,‘egon‘,‘female‘);Query OK, 1 row affected (0.01 sec)mysql> select * from usr;+------+-------+--------+| id | name | gender |+------+-------+--------+| 1001 | jsson | male || 1002 | egon | female |+------+-------+--------+2 rows in set (0.00 sec)-----------------------------------------------------------------set集合:mysql> create table tea(id int,name char(16),gender enum(‘male‘,‘female‘,‘other‘),hobby set(‘read‘,‘sleep‘,‘dbj‘));Query OK, 0 rows affected (0.04 sec)mysql> insert into tea values(10011,‘egon‘,‘female‘,‘read,dbj‘);Query OK, 1 row affected (0.01 sec)mysql> insert into tea values(10012,‘alex‘,‘male‘,‘dbj‘);Query OK, 1 row affected (0.05 sec)mysql> select * from tea;+-------+------+--------+----------+| id | name | gender | hobby |+-------+------+--------+----------+| 10011 | egon | female | read,dbj || 10012 | alex | male | dbj |+-------+------+--------+----------+2 rows in set (0.00 sec)
PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录FOREIGN KEY (FK) 标识该字段为该表的外键NOT NULL 标识该字段不能为空UNIQUE KEY (UK) 标识该字段的值是唯一的AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键)DEFAULT 为该字段设置默认值UNSIGNED 无符号ZEROFILL 使用0填充not null + defaultnot null:不为空
mysql> create table us(id int,name char(16));Query OK, 0 rows affected (0.05 sec)mysql> insert into us values (1,null);Query OK, 1 row affected (0.01 sec)mysql> desc us;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | char(16) | YES | | NULL | |+-------+----------+------+-----+---------+-------+mysql> delete from us where id=1; # 先清空存入的null数据Query OK, 1 row affected (0.01 sec)mysql> alter table us modify name char(16) not null; # 将name输入的数据限制为非空Query OK, 0 rows affected (0.05 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc us; # 查看表格式:+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | char(16) | NO | | NULL | |+-------+----------+------+-----+---------+-------+mysql> insert into us values(002,null); # 此时再输入null就会报错了,因为我们限制了name必须输入未非空的字符。ERROR 1048 (23000): Column ‘name‘ cannot be nulldefault 默认值mysql> create table st(id int,name char(16) not null,gender enum(‘male‘,‘female‘) default ‘male‘); # 为gender字段设置默认值‘male,前面指定输入字段不包含gender时候,这时候的默认字段名就会生效。Query OK, 0 rows affected (0.03 sec)mysql> insert into st(id,name) values (1001,‘lxx‘);Query OK, 1 row affected (0.01 sec)mysql> select * from st;+------+------+--------+| id | name | gender |+------+------+--------+| 1001 | lxx | male |+------+------+--------+1 row in set (0.00 sec)
###单列唯一:mysql> create table user1(id int unique,name char(16));Query OK, 0 rows affected (0.04 sec)mysql> insert into user1 values (1,‘js‘),(1,‘eg‘); #报错ERROR 1062 (23000): Duplicate entry ‘1‘ for key ‘id‘mysql> insert into user1 values (1,‘js‘),(2,‘eg‘); #不报错Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0###联合唯一:多个字段如果同时有相同的输入时,会报错mysql> create table server(id int,ip char(16),port int,unique(ip,port));Query OK, 0 rows affected (0.04 sec)mysql> desc server;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || ip | char(16) | YES | MUL | NULL | || port | int(11) | YES | | NULL | |+-------+----------+------+-----+---------+-------+3 rows in set (0.00 sec)mysql> insert into server values (1,‘127.0.0.1‘,8080);Query OK, 1 row affected (0.01 sec)mysql> insert into server values (2,‘127.0.0.1‘,8080); #ip和port都相同,报错ERROR 1062 (23000): Duplicate entry ‘127.0.0.1-8080‘ for key ‘ip‘ mysql> insert into server values (2,‘127.0.0.2‘,8080); # 仅仅是port相同不会报错Query OK, 1 row affected (0.00 sec)mysql> insert into server values (3,‘127.0.0.1‘,8081); #仅仅是ip相同也不会报错Query OK, 1 row affected (0.00 sec)mysql> select * from server; 查看结果:+------+-----------+------+| id | ip | port |+------+-----------+------+| 1 | 127.0.0.1 | 8080 || 2 | 127.0.0.2 | 8080 || 3 | 127.0.0.1 | 8081 |+------+-----------+------+3 rows in set (0.00 sec)
# 单从约束角度来说 primary key 等价于not null + uniquemysql> create table tt1(id int primary key); # 设置id主键唯一Query OK, 0 rows affected (0.04 sec)mysql> desc tt1;+-------+---------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| id | int(11) | NO | PRI | NULL | |+-------+---------+------+-----+---------+-------+1 row in set (0.00 sec)mysql> insert into tt1 values (1),(1); # 主键不唯一,报错ERROR 1062 (23000): Duplicate entry ‘1‘ for key ‘PRIMARY‘mysql> insert into tt1 values (1),(2); #唯一,不报错Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0-----------------------------------------------------------------
重点内容:一、一张表中必须有且只有一个主键,如果你没有设置主键,那么会从上到下搜索直到遇到一个非null且unique的字段自动将其设为主键。mysql> create table ts1(id int,name char(16),age int not null unique,addr char(16) not null unique)engine=innodb;Query OK, 0 rows affected (0.04 sec)mysql> desc ts1;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | char(16) | YES | | NULL | || age | int(11) | NO | PRI | NULL | || addr | char(16) | NO | UNI | NULL | |+-------+----------+------+-----+---------+-------+------------------------------------------------------------------二、如果表里面没有指定的任何可以设置的主键字段,那么innodb存储引擎会采用自己的默认的一个隐藏的字段作为主键,隐藏意味着你在查询的时候无法根据主键字段加速查询了------------------------------------------------------------------三、一张表中通常都应该有一个id字段,并且通常将id字段设为主键------------------------------------------------------------------额外知识点:#联合主键:与联合唯一字段一个道理,即多个字段联合起来作为表的主键,本质上将多个字段总体看为一个主键mysql> create table ts2(ip char(16),port int,primary key(ip,port));Query OK, 0 rows affected (0.04 sec)mysql> desc ts2; # 2个字段合为一个主键。+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| ip | char(16) | NO | PRI | NULL | || port | int(11) | NO | PRI | NULL | |+-------+----------+------+-----+---------+-------+
auto_increment 自增约束mysql> create table ts3(id int primary key auto_increment,name char(16));Query OK, 0 rows affected (0.05 sec)mysql> insert into ts3(name) values (‘jason‘),(‘alex‘),(‘lxx‘); #只输入name字段信息Query OK, 3 rows affected (0.01 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> select * from ts3; # 查看自增结果+----+-------+| id | name |+----+-------+| 1 | jason || 2 | alex || 3 | lxx |+----+-------+------------------------------------------------------------------补充:接着上面的例子,如果我将其中一个字段比如lxx,删除后再添加字段,会出现:mysql> delete from ts3 where id=3;Query OK, 1 row affected (0.01 sec)mysql> insert into ts3(name) values (‘xxxx‘);Query OK, 1 row affected (0.01 sec)mysql> select * from ts3;+----+-------+| id | name |+----+-------+| 1 | jason || 2 | alex || 4 | xxxx | # 此时序号居然还是接着自增。+----+-------+3 rows in set (0.00 sec)tip:如果要清楚继续自增,从头开始的话,就必须清空表,才行。mysql> truncate ts3; # 将整张表重置,id重新从0开始记录。Query OK, 0 rows affected (0.03 sec)mysql> insert into ts3(name) values (‘eeeee‘);Query OK, 1 row affected (0.01 sec)mysql> select * from ts3;+----+-------+| id | name |+----+-------+| 1 | eeeee |+----+-------+
-------------------------------------------------------------------------------------------------------------------------------------------
我们刚刚在上面设置了char,tinyint,存储数据时超过它们的最大存储长度,发现数据也能正常存储进去,只是mysql帮我们自动截取了最大长度。但在实际情况下,我们应该尽量减少数据库的操作,缓解数据库的压力,让它仅仅只管理数据即可,这样的情况下就需要设置安全模式
show variables like "%mode%"; # 查看数据库配置中变量名包含mode的配置参数# 修改安全模式set session # 只在当前操作界面有效set global # 全局有效set global sql_mode =‘STRICT_TRANS_TABLES‘# 修改完之后退出当前客户端重新登陆即可