国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

找SQL表的主鍵

2024-07-21 02:08:27
字體:
來源:轉載
供稿:網友

1:利用sql自帶的存儲過程

exec sp_pkeys @table_name='表名'

2:利用系統表information_schema.key_column_usage

select table_name,column_name from information_schema.key_column_usage
where table_name='表名'

3: 以下這個查詢表結構的語句可能大家都見過:

利用syscolumns,sysindexes,sysindexkeys三個表關聯可以查出主鍵

select
 (case when a.colorder=1 then d.name else '' end) n'表名',
 a.colorder n'字段序號',
 a.name n'字段名',
 (case when columnproperty( a.id,a.name,'isidentity')=1 then '√'else '' end) n'標識',
 (case when (select count(*)
 from sysobjects
 where (name in
           (select name
          from sysindexes
          where (id = a.id) and (indid in
                    (select indid
                   from sysindexkeys
                   where (id = a.id) and (colid in
                             (select colid
                            from syscolumns
                            where (id = a.id) and (name = a.name))))))) and
        (xtype = 'pk'))>0 then '√' else '' end) n'主鍵',
 b.name n'類型',
 a.length n'占用字節數',
 columnproperty(a.id,a.name,'precision') as n'長度',
 isnull(columnproperty(a.id,a.name,'scale'),0) as n'小數位數',
 (case when a.isnullable=1 then '√'else '' end) n'允許空',
 isnull(e.text,'') n'默認值',
 isnull(g.[value],'') as n'字段說明'
--into ##tx

from  syscolumns  a left join systypes b
on  a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id  and  d.xtype='u' and  d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sysproperties g
on a.id=g.id and a.colid = g.smallid 
order by object_name(a.id),a.colorder


4:


select o.name as 表名,c.name as 字段名,k.colid as 字段序號,k.keyno as 索引順序 from sysindexes i
join sysindexkeys k on i.id = k.id and i.indid = k.indid
join sysobjects o on i.id = o.id
join syscolumns c on i.id=c.id and k.colid = c.colid
where o.xtype = 'u'
and exists(select 1 from sysobjects where xtype = 'pk' and name = i.name)
order by o.name,k.colid


 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 砚山县| 玛纳斯县| 叶城县| 扬中市| 治多县| 抚远县| 鄯善县| 武清区| 塘沽区| 九龙县| 辽中县| 清水河县| 永登县| 锡林郭勒盟| 潮州市| 新民市| 日喀则市| 南昌市| 余干县| 龙川县| 嵊州市| 蕲春县| 沧源| 潜江市| 巢湖市| 广南县| 洛阳市| 新建县| 庆安县| 鄢陵县| 西昌市| 尉犁县| 札达县| 东丽区| 兴海县| 岑溪市| 贡嘎县| 肇源县| 镇巴县| 花莲市| 晋江市|