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?
price
DOUBLE NULL;
price
DOUBLE DEFAULT NULL;
price
DOUBLE NULL DEFAULT NULL;
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.
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.
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.
"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.
There is no difference. NULL DEFAULT NULL
is the implicit default.
From the CREATE TABLE documentation:
From the "Data Type Default Values" chapter:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With