Posted by: Vijay Modi | September 10, 2007

MS Sql Server: Alter Table set default value

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.


Responses

  1. Great stuff Vijay, just what i was looking for.

    Thanks mate.

  2. Really great article

  3. Very very Useful Info. Thanks Vijay

  4. Great. Saved me a good bit of time.

  5. This is what i searched in web. Thanks

  6. How can I copy and paste from MSDN too?

  7. Michael,

    I do not understand your problem.

    Give detail,

    Regards,
    Vijay Modi

  8. HI Vijay,

    I want to alter a existing column and add defalult value.

    Can u Please Provide me Solution for this.

    Thanks

  9. Hi Reetesh,

    the following will work:

    ALTER TABLE tblTable
    MODIFY isTempTest bit NOT NULL
    CONSTRAINT [DF_tblTable_isTempTest] DEFAULT ((0))

    Regards,
    Vijay Modi

  10. 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.

  11. 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

  12. Is the use of a named constraint mandatory for adding a default value to a column?
    Thanks!

  13. 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.”

  14. 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

  15. 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

    ?

    :)

  16. 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.

  17. 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

  18. wow, really good article, thanks, it helped me solving the issue i was facing. (Y)

  19. 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.

  20. thanks, it was helpful


Leave a response

Your response:

Categories