zoukankan      html  css  js  c++  java
  • 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)
    ---------------------

  • 相关阅读:
    3.17JSP作业
    JSP第二次作业
    JSP第一次作业
    软件测试课堂练习
    增删改查
    登录界面
    购物商城
    图床
    JSP-2020年4月14日-第七周
    JSP-2020年4月12日-第六周
  • 原文地址:https://www.cnblogs.com/wangjintao-0623/p/10083935.html
Copyright © 2011-2022 走看看