;", you can display all the constraints using the data table name, including primary key constraints, foreign key constraints Key constraints, non-null constraints, unique constraints, etc."/> ;", you can display all the constraints using the data table name, including primary key constraints, foreign key constraints Key constraints, non-null constraints, unique constraints, etc.">

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

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

青灯夜游
青灯夜游Original
2020-10-16 10:38:525697browse

Mysql method to query the foreign key constraints of a table: use the "SHOW CREATE TABLE" statement, the syntax format "SHOW CREATE TABLE 0235737cc25ee9f60263d40a983de714;", 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 <数据表名>;

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)

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!

Statement:
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