用于添加默认约束的命令 [英] Command for adding a default constraint

查看:214
本文介绍了用于添加默认约束的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用直接T-SQL似乎至少有两种方法来添加默认约束.我是否正确,以下两者之间的唯一区别是第二种方法专门为约束创建了一个名称,而第一种方法有一个由SQL Server生成的名称?

There appears to be at least two ways to add a default constraint using straight T-SQL. Am I correct that the only difference between the two below is that the second method specifically creates a name for the constraint, and the first method has one generated by SQL Server?

ALTER TABLE [Common].[PropertySetting] ADD DEFAULT ((1)) FOR [Active];
ALTER TABLE [Common].[PropertySetting] ADD CONSTRAINT [DF_PropertySetting_Active) DEFAULT ((1)) FOR [Active];

推荐答案

相当多,对于ALTER TABLE是可以的

Pretty much, yes for an ALTER TABLE

您也可以一步添加默认值的columnn来进行CREATE或ALTER.

You can add a columnn with default in one step for CREATE or ALTER too.

ALTER TABLE foo ADD bar varchar(100) CONSTRAINT DF_Foo_Bar DEFAULT ('bicycle')
ALTER TABLE foo ADD bar varchar(100) DEFAULT ('bicycle')

如您所述,如果未提供名称,则系统会生成一个名称. MSDN说, CONSTRAINT constraint_name 是可选的.对于任何

As you noted, the system generates a name if one is not supplied. CONSTRAINT constraint_name is optional says MSDN. The same applies to any column or table CONSTRAINT

修改 如果该列已经创建,而您只想添加约束,则使用:

Edit If the column was already created, and you only want to add the constraint use:

ALTER TABLE TableName ADD CONSTRAINT DF_Foo_Bar DEFAULT 'bicycle' FOR FieldName;

这篇关于用于添加默认约束的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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