I’ve seen many times the following syntax which defines a column in a create/alter DDL statement:
ALTER TABLE tbl ADD COLUMN col VARCHAR(20) NOT NULL DEFAULT "MyDefault"
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?
DEFAULTis 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 theNOT NULLconstraint:Then you could issue these statements
Alternatively, you can also use
DEFAULTinUPDATEstatements, according to the SQL-1992 standard:Note, not all databases support all of these SQL standard syntaxes. Adding the
NOT NULLconstraint will cause an error with statements4, 6, while1-3, 5are still valid statements. So to answer your question: No, they’re not redundant.