开发者

SQL 2000/2005/2008 - Find Unique Constraint Name for a column

开发者 https://www.devze.com 2023-01-17 17:28 出处:网络
I have a table with a column that needs the 开发者_运维知识库data type upgrading.However running any alter script causes errors due to a unnamed unique constraint.

I have a table with a column that needs the 开发者_运维知识库data type upgrading. However running any alter script causes errors due to a unnamed unique constraint.

I need to drop this constraint but unfortunately I do not know the name. I have a script which currently lists all the unique constraints on the table but I need to find out how to go that one step further and associate the constraint names with the columns.

Select *
From sysobjects
Where sysobjects.xtype = 'UQ' AND sysobjects.parent_obj= OBJECT_ID(N'Users')

this returns

UQ__Users__45F365D3
UQ__Users__46E78AOC

I need to know which columns theses are linked to in order to delete the right one. I need to support SQL 2000, 2005, and 2008.

Any suggestions would be appreciated.

Thanks Ben


You should be able to use INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE to establish this.

SELECT 
   CONSTRAINT_NAME 
FROM 
   INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
WHERE 
   TABLE_NAME = 'TableName' 
   AND COLUMN_NAME = 'ColumnName'

Not certain whether the view is fully supported in SQL 2000 though.

0

精彩评论

暂无评论...
验证码 换一张
取 消