您可以替换或更新 SQL 约束吗? [英] Can you replace or update a SQL constraint?

查看:11
本文介绍了您可以替换或更新 SQL 约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我称为等级"的列编写了以下约束:

I have written the following constraint for a column I've called 'grade':

CONSTRAINT gradeRule CHECK grade IN (‘easy’, ‘moderate’, ‘difficult’),

以后是否可以更新 gradeRule 以具有不同的值?例如,中等"和困难"可以更改为中等"和困难".

Is it possible to later update the gradeRule to have different values? For example, 'moderate' and 'difficult' could be changed to 'medium' and 'hard'.

谢谢

推荐答案

您可以删除现有约束,并使用 NOCHECK 选项添加新约束.即使表中的数据违反了约束,这也允许您添加约束.这样做的问题是,如果不先让它们通过约束,您将无法更新现有记录.

You could drop the existing constraint, and add the new constraint with the NOCHECK option. This would allow you to add the constraint even though data in the table violates the constraint. The problem with doing this though would be that you wouldn't be able to update existing records without making them pass the constraint first.

ALTER TABLE SomeTable DROP CONSTRAINT gradeRule
GO
ALTER TABLE SomeTable ADD CONSTRAINT gradeRule ... WITH NOCHECK
GO

虽然这是可能的,但通常不建议这样做,因为未来更新数据可能会出现问题.

Although this is possible, its not usually recommended because of the potential problems with future updates of the data.

这篇关于您可以替换或更新 SQL 约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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