码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-03-31 16:28:40      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:mat   and   products   type   方式   查询   data   str   where   

方式1:

select b.column_name
from information_schema.table_constraints a
inner join information_schema.constraint_column_usage b
on a.constraint_name = b.constraint_name
where a.constraint_type = PRIMARY KEY and a.table_name = products
go

 


方式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 查询表的主键的两种方式

标签:mat   and   products   type   方式   查询   data   str   where   

原文地址:http://www.cnblogs.com/zouhao/p/6651995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!