前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL 非空约束位置不同对自增列造成的影响

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

作者头像
星哥玩云
发布2022-08-17 21:24:07
1.6K0
发布2022-08-17 21:24:07
举报
文章被收录于专栏:开源部署开源部署

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 非空约束位置不同对自增列造成的影响
MySQL 非空约束位置不同对自增列造成的影响
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档