MySQL 主外键约束与标准SQL不同的地方

本文介绍标准SQL中外键约束的应用,包括子表引用父表的主键和唯一索引,以及InnoDB在此基础上所做的扩展,允许子表引用父表上的任何索引列。

标准SQL的外键约束条件

  1): 子表引用父表的主键

drop table if exists child,parent;

create table if not exists  parent(
    id int not null auto_increment primary key,
    v int
    );

create table if not exists child(
    id int not null auto_increment primary key,
    parent_id int not null,
    v int,
    constraint fk__child__parent_id foreign key (parent_id) references parent(id)                                                
    );

insert into parent(id,v) values(1,100);
insert into child(parent_id,v) values(1,1000);
insert into child(parent_id,v) values(1,1000);

select * from parent;
+----+------+
| id | v    |
+----+------+
|  1 |  100 |
+----+------+

select * from child;
+----+-----------+------+
| id | parent_id | v    |
+----+-----------+------+
|  1 |         1 | 1000 |
|  2 |         1 | 1000 |
+----+-----------+------+

 

  2): 子表引用交表的唯一索引

create table if not exists  parent(
    id int not null,
    v int,
    constraint unique index uix__parent_id (id)
    );

create table if not exists child(
    id int not null auto_increment primary key,
    parent_id int not null,
    v int,
    constraint fk__child__parent_id foreign key (parent_id) references parent(id)                                                
    );

insert into parent(id,v) values(1,100);
insert into child(parent_id,v) values(1,1000);
insert into child(parent_id,v) values(1,1000);

select * from parent;
+----+------+
| id | v    |
+----+------+
|  1 |  100 |
+----+------+


select * from child;
+----+-----------+------+
| id | parent_id | v    |
+----+-----------+------+
|  1 |         1 | 1000 |
|  2 |         1 | 1000 |
+----+-----------+------+

 

innodb在标准SQL上做的扩展

  1): 只要在父表上有在对应的列上建索引,那么这个列就能在子表中引用

create table if not exists  parent(
    id int not null auto_increment primary key,
    v int,
    index uix__parent_v (v) -- 只要父表上有索引就行
    );

create table if not exists child(
    id int not null auto_increment primary key,
    parent_v int not null,
    v int,
    constraint fk__child__parent_v foreign key (parent_v) references parent(v) -- 在子表中引用                                               
    );

insert into parent(id,v) values(1,100);
insert into parent(id,v) values(2,100);

insert into child(parent_v,v) values(100,2000);
insert into child(parent_v,v) values(100,2000);

select * from parent;
+----+------+
| id | v    |
+----+------+
|  1 |  100 |
|  2 |  100 |
+----+------+

select * from child;
+----+----------+------+
| id | parent_v | v    |
+----+----------+------+
|  1 |      100 | 2000 |
|  2 |      100 | 2000 |
+----+----------+------+

 

 

我的评介

  主外键约束在标准SQL下体现的是一种一对多的关系,但是经过MySQL的拓展之后可以表现出“多对多”的关系;虽然MySQL这样

  的设计有一定的灵活性,个人觉得最好还是使用标准SQL的方式。

 

学习交流

 

 -----------------------------http://www.sqlpy.com-------------------------------------------------

 -----------------------------http://www.sqlpy.com-------------------------------------------------

 

转载于:https://www.cnblogs.com/JiangLe/p/9594336.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值