Home > Database > Mysql Tutorial > How to query the foreign key constraints of a table in mysql?

How to query the foreign key constraints of a table in mysql?

青灯夜游
Release: 2020-10-19 11:02:02
Original
5761 people have browsed it

Mysql method to query the foreign key constraints of a table: use the "SHOW CREATE TABLE" statement, the syntax format "SHOW CREATE TABLE ;", you can display the use of the data table name All constraints, including primary key constraints, foreign key constraints, non-null constraints, unique constraints, etc.

How to query the foreign key constraints of a table in mysql?

(Recommended tutorial: mysql video tutorial)

You can use the SHOW CREATE TABLE statement in MySQL to view the constraints in the table and then query the foreign key constraints.

View the constraint syntax format in the data table as follows:

SHOW CREATE TABLE <数据表名>;
Copy after login

Example

Create the data table tb_emp8 and specify id as the primary key constraint and name as the unique Constraints, deptId is a non-null constraint and a foreign key constraint, and then check the constraints in the table. The results of the SQL statement are as follows.

mysql> CREATE TABLE tb_emp8
    -> (
    -> id INT(11) PRIMARY KEY,
    -> name VARCHAR(22) UNIQUE,
    -> deptId INT(11) NOT NULL,
    -> salary FLOAT DEFAULT 0,
    -> CHECK(salary>0),
    -> FOREIGN KEY(deptId) REFERENCES tb_dept1(id)
    -> );
Query OK, 0 rows affected (0.37 sec)
mysql> SHOW CREATE TABLE tb_emp8 \G
*************************** 1. row ***************************
       Table: tb_emp8
Create Table: CREATE TABLE `tb_emp8` (
  `id` int(11) NOT NULL,
  `name` varchar(22) DEFAULT NULL,
  `deptId` int(11) NOT NULL,
  `salary` float DEFAULT &#39;0&#39;,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `deptId` (`deptId`),
  CONSTRAINT `tb_emp8_ibfk_1` FOREIGN KEY (`deptId`) REFERENCES `tb_dept1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
1 row in set (0.19 sec)
Copy after login

The above is the detailed content of How to query the foreign key constraints of a table in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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