2011-07-28 65 views
0

如果我有一個表同樣創建於以下內容:獲得MySQL獨特的按鍵組合

CREATE TABLE MyTable(
    id1Part1 INT NOT NULL, 
    id1Part2 INT NOT NULL, 

    id2Part1 INT NOT NULL, 
    id2Part2 INT NOT NULL, 

    UNIQUE KEY (id1Part1, id1Part2), 
    UNIQUE KEY (id2Part1, id2Part2) 
); 

我怎麼能現在數據庫給我的兩個「唯一密鑰」的元組?
SHOW INDEX似乎並沒有做到這一點。)

回答

1

我不知道如果你正在尋找這樣的事情

select 
constraint_name, 
group_concat(column_name order by ordinal_position) as cols 
from information_schema.key_column_usage 
where table_schema = 'db_name' and table_name = 'table_name' 
group by constraint_name 
+0

嗯...我會盡量檢查它,當我有機會,謝謝。似乎可能是我需要的。 – Mehrdad

1

MySQL不能給你兩個主鍵。看看這個:

mysql> CREATE TABLE MyTable(
    ->  id1Part1 INT NOT NULL, 
    ->  id1Part2 INT NOT NULL, 
    -> 
    ->  id2Part1 INT NOT NULL, 
    ->  id2Part2 INT NOT NULL, 
    -> 
    ->  UNIQUE KEY (id1Part1, id1Part2), 
    ->  UNIQUE KEY (id2Part1, id2Part2) 
    ->); 
Query OK, 0 rows affected (0.17 sec) 

mysql> desc mytable; 
+----------+---------+------+-----+---------+-------+ 
| Field | Type | Null | Key | Default | Extra | 
+----------+---------+------+-----+---------+-------+ 
| id1Part1 | int(11) | NO | PRI | NULL |  | 
| id1Part2 | int(11) | NO | PRI | NULL |  | 
| id2Part1 | int(11) | NO | MUL | NULL |  | 
| id2Part2 | int(11) | NO |  | NULL |  | 
+----------+---------+------+-----+---------+-------+ 

它顯示只有一個主鍵。

+0

我很困惑......我們在談論*獨特*鍵或*主鍵*鍵嗎? – Mehrdad