Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

mysql

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.

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,

Normally I'm using:

price DOUBLE NULL;

if I want to enable that column to accept NULL values.

So what are the differences between these 3 statements?

  1. price DOUBLE NULL;

  2. price DOUBLE DEFAULT NULL;

  3. price DOUBLE NULL DEFAULT NULL;

like image 429
Prageeth Liyanage Avatar asked Sep 22 '14 06:09

Prageeth Liyanage


People also ask

What is default NULL in MySQL?

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.

Are columns NULL by default?

By default, a column holds NULL values. In the example, we use the NOT NULL constraint that enforces a column not to accept NULL values.

What is the difference between NULL and default?

The first NULL says that the column is nullable, i.e. accepts NULL . The second NULL (after DEFAULT ) is the default value. If you only have the default, but make the column reject nulls, then that default cannot be used.

What is NULL default NULL in SQL?

"Standard" SQL specifies the following rules: For each row in table, a column can contain a value or NULL which indicates "no value." If a column is declared NOT NULL, it must always contain a non-NULL value; NULL is not allowed.


2 Answers

There is no difference. NULL DEFAULT NULL is the implicit default.

From the CREATE TABLE documentation:

  • If neither NULL nor NOT NULL is specified, the column is treated as though NULL had been specified

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.
like image 52
a_horse_with_no_name Avatar answered Oct 14 '22 04:10

a_horse_with_no_name


In all three of the following cases:

price DOUBLE NULL;

price DOUBLE DEFAULT NULL;

price DOUBLE NULL DEFAULT NULL;

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

like image 20
user207421 Avatar answered Oct 14 '22 04:10

user207421