information_schema.referential_constraints 学习


information_schema.referential_constraints 表用于查看外键约束

 

1、information_schema.referential_constraints表的常用列:

  1、constraint_schema                :约束(外键)所在的库名

  2、constraint_name                  :约束名(外键名)

  3、unique_constraint_schema            :被引用约束所在库名

  4、unique_constraint_name              :被引用约束名

 

2、例子:

  1、定义一对有外键约束的表:

复制代码
create table teacher(
id int not null auto_increment,
name varchar(4),
constraint pk__teacher primary key(id),
constraint ix__teacher__name unique index (name))
engine=innodb default char set utf8;



create table student(
id int not null auto_increment,
name varchar(4),
teacher_id int not null,
teacher_name varchar(4),
constraint pk__student primary key(id),
constraint fk__student__id foreign key (teacher_id ) references teacher(id),
constraint fk__student__teacher_name foreign key (teacher_name) references teacher(name) on delete no action on update cascade)
engine=innodb default char set utf8;
复制代码

  2、查看外键约束

复制代码
select * from information_schema.referential_constraints \G
*************************** 1. row ***************************
       CONSTRAINT_CATALOG: def
        CONSTRAINT_SCHEMA: tempdb
          CONSTRAINT_NAME: fk__student__id
UNIQUE_CONSTRAINT_CATALOG: def
 UNIQUE_CONSTRAINT_SCHEMA: tempdb
   UNIQUE_CONSTRAINT_NAME: PRIMARY
             MATCH_OPTION: NONE
              UPDATE_RULE: RESTRICT
              DELETE_RULE: RESTRICT
               TABLE_NAME: student
    REFERENCED_TABLE_NAME: teacher
*************************** 2. row ***************************
       CONSTRAINT_CATALOG: def
        CONSTRAINT_SCHEMA: tempdb
          CONSTRAINT_NAME: fk__student__teacher_name
UNIQUE_CONSTRAINT_CATALOG: def
 UNIQUE_CONSTRAINT_SCHEMA: tempdb
   UNIQUE_CONSTRAINT_NAME: ix__teacher__name
             MATCH_OPTION: NONE
              UPDATE_RULE: CASCADE
              DELETE_RULE: NO ACTION
               TABLE_NAME: student
    REFERENCED_TABLE_NAME: teacher



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值