MySQL 非空約束位置不一樣對自增列形成的影響

原文: MySQL 非空約束位置不一樣對自增列形成的影響

MySQL版本
select version();
+------------+
| version() |
+------------+
| 5.7.21-log |
+------------+
1 row in set (0.00 sec)html


非空約束爲null 並在自增列屬性前code

  • 即便自增列的非空約束定義能夠爲 null,但實際自增列爲not null

create table test_auto_incre(id int null auto_increment,id2 int default null ,key idx_id(id));htm

show create table test_auto_incre;blog

CREATE TABLE test_auto_incre (
id int(11) NOT NULL AUTO_INCREMENT,
id2 int(11) DEFAULT NULL,
KEY idx_id (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8rem

insert into test_auto_incre(id2) values(12),(2312);
select * from test_auto_incre;
+----+------+
| id | id2 |
+----+------+
| 1 | 12 |
| 2 | 2312 |
+----+------+
2 rows in set (0.00 sec)get


非空約束爲null 並在自增列屬性後io

  • 自增列定義能夠爲null,實際自增列也能夠爲null;自增列失去做用!

create table test_auto_incre2(id int auto_increment null,id2 int null,key idx_id(id));
Query OK, 0 rows affected (0.02 sec)table

非空約束在自增列屬性後,不是MySQL的標準建表語句,但建該表沒有報錯和警告
show create table test_auto_incre2;class

CREATE TABLE test_auto_incre2 (
id int(11) AUTO_INCREMENT,
id2 int(11) DEFAULT NULL,
KEY idx_id (id)
) ENGINE=InnoDB;test

插入數據

insert into test_auto_incre2(id2) values(12),(2312);
select * from test_auto_incre2;
+------+------+
| id | id2 |
+------+------+
| NULL | 12 |
| NULL | 2312 |
+------+------+
2 rows in set (0.00 sec)


非空約束爲not null 並在自增列屬性後

create table test_auto_incre2(id int auto_increment not null,id2 int null,key idx_id(id));

show create table test_auto_incre2;

CREATE TABLE test_auto_incre2 (
id int(11) NOT NULL AUTO_INCREMENT,
id2 int(11) DEFAULT NULL,
KEY idx_id (id)
) ENGINE=InnoDB A

插入數據

insert into test_auto_incre2(id2) values(12),(2312);
select * from test_auto_incre2;
+----+------+
| id | id2 |
+----+------+
| 1 | 12 |
| 2 | 2312 |
+----+------+

MySQL標準建表語法
297e380e8d580397b0fc6cbf3b967fe7.png

相關文章
相關標籤/搜索