Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以编写一个查询来确定表是否具有主键?
SELECT * FROM sysobjects WHERE id = OBJECT_ID('TRS') and xtype = PK
如果它没有主要的,那么它不会返回任何内容。
在这个链接中,作者谈到了使用Xtype,但它似乎是一个古老的术语。
Xtype
如果有主键,这会给你键和它的列名,否则什么都没有。
SELECT sysobjects.name, syscolumns.name FROM sysobjects INNER JOIN syscolumns ON syscolumns.id = sysobjects.id INNER JOIN syskeys ON syskeys.id = sysobjects.id WHERE sysobjects.type = 'U' AND syskeys.type = 1