I have a table that when I describe
it is:
mysql> DESC my_table;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| contact_id | int(11) | NO | PRI | NULL | auto_increment |
| location | varchar(20) | YES | | NULL | |
| city | varchar(20) | YES | | NULL | |
| state | varchar(2) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
My question is: why for the primary key contact_id
the Default
is displayed as NULL
?
I created the table with NOT NULL
for the column and the Primary Key
can not be NULL
anyway.
How can Null
be NO
and Default
be NULL
?
Primary keys The primary key contains unique values and identifies each row in a table. The primary key cannot contain NULL values. A table can have only one primary key and this primary key can consist of single or multiple columns.
Primary key constraintsNULL values are not allowed. If the column(s) contain NULL values, the system will not add the primary key constraint.
Entity integrity guarantees that every primary key attribute is non-null. The primary key performs the unique identification function in a relational model. An entity that cannot be identified is a contradiction in terms, hence the name entity integrity.
Consequently, a Primary Key cannot have a static Default Value and also be Unique. Similarly, a Foreign Key cannot be both NOT-NULL and have a Default Value of (0).
The fact that it can't be null makes the content of the 'default' column irrelevant. They are using 'null' in the 'default' column because otherwise they would need another magic value to indicate 'irrelevant', 'unused', end.
Don't worry about it.
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