Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Column Allow NULL but Default Value Set to NOT NULL

Tags:

sql

mysql

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!

like image 827
rpophessagr Avatar asked Dec 08 '11 20:12

rpophessagr


People also ask

Can a column defined as a NOT NULL can have a default value of NULL?

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.

Can a nullable column have a default value?

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.

How do I insert a NULL value into a NOT NULL column?

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' ).


1 Answers

Did you try

ALTER TABLE table_name ADD column_name VARCHAR(20) NULL DEFAULT '';
like image 111
Ted Hopp Avatar answered Oct 25 '22 07:10

Ted Hopp