J. Sethi
J. Sethi

Reputation: 33

desc table in mysql say Null is No but default is NULL?

I am using mysql for a project. When I did 'desc table_name', I see that the tabular output shows the Null of the field is set to No, but then 'Default' is set to NULL. How can a field not have a Null but then default is Null itself?

mysql> desc users;                                                                                 
+-----------------+------------------+------+-----+----------------------------+----------------+  
| Field           | Type             | Null | Key | Default                    | Extra          |  
+-----------------+------------------+------+-----+----------------------------+----------------+  
| id              | int(10) unsigned | NO   | PRI | NULL                       | auto_increment |  
| created_at      | timestamp        | YES  |     | NULL                       |                |  
| updated_at      | timestamp        | YES  |     | NULL                       |                |  
| email           | varchar(255)     | NO   |     | NULL                       |                |  
| password        | varchar(255)     | NO   |     | NULL                       |                |  
| remember_token  | varchar(100)     | YES  |     | NULL                       |                |  
| profile_pic     | varchar(255)     | YES  |     | img/profile/profilepic.jpg |                |  
| activation_code | varchar(255)     | YES  | UNI | NULL                       |                |  
| status_id       | int(10) unsigned | NO   | MUL | 1                          |                |  
| profile_id      | int(10) unsigned | YES  | MUL | NULL                       |                |  
+-----------------+------------------+------+-----+----------------------------+----------------+  
10 rows in set (0.00 sec)                                                                          

Upvotes: 2

Views: 2066

Answers (2)

Wesley Lau
Wesley Lau

Reputation: 11

  1. we should know this:
  • Null(NO): means this column couldn't accept a "NULL" value
  • Null(YES): means this column could accept a "NULL" value
  • Default: means this column's default value :)
  1. we should know: in MySQL, blank is not equal to NULL, if we insert nothing into a column, it will stay nothing(it will not insert a "NULL" into this column)

  2. so, when Null(NO) comes with Default(NULL), if we insert nothing to this value, it'll have a default value "NULL" and it 'll conflict with the constraint, then it'll throw a exception

Upvotes: 1

Mureinik
Mureinik

Reputation: 312289

This is a perfectly legal combination. It means that when you try to insert a new row and do not explicitly specify this column's value, it would default to null and thus error out. This combination is often used as a way to force you to explicitly assign a value to the column instead of just relying on defaults.

Upvotes: 4

Related Questions