if exists (select 1 from sysconstraints a,syscolumns b where a.id =object_id('tablename') and b.id= object_id('tablename') and a. status =2 and a.colid = b.colid and b.name = 'colname') print '有UNIQUE KEY 约束' else print '没有UNIQUE KEY 约束'
status =2 有问题,上面是不对的。
if exists (select 1 from sysconstraints a,syscolumns b where a.id =object_id('test_key') and b.id= object_id('test_key') and OBJECTPROPERTY (a.constid,'IsUniqueCnst') =1 and ((a.colid = b.colid and b.name = 'a') or (a.colid=0))) print '有UNIQUE KEY 约束' else print '没有UNIQUE KEY 约束'
select ColName=name,IsUnique=(case colstat when 2 then 'Yes' else 'No' end) from syscolumns where id*=(select id from sysobjects where name='YourTableName')
colstat为2就是Unique? Help中说syscolumns.colstat是"For internal use only."
包含约束映射,映射到拥有该约束的对象。该系统目录存储在每个数据库中。列名 数据类型 描述
constid int 约束号。
id int 拥有该约束的表 ID。
colid smallint 在其上定义约束的列 ID,如果是表约束则为 0。
spare1 tinyint 保留。
status int 位图指示状态。可能的值包括:
1 = PRIMARY KEY 约束。
2 = UNIQUE KEY 约束。
3 = FOREIGN KEY 约束。
4 = CHECK 约束。
5 = DEFAULT 约束。
16 = 列级约束。
32 = 表级约束。
actions int 保留。
error int 保留。
status =2 and a.colid = b.colid and b.name = 'colname') print '有UNIQUE KEY 约束' else print '没有UNIQUE KEY 约束'
if exists (select 1 from sysconstraints a,syscolumns b where a.id =object_id('test_key') and b.id= object_id('test_key') and
OBJECTPROPERTY (a.constid,'IsUniqueCnst') =1 and ((a.colid = b.colid and b.name = 'a') or (a.colid=0))) print '有UNIQUE KEY 约束' else print '没有UNIQUE KEY 约束'
每行记录对应该数据表中的一个字段的信息,行集有两列:ColName(字段名)和IsUnique(是否唯一)
用print打出来的结果我在程序里没法用。
Help中说syscolumns.colstat是"For internal use only."