MYSQL 列创建中的 NULL 与 DEFAULT NULL 与 NULL DEFAULT NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25968054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NULL vs DEFAULT NULL vs NULL DEFAULT NULL in MYSQL column creation?
提问by Prageeth Liyanage
Following sql table definition is illustrated one of create table statement from my MYSQL database which is developed by a former developer of my company.
下面的 sql 表定义说明了我的 MYSQL 数据库中的一条 create table 语句,它是由我公司的前开发人员开发的。
DROP TABLE IF EXISTS `classifieds`.`category_vehicles`;
CREATE TABLE `classifieds`.`category_vehicles`(
`adv_id_ref` BIGINT UNSIGNED NOT NULL,
`category_id_ref` TINYINT UNSIGNED NOT NULL,
`forsale_status` TINYINT (1) NOT NULL,
`vehicle_type_id_ref` TINYINT UNSIGNED NOT NULL,
`price` DOUBLE NULL DEFAULT NULL,
PRIMARY KEY (`adv_id_ref`)
) ENGINE = INNODB CHARSET = latin1 COLLATE = latin1_swedish_ci ;
In there look at the statement price
DOUBLE NULL DEFAULT NULL,
在那里看语句price
DOUBLE NULL DEFAULT NULL,
Normally I am using
通常我使用
price
DOUBLE NULL;
price
双空;
if I want to enable that column to accept NULL values.
如果我想让该列接受 NULL 值。
So what are the differences between these 3 statements?
那么这3种说法有什么区别呢?
1) price
DOUBLE NULL;
1)price
双空;
2) price
DOUBLE DEFAULT NULL;
2)price
双默认空值;
3) price
DOUBLE NULL DEFAULT NULL;
3)price
双空默认空;
Thank you very much.
非常感谢。
回答by a_horse_with_no_name
There is no difference. NULL DEFAULT NULL
is the implicit default.
没有区别。NULL DEFAULT NULL
是隐式默认值。
From the CREATE TABLE documentation:
- If neither NULL nor NOT NULL is specified, the column is treated as though NULL had been specified
- 如果既未指定 NULL 也未指定 NOT NULL,则将该列视为已指定 NULL
From the "Data Type Default Values" chapter:
- If a column definition 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.
- 如果列定义不包含显式 DEFAULT 值,MySQL 确定默认值如下: 如果列可以将 NULL 作为值,则使用显式 DEFAULT NULL 子句定义该列。
回答by user207421
price DOUBLE NULL;
price
is a double and can be null and its default value is null.
price
是一个双精度值,可以为空,其默认值为空。
price DOUBLE DEFAULT NULL;
price
is a double and can be null and its default value is null.
price
是一个双精度值,可以为空,其默认值为空。
price DOUBLE NULL DEFAULT NULL;
price
is a double and can be null and its default value is null.
price
是一个双精度值,可以为空,其默认值为空。