patternsqlMinor
Field both NOT NULL and DEFAULT NULL
Viewed 0 times
fieldnullbothdefaultandnot
Problem
This MySQL table had me perplexed for a moment:
Since the
Then I realized that this schema aims to force to insert a value (and a non-NULL one) in
Is this an acceptable way to do so, or there are better ways?
mysql> desc quux;
+---------------------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+-----------------------+------+-----+---------+----------------+
| foobar | int(11) | NO | | NULL | |
(...)
mysql> show create table quux;
(...)
`foobar` int(11) NOT NULL,
(...)Since the
foobar field was never created with a DEFAULT clause, it gets automatically assigned DEFAULT NULL. However, at a first sight this looks like contradicting the fact that it was also defined as NOT NULL.Then I realized that this schema aims to force to insert a value (and a non-NULL one) in
foobar when adding a new record. Is this an acceptable way to do so, or there are better ways?
Solution
That's perfectly acceptable, it basically states:
You have to provide a value for this, but I have no clue what that would be.
If you define a default value, you claim that this is a sensible value for any row that doesn't supply a value - which is not really feasible for most attributes.
Think about a column named
You have to provide a value for this, but I have no clue what that would be.
If you define a default value, you claim that this is a sensible value for any row that doesn't supply a value - which is not really feasible for most attributes.
Think about a column named
first_name - you want to make sure a value is supplied but there is no way you can come up with a sensible default value for that. Or a column named salary in an employee table.Context
StackExchange Database Administrators Q#156159, answer score: 8
Revisions (0)
No revisions yet.