Hi Friends,
Do you want to know how to Alter Table for adding new column with Default value? The sysntax of sql server is:
ALTER TABLE table
{
ADD
{
column_name data_type [ ( size ) ]
[ DEFAULT value ]
{ [ NULL | NOT NULL ] | [ { PRIMARY KEY | UNIQUE } ] }
{ [ CONSTRAINT constraint_name ] }
}
|
ALTER COLUMN column_name
{
data_type [ ( size ) ] [ ( precision [ , scale ] ) ]
[ NULL | NOT NULL ]
}
|
DROP { COLUMN column_name | [ CONSTRAINT ] constraint_name }
}
- table
- Specifies which table is to be altered.
- column_name
- The name of the column being added, altered, or dropped.
- data_type
- The data type of the column being added or altered.
- size
- Is the length of the data that can be entered into a field.
- DEFAULT value
- Is the default value for the column being altered.
- NULL | NOT NULL
- Is a parameter that indicates whether a column can or cannot contain null values.
- PRIMARY KEY
- Is a parameter that identifies the column or set of columns whose values uniquely identify each row in a table. Each table can only have one primary key constraint.
- UNIQUE
- Is a constraint that enforces the uniqueness of the values in a set of columns.
- constraint_name
- The name of the constraint to be added or dropped.
- precision
- Specifies the precision for the data type.
- scale
- Specifies the scale for the data type.
The ALTER TABLE statement can be used to modify an existing table by adding, altering, or dropping columns and indexes.
Reference: http://www.devguru.com/technologies/t-sql/7120.asp
So when you want to set the default value when you are adding a new column in the databse table, you can use the following example. I have created a column named ‘tempTest’ and its datatype is ‘bit’ and default value is ‘0′ i.e. false.
ALTER TABLE tblTable
ADD isTempTest bit NOT NULL
CONSTRAINT [DF_tblTable_isTempTest] DEFAULT ((0))
I have added CONSTRAINT ‘DF_tblTable_isTempTest’ to set the default value 0.
You can add you comment here.
Great stuff Vijay, just what i was looking for.
Thanks mate.
By: Soeren Pedersen on September 24, 2007
at 6:22 pm
Really great article
By: Hiren on October 17, 2007
at 12:54 pm
Very very Useful Info. Thanks Vijay
By: Jayabal on October 18, 2007
at 10:24 am
Great. Saved me a good bit of time.
By: Samir Mistry on June 3, 2008
at 2:14 pm
This is what i searched in web. Thanks
By: Suresh Kumar on June 5, 2008
at 11:08 am
How can I copy and paste from MSDN too?
By: Michael O'Neill on June 25, 2008
at 2:14 am
Michael,
I do not understand your problem.
Give detail,
Regards,
Vijay Modi
By: Vijay Modi on June 25, 2008
at 5:02 am
HI Vijay,
I want to alter a existing column and add defalult value.
Can u Please Provide me Solution for this.
Thanks
By: reetesh on July 9, 2008
at 12:35 pm
Hi Reetesh,
the following will work:
ALTER TABLE tblTable
MODIFY isTempTest bit NOT NULL
CONSTRAINT [DF_tblTable_isTempTest] DEFAULT ((0))
Regards,
Vijay Modi
By: Vijay Modi on July 11, 2008
at 5:10 am
Hello Vijay,
In the above example for “altering an existing column and adding defalult value”, I am getting the following error in SQL Server:
Incorrect syntax near ‘MODIFY’.
Can you provide some solution for this.
Thanks.
By: H Kamboj on October 31, 2008
at 4:35 pm
Hi H Kamboj,
Please find the following:
ALTER TABLE tblTable
ALTER isTempTest bit NOT NULL
CONSTRAINT [DF_tblTable_isTempTest] DEFAULT ((0))
Check this one and I think it will help you.
Thanks & Regards,
Vijay Modi
By: Vijay Modi on November 3, 2008
at 5:51 am
Is the use of a named constraint mandatory for adding a default value to a column?
Thanks!
By: Rich on February 5, 2009
at 7:49 pm
I don’ think its mandatory: From the SQL server 2000 help file:
“constraint_name is the new constraint. Constraint names must follow the rules for identifiers, except that the name cannot begin with a number sign (#). If constraint_name is not supplied, a system-generated name is assigned to the constraint.”
By: DavidNL on March 27, 2009
at 8:13 am
Hmm, i have tested it now and i can not get it to work without supplying a contraint name >.<
This works:
ALTER TABLE MyTable
ADD
CONSTRAINT [test] DEFAULT (0) FOR FIELD1
And this does not work:
ALTER TABLE MyTable
ADD
CONSTRAINT DEFAULT (0) FOR FIELD1
By: DavidNL on March 27, 2009
at 8:36 am
What about:
IF (SELECT Count(*) FROM sysobjects o, syscolumns c WHERE o.name = ‘DefinitionA’ and c.name = ‘gd_var’ and o.id = c.id) = 0
BEGIN
ALTER TABLE DefinitionA
ADD gd_var BIT NOT NULL DEFAULT 0
END
?
By: Steven on July 22, 2009
at 2:44 pm
Vijay, I was looking for information on how to add a default to an existing column and found your site.
Michael O above was being sarcastic because your post is similar to the MSDN documentation. Don’t worry, your site is better.
While I had to look elsewhere for my solution, I thought I’d add it to your site because it is so popular:
USE [database]
GO
ALTER TABLE [dbo].[table_name] ADD CONSTRAINT [DF_table_name_column_name] DEFAULT (getdate()) FOR [column_name]
GO
Similar syntax can be used to drop the constraint.
By: scott on August 7, 2009
at 6:21 pm
my query is that if the value of the textbox in c# then alter table use in the query is
“alter table Packing_Obj ADD ‘”+ textBox1.Text +”‘ varchar(50)”
how can be enter the colume nane in the database
By: arunvishnoi on August 25, 2009
at 5:44 am
wow, really good article, thanks, it helped me solving the issue i was facing. (Y)
By: Zain Shaikh on September 5, 2009
at 3:50 pm
Hi Vijay Modi,
A default column value can be equal to the number of records in other tables?
For Example :
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT ‘Select Count(id) etc. etc.’
)
Best regards.
By: Ushar on October 12, 2009
at 8:04 am
thanks, it was helpful
By: Jarek on October 29, 2009
at 12:14 pm