SQL Server 2000 删除带有约束的列

发布于 2024-07-17 07:03:44 字数 538 浏览 4 评论 0原文

我遇到了与这个问题中描述的相同的问题,但就是这样SQL Server 2005 和“已接受”的答案在 SQL Server 2000 中不起作用。

具体来说:我正在尝试运行 ALTER TABLE foo DROP COLUMN bar,但它失败了,因为存在“默认约束”。 ” 这意味着,我在该列上有一个默认值,SQL Server 将其实现为一个单独的约束,我需要先将其删除。

问题是创建列时没有为默认约束指定名称,因此我必须查询系统表以发现约束的(自动生成的)名称。

其他问题中给出的答案适用于 SQL Server 2005,但不适用于 SQL Server 2000。我需要后者。

[更新]我需要一个查询来回答“表foo中列bar的默认约束的名称是什么”这个问题。 这不是人类手动寻找答案的方法。

I have the same problem as described in this question, but there it's SQL Server 2005 and the "accepted" answer doesn't work in SQL Server 2000.

Specifically: I'm trying to run ALTER TABLE foo DROP COLUMN bar, and it's failing because there's a "default constraint." Meaning, I have a default value on that column which SQL Server implements as a separate constraint that I need to delete first.

The problem is no name was given for the default constraint when the column was created, so I have to query the system tables to discover the (auto-generated) name of the constraint.

The answer given in that other question works for me in SQL Server 2005 but not in SQL Server 2000. I need the latter.

[UPDATE] I need a query that can answer the question "What is the name of the default constraint for column bar in table foo." Not a way for a human being to manually find the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把昨日还给我 2024-07-24 07:03:44

刚刚弄清楚引用的 SQL 2005 查询实际上在做什么。 下面是在 SQL 2000 中运行的该查询的复制

select 
    col.name, 
    col.colorder, 
    col.cdefault, 
    OBJECTPROPERTY(col.cdefault, N'IsDefaultCnst') as is_defcnst, 
    dobj.name as def_name
from syscolumns col 
    left outer join sysobjects dobj 
        on dobj.id = col.cdefault and dobj.type = 'D' 
where col.id = object_id(N'dbo.table_name') 
and dobj.name is not null

Just figured out what the referenced SQL 2005 query was actually doing. Here's a replication of that query that works in SQL 2000

select 
    col.name, 
    col.colorder, 
    col.cdefault, 
    OBJECTPROPERTY(col.cdefault, N'IsDefaultCnst') as is_defcnst, 
    dobj.name as def_name
from syscolumns col 
    left outer join sysobjects dobj 
        on dobj.id = col.cdefault and dobj.type = 'D' 
where col.id = object_id(N'dbo.table_name') 
and dobj.name is not null
西瑶 2024-07-24 07:03:44

要获取表的默认约束列表,请尝试以下操作

select * from sysobjects [constraint] 
 join sysobjects [table] on [constraint].parent_obj = [table].id 
where [constraint].type = 'D'
 and [table].name = 'table_name'
--another option: and [table].id = OBJECT_ID(N'dbo.table_name')

To get a list of the default constraints for a table, try this

select * from sysobjects [constraint] 
 join sysobjects [table] on [constraint].parent_obj = [table].id 
where [constraint].type = 'D'
 and [table].name = 'table_name'
--another option: and [table].id = OBJECT_ID(N'dbo.table_name')
半葬歌 2024-07-24 07:03:44
sp_help 'tablename'

为您提供有关表的一堆信息 - 包括所有约束和约束名称

实际上 sp_help 调用此:

sp_helpconstraint 'tablename', nomsg

默认约束将显示“列 xxx 上的默认值”

sp_help 'tablename'

gives you a bunch of info on the table - including all the constraints and the constraint names

Actually sp_help calls this:

sp_helpconstraint 'tablename', nomsg

Default constraints will say "DEFAULT on column xxx"

这个是在 bdukes 提供的第一个解决方案的基础上构建的,与 information_schema.columns 混合,似乎 info 包含默认所属列的序号位置

SELECT name 
FROM sysobjects [constraint] 
 JOIN sysobjects [table] on [constraint].parent_obj = [table].id
 JOIN information_schema.columns col ON [table].name = col.table_name AND [constraint].info = col.ordinal_position
WHERE [constraint].type = 'D'
 AND [table].name = 'table_name'
 AND col.column_name = 'column_name'

This one is constructed over the first solution provided by bdukes, mixed with information_schema.columns, it seems that info contains the ordinal position of the column the default belongs

SELECT name 
FROM sysobjects [constraint] 
 JOIN sysobjects [table] on [constraint].parent_obj = [table].id
 JOIN information_schema.columns col ON [table].name = col.table_name AND [constraint].info = col.ordinal_position
WHERE [constraint].type = 'D'
 AND [table].name = 'table_name'
 AND col.column_name = 'column_name'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文