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 非空约束位置不同对自增列造成的影响的更多相关文章

  1. MySQL之非空约束(NOT NULL)

    定义: MySQL非空约束(NOT NULL)可以通过 CREATE TABLE 或 ALTER TABLE 语句实现.在表中某个列的定义后加上关键字 NOT NULL 作为限定词,来约束该列的取值不 ...

  2. mySQL 约束 (Constraints):一、非空约束 NOT NULL 约束

    非空约束 NOT NULL 约束: 强制列不能为 NULL 值,约束强制字段始终包含值.这意味着,如果不向字段添加值,就无法插入新记录或者更新记录. 1.在 "Persons" 表 ...

  3. MySql -- not null 非空约束

    2.not null 非空约束 用于确保当前列的值不为空:在创建表时,如果不指定是否可以为空,字段默认可以为NULL. -- 这是上一篇默认约束创建的表 CREATE TABLE `test`.`us ...

  4. not null 非空约束

    例子:create table tb1(     id int,     name varchar(20) not null); 注意  空字符不等于null #手动,添加非空约束 (必须这个字段,没 ...

  5. Oracle数据库,非空约束、主键约束、外键约束、唯一约束

    非空约束:设置列时,可为空默认可为空,去掉对号之后设置数据不可为空: 唯一约束:在键中设置,唯一约束名称.类型Unique.列名:设置应用完成之后,此列数据具有唯一性:即数据不可重复(类型:Uniqu ...

  6. 【第三章】MySQL数据库的字段约束:数据完整性、主键、外键、非空、默认值、自增、唯一性

    一.表完整性约束 作用:用于保证数据的完整性和一致性==============================================================约束条件  说明PRIM ...

  7. oracle非空约束

    ALTER TABLE TB_ZJGL_DWSB_GRMX_LOG MODIFY HJQX   NULL;

  8. SQL - 创建一个学生表,要求有主键约束和非空约束

    CREATE TABLE [dbo].[Student] ( [ID] [int] NOT NULL, [Name] [nchar](10) NOT NULL, [Age] [int] NOT NUL ...

  9. Mysql 表约束 非空、唯一、主键、自增长、默认、外键约束(基础6)

    非空(not null).唯一(unique key).主键(primary key).自增长(auto_increment).默认约束(default) 准备基础环境: mysql> crea ...

随机推荐

  1. 不在sudoer里解决办法 和 RHEL 挂载NTFS硬盘

    输入su 切换到root用户 打开/etc/sudoers sudo vim sudoers 在root    ALL=(ALL:ALL) ALL 下边比着写一个自己的用户名就可以了 下载 可以到ht ...

  2. Spring Boot 启动(四) EnvironmentPostProcessor

    Spring Boot 启动(四) EnvironmentPostProcessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698. ...

  3. PAT A1010.Radix 二分法

    PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...

  4. Ubuntu学习之路

    一. Ubuntu简介 Ubuntu(乌班图)是一个基于Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“ubuntu”一词,意思是“人性”.“我的存在是因为大 ...

  5. Core DES加、解密

    安装 Portable.BouncyCastle Nuget包 地址:https://www.nuget.org/packages/Portable.BouncyCastle/ Install-Pac ...

  6. @ReequestParam

    @RequestParam:传递参数,即将请求参数区数据映射到功能处理方法的参数上 eg. public String queryUserName(@RequestParam String usern ...

  7. Retrieving archetypes

    报错:Retrieving archetypes:' has encountered a problemAn internal error occurred during:"Retrievi ...

  8. c#串口测试

    软件和代码网盘下载 https://pan.baidu.com/s/1dFrE1pv#list/path=%2F SerialPort 类 https://msdn.microsoft.com/zh- ...

  9. 实验十四 第九组 张燕~杨蓉庆~杨玲 Swing图形界面组件

    实验十四  Swing图形界面组件 8-11-29 理论知识 Swing和MVC设计模式 (1)设计模式(Design pattern)是设计者一种流行的 思考设计问题的方法,是一套被反复使用,多数人 ...

  10. new-xml-data

    ~<cyfxml> <hhis> <hhi name="大货系统" is-sub-item-folder="True" is-su ...