How can a column with a default value be added to an existing table in SQL Server 2000 / SQL Server 2005?
Join them; it only takes a minute:
|
|
|||||||||||||||||||||
|
The inclusion of the DEFAULT fills the column in existing rows with the default value, so the NOT NULL constraint is not violated. |
|||||||||||||||||||||
|
|
When adding a nullable column,
|
|||||||||||||||||
|
|
|||||
|
|
|||||||||
|
|
Beware when the column you are adding has a |
|||||
|
|
The most basic version with two lines only
|
|||
|
|
|
-- add a column with a default DateTime
|
||||
|
|
|
If you want to add multiple columns you can do it this way for example:
|
|||
|
|
|
||||
|
|
|
In SQL Server 2008-R2, I go to the design mode -in a test db- and add my two columns using the designer and made the settings with the GUI then, the infamous Right-Click gives the option "Generate Change Script"! |
||||
|
|
|
You can do the thing with T-SQL by following way.
As well as you can use SQL Server Management Studio also by right clicking table in Design menu setting default value to table. And futher more if you want to add same column(if not exists) to all tables in databse then use.
|
|||
|
|
|
Alternatively you can add a default without having to explicitly name the constraint.
If you have an issue with existing default constraints when creating this constraint then they can be removed by.
|
|||
|
|
|
To add a column to an existing database with a default value, we can use:
Here is another way to add a column to an existing database with a default value. A much more thorough SQL script to add a column with a default value is below including checking if the column exists before adding it also checkin the constraint and dropping it if there is one. This script also names the constraint so we can have a nice naming convention (I like DF_) and if not SQL will give us a constraint with a name which has a randomly generated number; so it's nice to be able to name the constraint too.
These are two ways to add a column to an existing database with a default value. |
||||
|
|
The MSDN article ALTER TABLE (Transact-SQL) has all of the alter table syntax. |
||||
|
|
|
Example:
|
||||
|
|
|
Example:
|
||||
|
|
|
SQL Server + Alter Table + Add Column + Default Value uniqueidentifier
|
||||
|
|
|
This can also be done in the SSMS GUI. I showed a default date below but, the default value can be whatever, of course.
|
||||
|
|
|
Try this
|
|||
|
|
|
Add new column to Table
E.g
If User wants to make it auto incremented then
|
|||
|
|
|
|||||
|
From this query you can add a column of datatype integer with default value 0. |
|||
|
|
|
SQL Server + Alter Table + Add Column + Default Value uniqueidentifier...
|
|||
|
|
|
This can be done by below code.
|
|||
|
|
|
First create a table with name student:
add 1 column to it:
Table created and a column is added to an existing table with a default value. |
|||
|
|
|
If the default is Null, then:
Done! |
||||
|
|
protected by Martin Smith Aug 23 '13 at 7:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

