手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL 非空约束位置不同对自增列造成的影响

[日期:2019-04-27] 来源:Linux社区  作者:YangJiaXin [字体: ]

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


非空约束为null 并在自增列属性前

  • 即使自增列的非空约束定义可以为 null,但实际自增列为not null

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

show create table test_auto_incre;

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=utf8

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)


非空约束为null 并在自增列属性后

  • 自增列定义可以为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)

非空约束在自增列属性后,不是MySQL的标准建表语句,但建该表没有报错和警告
show create table test_auto_incre2;

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

插入数据

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标准建表语法

MySQL 非空约束位置不同对自增列造成的影响

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2019-04/158338.htm

linux
相关资讯       MySQL自增列  MySQL非空约束 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款