更新:
而且更新後的問題,你可能需要使用你的PHP腳本如下:
SELECT COUNT(*) num_keys
FROM information_schema.KEY_COLUMN_USAGE
WHERE table_name ='tb' AND constraint_name = 'PRIMARY';
這個查詢將返回num_keys
> 1,如果表tb
有一個複合主鍵。
我不知道如果我明白你想實現什麼,但你可能要考慮使用SHOW INDEX
如下:
CREATE TABLE tb (a int, b int, c int);
Query OK, 0 rows affected (0.21 sec)
ALTER TABLE tb ADD CONSTRAINT pk_tb PRIMARY KEY (a, b);
Query OK, 0 rows affected (0.06 sec)
SHOW INDEX FROM tb WHERE key_name='PRIMARY';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tb | 0 | PRIMARY | 1 | a | A | NULL | NULL | NULL | | BTREE | |
| tb | 0 | PRIMARY | 2 | b | A | 0 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.02 sec)
如果不是一個複合鍵,你會只有在SHOW INDEX
查詢獲取一行:
CREATE TABLE tb2 (a int, b int, c int);
Query OK, 0 rows affected (0.05 sec)
ALTER TABLE tb2 ADD CONSTRAINT pk_tb PRIMARY KEY (a);
Query OK, 0 rows affected (0.05 sec)
SHOW INDEX FROM tb2 WHERE key_name='PRIMARY';
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tb2 | 0 | PRIMARY | 1 | a | A | 0 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.02 sec)
你是什麼意思識別?你使用什麼工具? phpMyAdmin的? SQLyog的?或者你想以編程的方式做到這一點?如果是這樣,什麼語言? – 2010-05-17 11:26:16
@OM永恆:重新形象,是不是足夠確定你的鑰匙是複合? (由'moduleid'和'menuid'組成) – 2010-05-17 11:56:02
我的朋友Daniel請嘗試理解我的PHP腳本沒有眼睛來識別組合鍵...... :)請告訴我什麼方法來找出「這張桌子有複合材料鑰匙....「請正確閱讀我的問題... – 2010-05-17 11:58:19