Id like to create a column on my table that allows null but is set by default to empty (not null
)
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} DEFAULT '';
This doesn't seem to work. Any ideas?
Thanks!
By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
The Simple AnswerIf the column is nullable then it will create the column with a NULL value instead of the default value, however, if column is not nullable and there is a default value, SQL Server has to apply that value to column to avoid violating not null constraint.
Code Inspection: Insert NULL into NOT NULL column You cannot insert NULL values in col1 and col2 because they are defined as NOT NULL. If you run the script as is, you will receive an error. To fix this code, replace NULL in the VALUES part with some values (for example, 42 and 'bird' ).
Did you try
ALTER TABLE table_name ADD column_name VARCHAR(20) NULL DEFAULT '';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With