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


How to identify the primary primary key in any MySQL database table? Or

Edit 2 What SQL query should be used to display the indices of any table that contains the entire primary key?

I have several tables in the mysql database that hold the overall key of 2 or 3 primary keys, I'm using phpmyadmin, and I need a code for a table script , Use the $ 3 ['TABLE_NAME'] "index from a given query from a query

  to find out which table contains the entire key, I can now recognize the primary key By the tables "Where key_name = 'primary'  

what I want is what I want, but now how can I find out the index where I have the overall key?

1 edit

The overall primary key for viewing the primary primary key in Phpmyadmin Phpmyadmin looks like this:

update:

For further questions to be updated, you can use the following in your PHP script:

  SELECT COUNT (*) num_keys from info_schema.KEY_COLUMN_USAGE where table_name = 'TB' and constraint_name = 'Primary';  

This query is num_keys > 1 if the table is a composite primary key in the table tb .


I am not sure that I have to understand what you are trying to achieve, but you can consider it using the SHOW INDEX :

  create table TB (A int, b int, c int); The query is ok, affected by 0 rows (0.21 seconds) Add optional table TB pk_tb primary key (A, B); The query is ok, the 0 rows affected (0.06 seconds) show TB from the index where key_name = 'primary'; + ------- + ------------ + ---------- + -------------- + - - ---------- + ----------- + ------------- + ---------- + - - ------ + ------ + ------------ + --------- + | Table | Non_ic | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub-Part | Packed | Faucet Index type | Comment | + ------- + ------------ + ---------- + -------------- + - - ---------- + ----------- + ------------- + ---------- + - - ------ + ------ + ------------ + --------- + | TB 0 | Primary | 1 | One | A. Faucet Faucet Faucet | BTRI | | | TB 0 | Primary | 2 | B A. 0 | Faucet Faucet | BTRI | | + ------- + ------------ + ---------- + -------------- + - - ---------- + ----------- + ------------- + ---------- + - - ------ + ------ + ------------ + --------- + 2 rows set (0.02 seconds)  < / Pre> 

If this is not a composite key, then you will find a line in the row index query:

  create table Tb2 (An int, B int, c int); Query OK, Affected by 0 rows (0.05 seconds) Add optional table TB2 Insert pk_tb Primary key (A); The query is ok, 0 shows the affected rows (0.05 seconds) from TB2 index key_name = 'primary'; + ------- + ------------ + ---------- + -------------- + - - ---------- + ----------- + ------------- + ---------- + - - ------ + ------ + ------------ + --------- + | Table | Non_ic | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub-Part | Packed | Faucet Index type | Comment | + ------- + ------------ + ---------- + -------------- + - - ---------- + ----------- + ------------- + ---------- + - - ------ + ------ + ------------ + --------- + | TB2 | 0 | Primary | 1 | One | A. 0 | Faucet Faucet | BTRI | | + ------- + ------------ + ---------- + -------------- + - - ---------- + ----------- + ------------- + ---------- + - - ------ + ------ + ------------ + --------- Rows in + 1 set (0.02 seconds)  < / Pre> 

Comments