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 把数据和索引存放在表空间 ...
随机推荐
- Mybatis之基础应用小结以及IntelliJ IDEA目录结构的一些小问题
IntelliJ IDEA 目录结构的一些小问题 [Mybatis 之基础应用小结] 1.不管怎么样,先建立一个简单的MySQL数据表,如下所示 2.接下来要做的事情就是通过Mybatis对数据表进行 ...
- ASP.NET MVC CheckBoxFor的int to bool
当我们使用CheckBoxFor类型需要使用bool ,可以将 int转换成bool <div class="form-group"> <label class= ...
- Go语言学习之运算符(The way to go)
生命不止,继续go go go 今天介绍go中的运算符. 运算符大概分为: Arithmetic Operators Relational Operators Logical Operators Bi ...
- Android Studio 入门级教程
引用原文:http://www.cnblogs.com/abao0/p/6934023.html 写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权.希望尽自己的努力 ...
- Windows 修改个性化时间显示
A goal is a dream with a deadline. Much effort, much prosperity. 我感觉我的时间显示不够人性化.不够个性化 修改注册表 我的系统为Win ...
- IOS 出现错误 :Reason: image not found
把Build Phases 里HyphenateLite.framework后边的选项修改成为Optional就可以了 dyld 后面有提示 HyphenateLite.framework
- [USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目描述 Every year in Wisconsin the cows celebrate the USA autumn holiday of Halloween by dressing up i ...
- ubuntu 16.04 vscode + php debug
1.vscode 安装PHP Debug扩展: 2.php环境配置: 1.安装xdebug扩展: sudo apt-get install php-xdebug 2.找到扩展的路径: chq@chq- ...
- IT行业的一些专业术语
SDK:SDK(Software Development Kit, 即软件开发工具包 )一般是一些被软件工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合. 参考:h ...
- 使用ConcurrentLinkedQueue惨痛的教训
服务端原本有个定时任务对一个集合ArrayList 中的消息做处理. 因为考虑到处理消息是先进先出原则,所以优化的时候考虑改用ConcurrentLinkedQueue 当时没仔细深入研究过这个集合就 ...