首页 > 代码库 > SQL Server 查询表的主键的两种方式

SQL Server 查询表的主键的两种方式

方式1:

select b.column_namefrom information_schema.table_constraints ainner join information_schema.constraint_column_usage bon a.constraint_name = b.constraint_namewhere a.constraint_type = PRIMARY KEY and a.table_name = productsgo

 


方式2:

SELECT a.name   FROM   syscolumns a   inner  join sysobjects d on a.id=d.id         where  d.name=products and exists(SELECT 1 FROM sysobjects where xtype=PK and  parent_obj=a.id and name in (    SELECT name  FROM sysindexes   WHERE indid in(    SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid  )))

 

方式3:

SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE   WHERE TABLE_NAME=PDA_xjData 

 

SQL Server 查询表的主键的两种方式