How to add multiple columns to a table and add def

2020-05-30 02:54发布

I want to add 2 new columns to existing table.

One of them should be NOT NULL with default value 0 (filled in the existing rows as well).

I have tried the following syntax:

Alter TABLE dbo.MamConfiguration
    add [IsLimitedByNumOfUsers] [bit]  NOT NULL,
    CONSTRAINT IsLimitedByNumOfUsers_Defualt [IsLimitedByNumOfUsers] DEFAULT 0
    [NumOfUsersLimit] [int] NULL
go

But it throws exception. How should I write it?

3条回答
迷人小祖宗
2楼-- · 2020-05-30 03:35

Try this.

ALTER TABLE dbo.MamConfiguration  
ADD [IsLimitedByNumOfUsers] [bit]  NOT NULL DEFAULT 0,     
[NumOfUsersLimit] [int] NULL  
查看更多
▲ chillily
3楼-- · 2020-05-30 03:44

You can use this:

ALTER TABLE dbo.MamConfiguration
ADD [IsLimitedByNumOfUsers] [BIT] NOT NULL DEFAULT 0,   
    [NumOfUsersLimit] [INT] NULL
GO

or this:

ALTER TABLE dbo.MamConfiguration
ADD [IsLimitedByNumOfUsers] [BIT] NOT NULL 
        CONSTRAINT IsLimitedByNumOfUsers_Default DEFAULT 0,
    [NumOfUsersLimit] [INT] NULL
go

More: ALTER TABLE

查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-30 03:51

To add multiple columns to a table and add default constraint on one of them-

ALTER TABLE dbo.MamConfiguration
ADD [IsLimitedByNumOfUsers] [BIT] CONSTRAINT Def_IsLimitedByNumOfUsers DEFAULT(0) NOT NULL,   
    [NumOfUsersLimit] [INT] NULL;
GO
查看更多
登录 后发表回答