Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default value for an existing column

This isn't working in SQL Server 2008:

ALTER TABLE Employee ALTER COLUMN CityBorn SET DEFAULT 'SANDNES' 

The error is:

Incorrect syntax near the keyword 'SET'.

What am I doing wrong?

like image 635
Nakul Chaudhary Avatar asked Jul 22 '11 14:07

Nakul Chaudhary


People also ask

How do I set a DEFAULT to an existing column?

In Object Explorer, right-click the table with columns for which you want to change the scale and select Design. Select the column for which you want to specify a default value.

How do I change the default value of a column in mysql?

To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values must be constants. For example, you cannot set the default for a date-valued column to NOW( ) , although that would be very useful.

Which command used to change the default value of any column?

Changing a Column's Default Value. To set a new default for a column, use a command like: ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77; Note that this doesn't affect any existing rows in the table, it just changes the default for future INSERT commands.

What is the default value of column in SQL?

Default values can be NULL, or they can be a value that matches the data type of the column (number, text, date, for example).


1 Answers

This will work in SQL Server:

ALTER TABLE Employee ADD CONSTRAINT DF_SomeName DEFAULT N'SANDNES' FOR CityBorn; 
like image 131
Yuck Avatar answered Oct 10 '22 10:10

Yuck