How to query primary key in MySQL

WBOY
Release: 2023-05-28 15:28:12
forward
4150 people have browsed it

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';
Copy after login
Copy after login

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';
Copy after login
Copy after login

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');
Copy after login

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 表名;
Copy after login
Copy after login

To query the primary key information according to the table name, you can use the following SQL statement:

DESC 表名;
Copy after login
Copy after login

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'
Copy after login
Copy after login

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'
Copy after login
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!