MySQL建表语句,如果不指定DEFAULT 关键字,默认会变成 DEFAULT NULL

MySQL建表语句,如果不指定DEFAULT 关键字,默认会变成 DEFAULT NULL
下述两个建表语句中t_sign字段,一个没指定DEFAULT NULL,一个指定了DEFAULT NULL。

CREATE TABLE `t1` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '唯一ID',
  `t_sign` varchar(16)  COMMENT '签名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB ;

CREATE TABLE `t2` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '唯一ID',
  `t_sign` varchar(16) DEFAULT NULL COMMENT '签名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB ;

最后查询

select column_name, column_default,data_type,is_nullable,column_type,extra from information_schema.columns 

视图和执行 show create table xxx

语句看到的结果都一样的。
t_sign 列定义都为 `t_sign` varchar(16) DEFAULT NULL COMMENT '签名'。


官方文档说明: 
If a data type specification includes no explicit DEFAULT value, MySQL determines the default value as follows:
If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.
If the column cannot take NULL as a value, MySQL defines the column with no explicit DEFAULT clause.

https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html

posted @ 2023-03-09 10:22  屠魔的少年  阅读(4)  评论(0)    收藏  举报