为了账号安全,请及时绑定邮箱和手机立即绑定

数据库怎么通过写代码级联删除,代码尽量简单?

数据库怎么通过写代码级联删除,代码尽量简单?

潇潇雨雨 2018-12-06 14:40:28
create table student(sno char(9) primary key,sname varchar(20),ssex char(2),sage smallint,sdept varchar(20)); create table course(cno char(4) primary key,cname varchar(40),cpno char(4),ccredit smallint,foreign key(cpno) references course(cno));   create table sc(sno char(9),cno char(4),grade smallint,primary key(sno,cno),foreign key(sno) references student(sno), foreign key(cno) references course(cno));   insert into student values('200215121','李勇','男',20,'CS');insert into student values('200215122','刘晨','女',19,'CS');insert into student values('200215123','王敏','女',18,'MA');insert into student values('200215124','张立','男',19,'IS'); insert into COURSE values('0002','数学',NULL,2);insert into COURSE values('0006','数据处理',NULL,2);insert into COURSE values('0007','PASCAL语言','0006',4);insert into COURSE values('0005','数据结构','0007',4);insert into COURSE values('0001','数据库','0005',4);insert into COURSE values('0003','信息系统','0001',4);insert into COURSE values('0004','操作系统','0006',3); insert into sc values('200215121','0001',92);insert into sc values('200215121','0002',85);insert into sc values('200215121','0003',88);insert into sc values('200215122','0002',90);insert into sc values('200215122','0003',80);insert into sc values('200215123','0001',93);insert into sc values('200215124','0001',90);   然后我要删除6号课程记录会报错怎么删除求大神指导
查看完整描述

8 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

---- 1、删除

USE testdb2
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[student]') AND type IN (N'U')) DROP TABLE [student];

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[course]') AND type IN (N'U')) DROP TABLE [course];

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[sc]') AND type IN (N'U')) DROP TABLE [sc];



create table student(sno char(9) primary key,sname varchar(20),ssex char(2),sage smallint,sdept varchar(20));

create table course(cno char(4) primary key,cname varchar(40),cpno char(4),ccredit smallint);

create table sc(sno char(9),cno char(4),grade smallint,
primary key(sno,cno),foreign key(sno) references student(sno),
foreign key(cno) references course(cno) on delete cascade );


 

insert into student values('200215121','李勇','',20,'CS');
insert into student values('200215122','刘晨','',19,'CS');
insert into student values('200215123','王敏','',18,'MA');
insert into student values('200215124','张立','',19,'IS');

insert into COURSE values('0002','数学',NULL,2);
insert into COURSE values('0006','数据处理',NULL,2);
insert into COURSE values('0007','PASCAL语言','0006',4);
insert into COURSE values('0005','数据结构','0007',4);
insert into COURSE values('0001','数据库','0005',4);
insert into COURSE values('0003','信息系统','0001',4);
insert into COURSE values('0004','操作系统','0006',3);

insert into sc values('200215121','0001',92);
insert into sc values('200215121','0002',85);
insert into sc values('200215121','0003',88);
insert into sc values('200215122','0002',90);
insert into sc values('200215122','0003',80);
insert into sc values('200215123','0001',93);
insert into sc values('200215124','0006',90);


DELETE FROM course WHERE cno = '0006' ;

select * from course c 
select * from student s 
select * from sc
查看完整回答
反对 回复 2019-01-07
?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

你要先删外建表的记录,如sc 、student表对应的外建记录

查看完整回答
反对 回复 2019-01-07
?
HUWWW

TA贡献1874条经验 获得超12个赞

触发器试试

查看完整回答
反对 回复 2019-01-07
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

你在外键加上 on delete cascade

查看完整回答
反对 回复 2019-01-07
?
冉冉说

TA贡献1877条经验 获得超1个赞

具体在哪里,说清楚一点谢谢,本人刚学

查看完整回答
反对 回复 2019-01-07
?
慕标5832272

TA贡献1966条经验 获得超4个赞

@米老鬼: 

查看完整回答
反对 回复 2019-01-07
?
森林海

TA贡献2011条经验 获得超2个赞

@刘宏玺: 我用的不是这个软件,是Microsoft sql好像只能写代码吧,能不能说一下怎么通过写代码实现级联删除还有写在什么地方

查看完整回答
反对 回复 2019-01-07
?
慕容3067478

TA贡献1773条经验 获得超3个赞

@米老鬼: …………

查看完整回答
反对 回复 2019-01-07
  • 8 回答
  • 0 关注
  • 671 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信