在 ​​MySQL​​ 中可以使用 SHOW CREATE TABLE 语句来查看表中的约束。

查看数据表中的约束语法格式如下:

SHOW CREATE TABLE <表名>;

例 1

创建数据表 tb_emp8 并指定 id 为主键约束,name 为唯一约束,deptId 为非空约束和外键约束,salary检查约束>0, 然后查看表中的约束,SQL 语句运行结果如下。

mysql> create table if not exists tb_emp8
-> (
-> id int(11) primary key auto_increment,
-> name varchar(25) unique key,
-> deptId int(11) not null,
-> salary float,
-> constraint chk_emp8_salary check(salary > 0),
-> constraint fk_emp8_deptId foreign key (deptId) references tb_dept1 (id)
-> );
Query OK, 0 rows affected, 2 warnings (0.53

desc查看tb_emp8, 如下:

MySQL教程51-MySQL查看表中的约束_外键约束

show create table tb_emp8, 如下:

MySQL教程51-MySQL查看表中的约束_主键_02