1. Use SHOW INDEX
Use the SHOW INDEX function to query the primary key information of the specified table. The syntax of this function is as follows:
SHOW INDEX FROM 表名 WHERE Key_name = 'PRIMARY';
To query the primary key information according to the table name, you can use the following SQL statement:
SHOW INDEX FROM 表名 WHERE Key_name = 'PRIMARY';
Among them, the table name is the name of the table whose primary key needs to be queried.
When a table in the MySQL database contains multiple primary keys, you can use the following SQL statement to query all primary key information of the specified table:
SHOW INDEX FROM 表名 WHERE Non_unique = 0 AND Key_name IN ('PRIMARY');
2. Use the DESC command
Use the DESC command to query related information of the specified table, including primary key information. The syntax of this command is as follows:
DESC 表名;
To query the primary key information according to the table name, you can use the following SQL statement:
DESC 表名;
Among them, the table name is the name of the table whose primary key needs to be queried.
The DESC command will return a result set containing column information of the specified table. In this result set, the primary key information will be marked as PRI.
3. Use INFORMATION_SCHEMA
Use the INFORMATION_SCHEMA function to query the primary key information of the specified table. The syntax of this function is as follows:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '数据库名' AND TABLE_NAME = '表名' AND COLUMN_KEY = 'PRI'
Query according to the table name For primary key information, you can use the following SQL statement:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '数据库名' AND TABLE_NAME = '表名' AND COLUMN_KEY = 'PRI'
Among them, the database name and table name need to be queried for the primary key.
By using the INFORMATION_SCHEMA function, you can retrieve a result set containing the specified table column information. In this result set, the primary key information will be marked as PRI. The primary key information of the specified table can be queried through the above SQL statement.
The above is the detailed content of How to query primary key in MySQL. For more information, please follow other related articles on the PHP Chinese website!