How to get primary key of table?

前端 未结 14 2245
不思量自难忘°
不思量自难忘° 2020-12-02 15:01

Is there a way to get the name of primary key field from mysql-database? For example:

I have a table like this:

+----+------+
| id | name |
+----+---         


        
14条回答
  •  一整个雨季
    2020-12-02 15:50

    If you want to generate the list of primary keys dynamically via PHP in one go without having to run through each table you can use

    SELECT TABLE_NAME, COLUMN_NAME
    FROM INFORMATION_SCHEMA.key_column_usage 
    WHERE table_schema = '$database_name' AND CONSTRAINT_NAME = 'PRIMARY' 
    

    though you do need to have access to the information.schema to do this.

提交回复
热议问题