mysql 外键(foreign key)的详解和实例

1、表引擎必须为InnoDB,MyISAM不支持

2、外键必须建立索引(可以为普通、主键、唯一,事先不建立的话会自动创建一个普通索引),你要用的外键和参照的外表的键,即

alter table B add constraint `b_foreign_key_name` foreign key (`bfk`) 

references A(`afk`) on delete no action on update no action;

时 b_foreign_key_name 为外键名,bfk字段和afk字段都必须存在索引


3、外表为约束表,约束着含有外键的被约束表,即 B 含有一个以 A 作为参考表的外键,则 A 为主 B 为从,弱关联on delete on update等动作,则 A 变更 B 会被变更,B 怎样变 A 不必跟随变动,且表 A 中必须事先存在 B 要插入的数据外键列的值,列如 B.bfk作为外键 参照 A.afk ,则 B.bfk插入的值必须是 A.afk 中已存在的


4、把3所的简单点就是若B有以A作为参照的外键,则B中的此字段的取值只能是A中存在的值,从表B会实时受到主表A的约束,同时若关联on delete on update等操作则当A中的被参照的字段发生delete或update时,B中的对应的记录也会发生delete 或 update操作,完整性。
                                                                                                                                                                            
下面我们以一个简单的学生信息管理系统的数据表做为实例:                                                                                                                                                                  

先把表和索引加好


//学生表 cid作为外键关联班级表 pid作为 档案表外键的关联 所以这俩货都得有索引

create table my_student(
`id` int unsigned not null auto_increment primary key,
`name` varchar(25) not null comment 'student name',
`pid` int unsigned not null comment 'student profile id',
`cid` int unsigned not null comment 'student class id',
key `cid`(`cid`),
key `pid`(`pid`)

)engine=InnoDB default charset=utf8 auto_increment=1;


//班级表 id作为 学生表外键的关联 已为主键索引

create table my_class(
`id` int unsigned not null auto_increment primary key,
`cname` varchar(25) not null comment 'class name',
`info` tinytext not null default ''

)engine=InnoDB default charset=utf8 auto_increment=1;


//档案表 id作为外键 关联 学生表 已为主键索引
create table my_profile(
`id` int unsigned not null auto_increment primary key,
`pname` varchar(25) not null comment 'profile name',
`info` tinytext not null default '' comment 'student info',
)engine=InnoDB default charset=utf8 auto_increment=1;    
                                                                                                                                                                      

这里我们将my_student作为my_profile的外表,即约束表,即my_profile以自身id作为 外键 关联 以 my_student 的pid字段作为参照,关联delete联动操作,update不做任何操作,如下


alter table my_profile add constraint profile_student foreign key (`id`) references my_student(`pid`) on delete cascade on update no action;


这里我们将my_class作为my_student的外表,即约束表,即my_student以自身cid作为 外键 关联 以 my_class 的id字段作为参照,关联update联动操作,delete不做任何操作,如下


alter table my_student add constraint student_class foreign key (`cid`) references my_class(`id`) on update cascade on delete no action;   

                                                                                                                                                         

则当我删除my_student中 id=1 的学生时,其会将my_profile中id为此学生pid的记录删掉


//删除id为1的学生记录,因档案表以学生表作为外表约束,且关联 on delete cascade操作

delete from my_student where id = 1;

这是外键机制自身执行的处理动作

delete from my_profile where id = (select pid from my_student where id = 1);

这是外键机制自身执行的处理动作


则当我更新my_class中 id=1 的班级为5时,其会将my_student中cid=1的学生更新为cid=5


//更新联动
update my_class set id = 5 where id = 1;

这是外键机制自身执行的处理动作

update my_student set cid = 5 where cid = 1;
这是外键机制自身执行的处理动作


注:外键的表与表之间耦合度高,在大型web应用时会导致性能降低,除非是关键性业务逻辑,否则不建议过多使用外键
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值