Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a primary key default be NULL? Why is it described as such?

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?

like image 621
Cratylus Avatar asked Apr 06 '13 11:04

Cratylus


People also ask

Can a primary key be NULL and why?

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.

Can a primary key field be NULL?

Primary key constraintsNULL values are not allowed. If the column(s) contain NULL values, the system will not add the primary key constraint.

Why can't a primary key be NULL?

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.

Can primary key have default value?

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).


1 Answers

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.

like image 137
user207421 Avatar answered Sep 30 '22 14:09

user207421