展会信息港展会大全

mysql的级联操作 创建表 插入数据 删除数据
来源:互联网   发布日期:2016-01-27 15:46:03   浏览:2348次  

导读:1 创建表a create table a( name char(20) not null, id char(20) not null primary key); 2 创建表b create table b( b_name char(20) not null, b_id char(20) not nu ...

1.创建表a

create table a(

name char(20) not null,

id char(20) not null primary key);

2.创建表b

create table b(

b_name char(20) not null,

b_id char(20) not null ,

constraint foreign key(b_id) references a(id) on delete cascade);

3.表建好后,插入数据

insert into a values("111","1");

insert into a values("222","2");

在b表中插入下面数据 insert into b values("1","1"); 显示插入成功。

在b表中插入下面数据 insert into b values("1","3"); 显示插入失败。是因为这里设置了外键而a表中的id没(id=="3")的数据。

执行delete from a where id="1"; b表中的数据也会被删除掉。这里就应用到了级联删除。

赞助本站

人工智能实验室

相关热词: 级联 mysql

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港