删除选课成绩在55分以下的学生记录; (4分)
精确答案:
DELETE FROM Student WHERE SNO IN
(SELECT SNO FROM SC WHERE GRADE<55);
或
DELETE
FROM Student
WHERE Student.SNO = SC.SNO AND SC.GRADE<55 ;
把“高等数学”课程成绩提高8%; (3分)
精确答案:
UPDATE SC SET Grade=Grade*1.08
WHERE Cno IN
(SELECT Cno
FROM Course
WHERE Cname=‘高等数学’);
6)求出女同学的每一年岁组(超过3人)有多少人?要求查询结果按人数降序排列,人数相同的按年岁升序排列; (5分)
精确答案:
SELECT Sage, COUNT(Sno)
FROM Student
WHERE Ssex=‘女’
GROUP BY Sage HAVING COUNT(Sno)>3
ORDER BY 2 DESC, Sage ASC //其中ASC也可以省略不写
7)定义视图S_age,其中包括学生学号以及每个学生选修课程的门数(要求成绩非空)和均匀成绩; (5分)
精确答案:
CREATE VIEW S_age (Sno, Cno_num, Avg_grade)
AS SELECT Sno,COUNT(Cno),AVG(grade)
FROM SC
WHERE grade IS NOT NULL
GROUP BY Sno
8)在Course表中增长一个类型为char(10)的职称(pro)列; (2分)
精确答案:
alter table Course
add pro char(10)
(2)用SQL语句实现下列问题
1)写出创建S表的T-SQL语句 ; (5分)
精确答案:
CREATE TABLE S
( Sno CHAR(8) PRIMARY KEY,
Sname CHAR(12) NOT NULL UNIQUE,
Ssex CHAR(2) CHECK(Ssex in(‘男’,‘女’)) DEFAULT ‘女’,
Sage INT CHECK(STage>=16 AND STage<=40) DEFAULT 21,
Sdept CHAR(20) DEFAULT ‘电子系’);
检索姓"李"且倒数第2个字为"华"或"涛"字的学生的姓名和学号; (4分)
精确答案:
SELECT Sname,Sno
FROM Student
WHERE Sname LIKE ‘李% [华涛] _ _’;
3)向学生选修课程表中插入元组“学生S6选修课程号C2”。 (2分)
精确答案:
INSERT
INTO SC (Sno, Cno)
VALUES( ‘S6’,’ C2’ )
删除选课成绩在60分以下的学生记录; (4分)
精确答案:
DELETE
FROM Student
WHERE SNO IN
(SELECT SNO
FROM SC
WHERE GRADE<60);
或
DELETE
FROM Student
WHERE Student.SNO = SC.SNO AND SC.GRADE<60 ;
把“大学英语”课程成绩提高5%; (3分)
精确答案:
UPDATE SC
SET Grade=Grade*1.05
WHERE Cno IN
(SELECT Cno
FROM Course
WHERE Cname=‘大学英语’)
6)求出男同学的每一年岁组(超过4人)有多少人?要求查询结果按人数降序排列,人数相同的按年岁升序排列; (5分)
精确答案:
SELECT Sage, COUNT(Sno)
FROM Student
WHERE Ssex=‘男’
GROUP BY Sage HAVING COUNT(Sno)>4
ORDER BY 2 DESC, Sage ASC //其中ASC也可以省略不写
7)定义视图S_age,其中包括学生学号以及每个学生选修课程的门数(要求成绩非空)和均匀成绩; (5分)
精确答案:
CREATE VIEW S_age (Sno, Cno_num, Avg_grade)
AS SELECT Sno,COUNT(Cno),AVG(grade)
FROM SC
WHERE grade IS NOT NULL
GROUP BY Sno
8)在S表中删除院系字段列。 (2分)
精确答案:
alter table student
drop column Sdept
15、用如下的SQL语句创建一个Student表
CREATE TABLE Student ( SNO Char(4) NOT NULL,
NAME Char(8) NOT NULL,
SEX Char(2), AGE INT )
可以插入到Student表中的元组是_________。
A、(‘0731’,‘李大明’,男,23)
B、(‘0731’,‘李大明’,23,男)
C、( NULL,‘李大明’,‘男’,‘23’)
D、(‘0731’,NULL,‘男’,23)
精确答案:A
④删除选课成绩在45分以下的学生记录。 (4分)
精确答案:
DELETE
FROM Student
WHERE SNO IN
(SELECT SNO
FROM SC
WHERE GRADE<45);
⑤把“网络工程”课程成绩提高8%。 (4分)
精确答案:
UPDATE SC
SET Grade=Grade*1.08
WHERE Cno IN
(SELECT Cno
FROM Course
WHERE Cname=‘网络工程’);
⑥求出女同学的每一年岁组(超过4人)有多少人?要求查询结果按人数升序排列,人数相同的按年岁降序排列。 (6分)
精确答案:
SELECT Sage, COUNT(Sno)
FROM Student
WHERE Ssex=‘女’
GROUP BY Sage HAVING COUNT(Sno)>4
⑦定义视图S_AVG,其中包括学生学号以及每个学生选修课程的门数(要求成绩非空)和均匀成绩。 (5分)
精确答案:
CREATE VIEW S_age (Sno, Cno_num, Avg_grade)
AS SELECT Sno,COUNT(Cno),AVG(grade)
FROM SC
WHERE grade IS NOT NULL
GROUP BY Sno