修改 CHECK 约束Modify Check Constraints

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database

当您希望更改约束表达式或更改对特定条件启用或禁用约束的选项时,可通过使用 SQL ServerSQL ServerSQL Server Management StudioSQL Server Management Studio ,在 Transact-SQLTransact-SQL 中修改 CHECK 约束。You can modify a check constraint in SQL ServerSQL Server by using SQL Server Management StudioSQL Server Management Studio or Transact-SQLTransact-SQL when you want to change the constraint expression or the options that enable or disable the constraint for specific conditions.

本主题内容In This Topic

开始之前Before You Begin

SecuritySecurity

权限Permissions

需要对表的 ALTER 权限。Requires ALTER permission on the table.

使用 SQL Server Management StudioUsing SQL Server Management Studio

修改 CHECK 约束To modify a check constraint

  1. 在“对象资源管理器” 中,右键单击包含 CHECK 约束的表,然后选择“设计”。In the Object Explorer, right-click the table containing the check constraint and select Design.

  2. 在“表设计器”菜单上,单击“CHECK 约束…”。On the Table Designer menu, click Check Constraints....

  3. “CHECK 约束” 对话框中,在 “选定的 CHECK 约束” 下选择要编辑的约束。In the Check Constraints dialog box, under Selected Check Constraint, select the constraint you wish to edit.

  4. 完成下表中的相应操作:Complete an action from the following table:

    功能To 需要遵循的步骤Follow these steps
    编辑约束表达式Edit the constraint expression “表达式” 字段中键入新的表达式。Type the new expression in the Expression field.
    重命名约束Rename the constraint “名称” 字段中键入新的名称。Type a new name in the Name field.
    将该约束应用于现有数据Apply the constraint to existing data 选择 “在创建或启用时检查现有数据” 选项。Select the Check Existing Data on Creation or Enabling option.
    向表中添加新数据或更新表中现有数据时禁用该约束。Disable the constraint when new data is added to the table or when existing data is updated in the table. 清除 “对 INSERT 和 UPDATE 强制约束” 选项。Clear the Enforce Constraint for INSERTs and UPDATEs option.
    当复制代理在表中插入或更新数据时,禁用该约束。Disable the constraint when a replication agent inserts or updates data in your table. 清除 “强制用于复制” 选项。Clear the Enforce For Replication option.

    备注

    对于 CHECK 约束,有些数据库具有不同的功能。Some databases have different functionality for check constraints.

  5. 单击“关闭” 。Click Close.

  6. 在“文件”菜单上,单击“保存表名称”。On the File menu, click Savetable name.

使用 Transact-SQLUsing Transact-SQL

修改 CHECK 约束To modify a check constraint

必须首先删除现有的 CHECK 约束,然后使用新定义重新创建,才能使用 Transact-SQLTransact-SQL修改 CHECK 约束。To modify a CHECK constraint using Transact-SQLTransact-SQL, you must first delete the existing CHECK constraint and then re-create it with the new definition. 有关详细信息,请参阅 删除 CHECK 约束创建 CHECK 约束For more information, see Delete Check Constraints and Create Check Constraints.