2013-08-31 37 views
1

我知道查詢來獲取數據庫的表名是:SQL - 如何提取多字段主鍵

SELECT * 
FROM information_schema.tables 
WHERE table_type = 'BASE TABLE' 

如何獲得的表,如果它的主鍵是由多個列組成?或者獲得特定表的複合主鍵?

回答

1

如果我深知,你可以有TSQL一些選項。

步驟:

exec sp_pkeys 'table', 'schema' 

查看:

如果你想要得到的只是與主要相關的列這個查詢將返回唯一約束Ëforeign_keys相關數據以及

select * from information_schema.key_column_usage 
where table_schema = 'schema' and table_name = 'table' 

鑰匙,你可以嘗試類似的波紋管。我認爲它可以改變數據庫版本,我現在還不確定。

select * 
    from information_schema.key_column_usage as k 
where table_schema = 'schema' and table_name = 'table' 
    and constraint_name = (
    select name 
     from sysobjects as u 
    where k.table_name = object_name(u.parent_obj) 
     and u.xtype = 'PK') 
    order by table_schema, table_name, ordinal_position 

如果這不是答案給我們更多的細節。

+0

兩種表都在數據庫中。那些攜帶數據和那些充當關聯的人。其實我想提取關聯表,主鍵是由其他表的主鍵組成。有什麼辦法嗎? –

0

試試這個:

sp_helpindex 'YourTable'