数据库复习

数据库复习笔记

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

posted @ 2024-04-23 08:05  xde_yt  阅读(7)  评论(0编辑  收藏  举报