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

后端 未结 30 2639
轮回少年
轮回少年 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
    

提交回复
热议问题