数据库复习
1|0数据库复习笔记
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、创建Student表”的SQL语句 ********** --
create table Student(
Sno char(10) primary key,
Sname varchar(20),
Ssex char(2),
Sage smallint,
Sdept varchar(20)
);
-- ********** 此处写“2、创建Course表”的SQL语句 ********** --
create table Course(
Cno char(10) primary key,
Cname varchar(20),
Cpno char(10),
Ccredit smallint
);
-- ********** 此处写“3、创建SC表”的SQL语句 ********** --
create table SC(
Sno char(10),
Cno char(10),
Grade smallint,
primary key(Sno,Cno)
);
GO
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、添加phone列”的SQL语句 ********** --
alter table Student add phone char(12);
-- ********** End ********** --
-- ********** 此处写“2、删除Cpno列”的SQL语句 ********** --
alter table Course drop column Cpno;
-- ********** 此处写“3、修改sdept列”的SQL语句 ********** --
alter table Student alter column sdept varchar(30);
GO
-- ********** 此处写“1、删除三张表”的SQL语句 ********** --
drop table if exists SC,Course,Student;
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、为Student表插入两行”的SQL语句 ********** --
insert into Student values('001','Smith','m','18','CS');
insert into Student values('002','Ketty','f','19','MA');
-- ********** 此处写“2、为Course表插入两行”的SQL语句 ********** --
insert into Course values('C01','DB',null,'2');
insert into Course values('C02','Oracle','C01','3');
-- ********** 此处写“3、为SC表插入3行”的SQL语句 ********** --
insert into SC values('001','C01','70');
insert into SC values('001','C02','82');
insert into SC values('002','C01','86');
-- ********** End ********** --
GO
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、将不及格的学生成绩加5分”的SQL语句 ********** --
update SC set grade+=5 where grade < 60;
-- ********** 此处写“2、将CS系男同学的年龄加1”的SQL语句 ********** --
update Student set sage+=1 where ssex = 'm' and sdept = 'CS' ;
-- ********** 此处写“3、将学生的学号前加上‘S’(其中S要大写)”的SQL语句 ********** --
update Student set sno = 'S' + sno;
-- ********** End ********** --
GO
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、将学生的学号前的‘S’删掉”的SQL语句 ********** --
update Student set sno = substring(sno,2,9);
-- ********** 此处写“2、在学生学号的后面加上‘S’”的SQL语句 ********** --
update Student set sno = substring(sno,1,4) + 'S';
-- ********** End ********** --
GO
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、在SC表中删除成绩为空的选课信息”的SQL语句 ********** --
delete from SC where grade is null;
-- ********** 此处写“2、删除年龄等于18岁的女同学”的SQL语句 ********** --
delete from Student where sage = '18' and ssex = 'f';
-- ********** 此处写“3、删除学分为3分的课程”的SQL语句 ********** --
delete from Course where ccredit = '3';
-- ********** End ********** --
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、查询CS系男同学的学号,姓名,年龄”的SQL语句 ********** --
select sno,sname,sage from Student where Sdept='CS' and Ssex = 'm';
-- ********** 此处写“2、查询不及格的学生选课信息,列出学号,课程号,成绩”的SQL语句 ********** --
select sno,cno,grade from SC where grade < 60;
-- ********** 此处写“3、查询先行课程不为空的课程(使用*表示查询结果)”的SQL语句 ********** --
select * from Course where cpno is not null;
-- ********** 此处写“4、查询姓名中带有'n'字母的学生的学号,姓名(使用like语句)”的SQL语句 ********** --
select sno,sname from Student where sname like '%n%';
-- ********** 此处写“5、使用distinct关键字查询学生表中不同的系,列出系(去除重复元祖)”的SQL语句 ********** --
select distinct sdept from Student;
-- ********** End ********** --
GO
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、查询90分以上学生的选课信息,列出学号,姓名,课程号,成绩”的SQL语句 ********** --
select A.sno,A.sname,C.cno,C.grade from Student A,SC C where grade > 90 and A.sno = C.sno;
-- ********** 此处写“2、查询‘DB’课程的选课情况,列出学号,成绩”的SQL语句 ********** --
select Sc.sno,SC.grade from Course,SC where Course.cname='DB' and Course.Cno = SC.Cno;
-- ********** End ********** --
GO
USE CS_yx_DB
GO
SET NOCOUNT ON
-- ********** Begin ********** --
-- ********** 此处写“1、查询CS系的学生选择‘DB’课程的情况,列出学号,成绩”的SQL语句 ********** --
select Student.sno,SC.grade from Student,Course,SC where cname='DB' and Student.sno = SC.sno and Course.cno = SC.cno and Sdept = 'CS';
-- ********** 此处写“2、查询女同学的选课情况,列出学号,课程号,课程名,成绩”的SQL语句 ********** --
select Student.sno,Course.cno,Course.cname,SC.grade from Student,Course,SC where ssex = 'f' and Student.sno = SC.sno and Course.cno = SC.cno and Student.sno = SC.sno;
-- ********** End ********** --
GO
__EOF__

本文作者:xued
本文链接:https://www.cnblogs.com/xdeyt/p/18152000.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/xdeyt/p/18152000.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧