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

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

问题描述

我已经为我称为grade的列写了以下约束:

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

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

code> 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天全站免登陆