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

栏目: 数据库 · 发布时间: 5年前

内容简介:select version();+------------+

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

传统企业,互联网在踢门

传统企业,互联网在踢门

刘润 / 中国华侨出版社 / 2014-7 / 42

1、第一本传统企业互联网化的战略指导书,首次提出“互联网加减法”,迄今最清晰的转型公式 鉴于目前很多传统企业“老办法不管用,新办法不会用”的现状,本书将用“互联网的加减法” 这个简单模型清晰地说明商业新时代的游戏规则和全新玩法,帮助传统企业化解“本领恐慌” 。 2、小米董事长&CEO 金山软件董事长雷军,新东方教育科技集团董事长兼CEO俞敏洪,复旦大学管理学院院长陆雄文,复旦大学博士、......一起来看看 《传统企业,互联网在踢门》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具