如何向表中添加多个列并在其中之一上添加默认约束? [英] How to add multiple columns to a table and add default constraint on one of them?

查看:49
本文介绍了如何向表中添加多个列并在其中之一上添加默认约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向现有表中添加 2 个新列.

I want to add 2 new columns to existing table.

其中一个应该是 NOT NULL,默认值为 0(也填写现有行).

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

我尝试了以下语法:

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?

推荐答案

你可以使用这个:

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

或者这个:

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

更多:ALTER TABLE

这篇关于如何向表中添加多个列并在其中之一上添加默认约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆