48

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;

1
  • You forgot one possibility... what about price DOUBLE DEFAULT NULL NOT NULL... i shit you not, i actually have a database defined like that :-) Commented Apr 4, 2019 at 19:06

3 Answers 3

44

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.
Sign up to request clarification or add additional context in comments.

Comments

21
price DOUBLE NULL;

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

price DOUBLE DEFAULT NULL;

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

price DOUBLE NULL DEFAULT NULL;

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

5 Comments

"price is a double and cannot be null" for the second expression is wrong. If NULL is omitted, NULL is assumed (see my quote from the manual)
@EJP to me seems like every statements are equal. I still cant extract this. :)
@Sylar They are equal.
@JanacMeena 'Looking for differences when there are none' is the whole point of the answer. That's why I wrote it that way. Edit reverted.
I edited since other users also were confused by this. Up to you if you want to keep this less readable
0
price DOUBLE NULL;

Allows the price column to accept NULL.

price DOUBLE DEFAULT NULL;

Allows the price column to accept NULL and sets the default value to NULL.

price DOUBLE NULL DEFAULT NULL;

Act the same as the second statement, as it accepts NULL values and sets the default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.