Add a column with a default value to an existing table in SQL Server

后端 未结 30 2174
轮回少年
轮回少年 2020-11-22 12:00

How can I add a column with a default value to an existing table in SQL Server 2000 / SQL Server 2005?

相关标签:
30条回答
  • 2020-11-22 12:14

    This is for SQL Server:

    ALTER TABLE TableName
    ADD ColumnName (type) -- NULL OR NOT NULL
    DEFAULT (default value)
    WITH VALUES
    

    Example:

    ALTER TABLE Activities
    ADD status int NOT NULL DEFAULT (0)
    WITH VALUES
    

    If you want to add constraints then:

    ALTER TABLE Table_1
    ADD row3 int NOT NULL
    CONSTRAINT CONSTRAINT_NAME DEFAULT (0)
    WITH VALUES
    
    0 讨论(0)
  • 2020-11-22 12:15

    Syntax:

    ALTER TABLE {TABLENAME} 
    ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
    CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
    WITH VALUES
    

    Example:

    ALTER TABLE SomeTable
            ADD SomeCol Bit NULL --Or NOT NULL.
     CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is autogenerated.
        DEFAULT (0)--Optional Default-Constraint.
    WITH VALUES --Add if Column is Nullable and you want the Default Value for Existing Records.
    

    Notes:

    Optional Constraint Name:
    If you leave out CONSTRAINT D_SomeTable_SomeCol then SQL Server will autogenerate
        a Default-Contraint with a funny Name like: DF__SomeTa__SomeC__4FB7FEF6

    Optional With-Values Statement:
    The WITH VALUES is only needed when your Column is Nullable
        and you want the Default Value used for Existing Records.
    If your Column is NOT NULL, then it will automatically use the Default Value
        for all Existing Records, whether you specify WITH VALUES or not.

    How Inserts work with a Default-Constraint:
    If you insert a Record into SomeTable and do not Specify SomeCol's value, then it will Default to 0.
    If you insert a Record and Specify SomeCol's value as NULL (and your column allows nulls),
        then the Default-Constraint will not be used and NULL will be inserted as the Value.

    Notes were based on everyone's great feedback below.
    Special Thanks to:
        @Yatrix, @WalterStabosz, @YahooSerious, and @StackMan for their Comments.

    0 讨论(0)
  • 2020-11-22 12:15
    ALTER TABLE <table name> 
    ADD <new column name> <data type> NOT NULL
    GO
    ALTER TABLE <table name> 
    ADD CONSTRAINT <constraint name> DEFAULT <default value> FOR <new column name>
    GO
    
    0 讨论(0)
  • 2020-11-22 12:15

    This has a lot of answers, but I feel the need to add this extended method. This seems a lot longer, but it is extremely useful if you're adding a NOT NULL field to a table with millions of rows in an active database.

    ALTER TABLE {schemaName}.{tableName}
        ADD {columnName} {datatype} NULL
        CONSTRAINT {constraintName} DEFAULT {DefaultValue}
    
    UPDATE {schemaName}.{tableName}
        SET {columnName} = {DefaultValue}
        WHERE {columName} IS NULL
    
    ALTER TABLE {schemaName}.{tableName}
        ALTER COLUMN {columnName} {datatype} NOT NULL
    

    What this will do is add the column as a nullable field and with the default value, update all fields to the default value (or you can assign more meaningful values), and finally it will change the column to be NOT NULL.

    The reason for this is if you update a large scale table and add a new not null field it has to write to every single row and hereby will lock out the entire table as it adds the column and then writes all the values.

    This method will add the nullable column which operates a lot faster by itself, then fills the data before setting the not null status.

    I've found that doing the entire thing in one statement will lock out one of our more active tables for 4-8 minutes and quite often I have killed the process. This method each part usually takes only a few seconds and causes minimal locking.

    Additionally, if you have a table in the area of billions of rows it may be worth batching the update like so:

    WHILE 1=1
    BEGIN
        UPDATE TOP (1000000) {schemaName}.{tableName}
            SET {columnName} = {DefaultValue}
            WHERE {columName} IS NULL
    
        IF @@ROWCOUNT < 1000000
            BREAK;
    END
    
    0 讨论(0)
  • 2020-11-22 12:17

    When adding a nullable column, WITH VALUES will ensure that the specific DEFAULT value is applied to existing rows:

    ALTER TABLE table
    ADD column BIT     -- Demonstration with NULL-able column added
    CONSTRAINT Constraint_name DEFAULT 0 WITH VALUES
    
    0 讨论(0)
  • 2020-11-22 12:17

    In SQL Server 2008-R2, I go to the design mode - in a test database - and add my two columns using the designer and made the settings with the GUI, and then the infamous Right-Click gives the option "Generate Change Script"!

    Bang up pops a little window with, you guessed it, the properly formatted guaranteed-to-work change script. Hit the easy button.

    0 讨论(0)
提交回复
热议问题