Skip to content
Advertisement

SQL Column definition : default value and not null redundant?

I’ve seen many times the following syntax which defines a column in a create/alter DDL statement:

The question is: since a default value is specified, is it necessary to also specify that the column should not accept NULLs ? In other words, doesn’t DEFAULT render NOT NULL redundant ?

Advertisement

Answer

DEFAULT is the value that will be inserted in the absence of an explicit value in an insert / update statement. Lets assume, your DDL did not have the NOT NULL constraint:

Then you could issue these statements

Alternatively, you can also use DEFAULT in UPDATE statements, according to the SQL-1992 standard:

Note, not all databases support all of these SQL standard syntaxes. Adding the NOT NULL constraint will cause an error with statements 4, 6, while 1-3, 5 are still valid statements. So to answer your question: No, they’re not redundant.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement