MYSQL 级联 添加外键
MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引。在创建索引的时候,可以指定在删除、更新父表时,对子表进行的相应操作,包括RESTRICT、NOACTION、SET NULL和CASCADE。其中RESTRICT和NO ACTION相同,是指在子表有关联记录的情况下父表不能更新;CASCADE表示父表在更新或者删除时,更新或者删除子表对应记录;SET NULL则是表示父表在更新或者删除的时候,子表的对应字段被SET NULL。下面以一个新闻表说明,该新闻数据库的结构如下:

create database yynews;
use yynews;
#新闻类别表
create table categories(
catId int AUTO_INCREMENT primary key,
catName varchar(40) not null unique
)charset utf8;
#新闻表:
create table news(
newsId int AUTO_INCREMENT primary key,
title varchar(100) not null unique,
content text not null,
createTime timestamp not null,
catId int
)charset utf8;
#添加外键的引用
alter table news add constraint foreign key(catid) references categories(catid);
#评论表:
create table comments(
commId int AUTO_INCREMENT primary key,
content text not null,
createTime timestamp not null,
newsId int not null,
userIP char(15) not null
)charset utf8;
#添加外键的引用
alter table comments add constraint foreign key(newsid) references news(newsid);
#插入测试数据
insert into categories(catname) values("娱乐新闻");
insert into categories(catname) values("国际新闻");
insert into news(title,content,createTime,catId) values('test1','test1',now(),1);
insert into news(title,content,createTime,catId) values('test2','test2',now(),2);
insert into news(title,content,createTime,catId) values('test3','test3',now(),1);
insert into comments(content,createTime,newsId,userIP) values('you',now(),1,'127.0.0.1');
insert into comments(content,createTime,newsId,userIP) values('you',now(),2,'127.0.0.1');
insert into comments(content,createTime,newsId,userIP) values('you',now(),3,'127.0.0.1');
insert into comments(content,createTime,newsId,userIP) values('you',now(),1,'127.0.0.1');
如下:
mysql> select * from categories;
+-------+--------------+
| catId | catName |
+-------+--------------+
| 2 | 国际新闻 |
| 1 | 娱乐新闻 |
+-------+--------------+
2 rows in set (0.00 sec)
mysql> select * from news;
+--------+-------+---------+---------------------+-------+
| newsId | title | content | createTime | catId |
+--------+-------+---------+---------------------+-------+
| 1 | test1 | test1 | 2015-05-19 15:22:53 | 1 |
| 2 | test2 | test2 | 2015-05-19 15:22:53 | 2 |
| 3 | test3 | test3 | 2015-05-19 15:22:53 | 1 |
+--------+-------+---------+---------------------+-------+
3 rows in set (0.00 sec)
mysql> select * from comments;
+--------+---------+---------------------+--------+-----------+
| commId | content | createTime | newsId | userIP |
+--------+---------+---------------------+--------+-----------+
| 1 | you | 2015-05-19 15:22:53 | 1 | 127.0.0.1 |
| 2 | you | 2015-05-19 15:22:53 | 2 | 127.0.0.1 |
| 3 | you | 2015-05-19 15:22:53 | 3 | 127.0.0.1 |
| 4 | you | 2015-05-19 15:22:54 | 1 | 127.0.0.1 |
+--------+---------+---------------------+--------+-----------+
4 rows in set (0.00 sec)
在还没有添加任何的级联操作的时,删除有关联的数据会报错。
mysql> delete from categories where catid=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`yynews`.
`comments`, CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`newsId`) REFERENCES `news` (`newsId`))
数据库报错告诉你有个外键阻止了你的操作。所以我们可以添加级联操作。也可以再创建数据库的时候就指定级联操作
如下:
#级联操作
alter table news add constraint foreign key(catid) references categories(catid) on delete cascade
on update cascade;
alter table comments add constraint foreign key(newsid) references news(newsid) on delete cascade
on update cascade;
#上面这句的这两个语句就是在添加外键的时候为该表和表之间添加级联操作,即,数据表在删除或更新数据表时,相
关连的表也会同时更新或删除。
例如:
mysql> delete from categories where catid=1;
Query OK, 1 row affected (0.03 sec)
我们删除了类别catid为1的数据即:娱乐新闻,那么有关娱乐新闻的news中的数据double将会被删除,新闻被删除的同时,新闻下的评论也会被同时删除。
如下所示:
mysql> select * from news;
+--------+-------+---------+---------------------+-------+
| newsId | title | content | createTime | catId |
+--------+-------+---------+---------------------+-------+
| 2 | test2 | test2 | 2015-05-19 15:17:03 | 2 |
+--------+-------+---------+---------------------+-------+
1 row in set (0.00 sec)
mysql> select * from comments;
+--------+---------+---------------------+--------+-----------+
| commId | content | createTime | newsId | userIP |
+--------+---------+---------------------+--------+-----------+
| 2 | you | 2015-05-19 15:17:03 | 2 | 127.0.0.1 |
+--------+---------+---------------------+--------+-----------+
1 row in set (0.00 sec)
---------------------
MYSQL 级联 添加外键的更多相关文章
- mysql处理添加外键时 error 150 问题
当你试图在mysql中创建一个外键的时候,这个出错会经常发生,这是非常令人沮丧的.像这种不能创建一个.frm 文件的报错好像暗示着操作系统的文件的权限错误或者其它原因,但实际上,这些都不是的,事实上, ...
- Mysql如何添加外键,如何实现连表查询
创建表student和表score,表student设置主键,表score设置表student中属性相同的为外键: 创建student表 create table student ( id int p ...
- MySQL的常用命令:添加外键,修改字段名称,增加字段 设置主键自增长等
Mysql命令添加外键 前提是有这么几个表 以mall_product 和 mall_category为例 ALTER TABLE mall_product ADD CONSTRAINT fore_ ...
- [原创]MYSQL中利用外键实现级联删除和更新
MySQL中利用外键实现级联删除.更新 MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定 ...
- Mysql添加外键约束
简单说一下使用外键的好处 1.完整性约束 比如:用户表中有字段 用户编号(id) , 名称(username)设备表中有字段 设备编号(id) , 设备名称(devicename) 设备属于的用户编号 ...
- MySQL添加外键的方法
为book表添加外键: <1>明确指定外键的名称: 语法:alter table 表名 add constraint 外键的名称 foreign key(你的外键字段名) REFERENC ...
- mysql添加外键无法成功的原因
最近很忙,碰到很多问题都忘了发上来做个记录,现在又忘了,FUCK,现在碰到一个问题, 就是mysql添加外键总是无法成功,我什么都试了,就是没注意signed和unsigned,FUCK,因为我用my ...
- mysql 添加外键详解
为已经添加好的数据表添加外键: 语法:alter table 表名 add constraint FK_ID foreign key(你的外键字段名) REFERENCES 外表表名(对应的表的主键字 ...
- MySQL中MyISAM与InnoDB区别及选择,mysql添加外键
InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...
随机推荐
- Java 基础总结(一)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. String,StringBuffer,StringBuilder 1) ...
- Javascript Array对象 sort()方法,记忆方法,方法扩展
相信 有很多 同仁们,尤其是初学者,在记住 Array对象 sort() 方法的排序,规则上,有点困难: 其实sort()方法已经在实际工作中用到很多遍了,可当我仔细推敲,这个sort()方法,什么时 ...
- Multiple actions were found that match the request in Web Api
https://stackoverflow.com/questions/14534167/multiple-actions-were-found-that-match-the-request-in-w ...
- 利用正则提取discuz的正文内容
源正文: [p=24, null, left][color=#000][font=宋体]近日,香港著名漫画家马荣成在香港举办的"[color=#ff660][url=http://cul.c ...
- .NET中如何测试Private和Protected方法
TDD是1)写测试2)写通过这些测试的代码,3)然后重构的实践.在,NET社区中, 这个概念逐渐变得非常流行,这归功于它所增加的质量保证.此时,它很容易测试public方法,但是一个普遍的问题出现了, ...
- JSP数据交互(一)
1.JSP内置对象 请求对象:request 输出对象:out 响应对象:response 应用程序对象:application 会话对象:session 页面上下文对象:pageContext 页面 ...
- C++ 进阶学习 ——模板
模板和重载类似,比重载更省事 通常有两种形式:函数模板和类模板: 函数模板针对仅参数类型不同的函数: 类模板针对仅数据成员和成员函数类型不同的类. 一个简单的函数模板 template <cla ...
- note——《Tableau商业分析一点通》
为了更好地发掘各领域的数据价值,且能对数据进行精确分析及可视化,掌握资料的脉动,做出正确的决策 人们需要一种工具:能够快速灵活地连接和整合数据,提供简单的方式实现从不同的角度去观察研究数据,计算和展示 ...
- 智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y
智课雅思词汇---二十五.形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y 一.总结 一句话总结: 1.形容词后缀-ate(determinate)? determi ...
- linux配置静态ip,关闭防火墙
在vmware下安装centos6.5通过桥接方式访问外网,因此需要配置ip. 一.ip配置 1.1. 配置动态ip vi /etc/sysconfig/network-scripts/ifcfg-e ...