Home > Database > Mysql Tutorial > Detailed introduction to related methods of querying primary keys in MySQL

Detailed introduction to related methods of querying primary keys in MySQL

PHPz
Release: 2023-04-20 13:40:52
Original
6149 people have browsed it

In the MySQL database, the primary key (Primary Key) is the keyword used to uniquely identify a record. The primary key is an important part of the data table. Its role is to uniquely identify a record and ensure that it cannot be repeated. Therefore, the application of primary keys is very important when querying records in the data table. So, how to query the primary key in MySQL? This article will introduce in detail the relevant methods of querying primary keys in MySQL.

1. Use SHOW INDEX

Use 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 can 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

To query the primary key information according to the table name, 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 are the database name and table name that need to query the primary key.

The INFORMATION_SCHEMA function 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. The primary key information of the specified table can be queried through the above SQL statement.

To sum up, this article introduces three methods of querying primary key information in the MySQL database, namely using the SHOW INDEX function, using the DESC command and using the INFORMATION_SCHEMA function. In actual queries, just choose one of the methods as needed.

The above is the detailed content of Detailed introduction to related methods of querying primary keys in MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template