如何在T-SQL中删除默认值或类似的约束? [英] How do you drop a default value or similar constraint in T-SQL?

查看:504
本文介绍了如何在T-SQL中删除默认值或类似的约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道语法:

ALTER TABLE [TheTable] DROP CONSTRAINT [TheDefaultConstraint]

但是如果我不知道它的名字,我如何删除默认约束? (也就是说,它在 CREATE TABLE 时间自动生成。)

but how to I drop the default constraint when I don't know its name? (That is, it was autogenerated at CREATE TABLE time.)

推荐答案

如果要手动执行此操作,可以使用Management Studio找到它(在表中的Constraints节点下)。

If you want to do this manually, you can use Management Studio to find it (under the Constraints node inside the table).

使用SQL:


  • 默认约束,可以使用 sys.default_constraints 找到它:

SELECT OBJECT_NAME(parent_object_id) AS TableName, name AS ConstraintName
FROM sys.default_constraints ORDER BY TableName, ConstraintName


  • 如果您正在寻找其他约束(检查,唯一,外键,默认值,主键),可以使用 sysconstraints

    SELECT OBJECT_NAME(id) AS TableName, OBJECT_NAME(constid) AS ConstraintName
    FROM sysconstraints ORDER BY TableName, ConstraintName
    


  • 您不会说使用的是哪个版本的SQL Server。以上对SQL 2005和SQL 2008的工作。

    You do not say which version of SQL Server you are using. The above work on both SQL 2005 and SQL 2008.

    这篇关于如何在T-SQL中删除默认值或类似的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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