views:

154

answers:

2

How to identify composite primary key in any Mysql Database table? or

EDIT 2 what sql query should be used to display the indees of any table who contains the composite primary keys?

I have many tables in mysql database which are having composite keys of 2 or 3 primary keys, I am using phpmyadmin, and I have to code a php script to identify which table has the composite keys, right now i can identify the primary key of the tables by using a query

SHOW INDEXES FROM `".$row3['TABLE_NAME']."` WHERE Key_name = 'PRIMARY'

which is giving me what i want, but now how can i find out the indexes where i have composite keys?

EDIT 1

In the context of Daniel Image comment for look of composite primary keys in phpmyadmin

composite primary keys look like this in phpmyadmin: alt text

+3  A: 

UPDATE:

Further to the updated question, you may want to use the following in your PHP script:

SELECT COUNT(*) num_keys 
FROM   information_schema.KEY_COLUMN_USAGE     
WHERE  table_name ='tb' AND constraint_name = 'PRIMARY';

This query will return num_keys > 1 if table tb has a composite primary key.


I'm not sure if I understood what you are trying to achieve, but you may want to consider using SHOW INDEX as follows:

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)

If it were not a composite key, you would only get one row in the SHOW INDEX query:

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)
Daniel Vassallo
Daniel if u r aware of phpmyadmin gui it displays the composite keys all together for a single row..one after other stepwise..
OM The Eternity
@OM: Don't you get something like this? http://img248.imageshack.us/img248/6843/showindex.png
Daniel Vassallo
No Its not like this, chk the update question i have pasted the image over there...
OM The Eternity
@OM The Eternity: It looks like it returned the count of all the keys in your database. Are you sure you have the `WHERE table_name ='your_table_name'` filter?... In addition you may want to add ` AND table_schema = 'your_schema_name'` if you have the same table name used in many schemas.
Daniel Vassallo
@Daniel I have correct ur select query from informartion schema... see the answer I have given below... we need to specify the table_schema as well otherwise i will output all dbs common table's name num_keys... I hope u inderstood what i mean.. :)
OM The Eternity
@OM The Eternity: Yep, just thought about that :)
Daniel Vassallo
N e ways Thanks for the Unfrustrated Support... :)
OM The Eternity
+1  A: 
SELECT COUNT( *  ) num_keys
FROM information_schema.KEY_COLUMN_USAGE
WHERE table_name = 'jos_modules_menu'
AND constraint_name = 'PRIMARY'
AND table_schema = 'pranav_test'

Thanks Daniel and Pranav :)

OM The Eternity