设为首页 加入收藏

TOP

mysql级联操作(实例)(一)
2015-07-24 11:53:29 来源: 作者: 【 】 浏览:4
Tags: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) refere
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySQL5.6安装&修改root密码&a.. 下一篇MySQL使用索引的场景及真正利用索..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)
·到底应该用MySQL还是 (2025-12-24 15:18:11)
·进入Linux世界大门的 (2025-12-24 14:51:47)
·Download Linux | Li (2025-12-24 14:51:44)