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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:04:09  来源:igfitidea点击:

NULL vs DEFAULT NULL vs NULL DEFAULT NULL in MYSQL column creation?

mysqlsql

提问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 priceDOUBLE NULL DEFAULT NULL,

在那里看语句priceDOUBLE NULL DEFAULT NULL,

Normally I am using

通常我使用

priceDOUBLE 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) priceDOUBLE NULL;

1)price双空;

2) priceDOUBLE DEFAULT NULL;

2)price双默认空值;

3) priceDOUBLE NULL DEFAULT NULL;

3)price双空默认空;

Thank you very much.

非常感谢。

回答by a_horse_with_no_name

There is no difference. NULL DEFAULT NULLis 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;

priceis a double and can be null and its default value is null.

price是一个双精度值,可以为空,其默认值为空。

price DOUBLE DEFAULT NULL;

priceis a double and can be null and its default value is null.

price是一个双精度值,可以为空,其默认值为空。

price DOUBLE NULL DEFAULT NULL;

priceis a double and can be null and its default value is null.

price是一个双精度值,可以为空,其默认值为空。