【MySQL】資料庫原理複習——SQL語言
對基本的SQL語句編寫的練習題,其中的題目的答案可能會有多種書寫方式。
1、題目1
1.1 關係模式
學生student:SNO:學號,SNAME:姓名,AGE:年齡 SEX:性別
SNO |
SNAME |
AGE |
SEX |
1 |
23 |
男 |
|
2 |
22 |
女 |
|
5 |
22 |
男 |
課程course:CNO:課程程式碼,CNAME:課程名稱,TEACHER:教師
CNO |
CNAME |
TEACHER |
K1 |
C語言 |
|
K5 |
資料庫原理 |
程軍 |
K8 |
編譯原理 |
程軍 |
學生成績SC:SNO:學號,CNO:課程程式碼,SCORE:成績
SNO |
CNO |
SCORE |
1 |
K1 |
83 |
2 |
K1 |
85 |
5 |
K1 |
92 |
2 |
K5 |
90 |
5 |
K5 |
84 |
5 |
K8 |
80 |
1.2 要求一
用SQL語言完成表的建立以及資料的插入:
- use test;
- create table if not exists student
- (
- SNO varchar(20) primary key,
- SNAME varchar(20) character set gbk,
- AGE int,
- SEX char(2) character set gbk CHECK(SEX IN('男','女'))
- );
- insert into student values('1','李強',23,'男');
- insert into student values('2','劉麗',22,'女');
- insert into student values('5','張友',22,'男');
- create table if not exists course
- (
- CNO varchar(20) primary key,
- CNAME varchar(20) character set gbk,
- TEACHER varchar(20) character set gbk
- );
- insert into course values('K1','C語言','王華');
- insert into course values('K5','資料庫原理','程軍');
- insert into course values('K8','編譯原理','程軍');
- create table if not exists sc
- (
- SNO varchar(20) NOT NULL,
- CNO varchar(20) NOT NULL,
- SCORE int NOT NULL,
- primary key (SNO,CNO),
- foreign key (SNO) references student(SNO),
- foreign key (CNO) references course(CNO)
- );
- insert into sc values('1','K1',83);
- insert into sc values('2','K1',85);
- insert into sc values('5','K1',92);
- insert into sc values('2','K5',90);
- insert into sc values('5','K5',84);
- insert into sc values('5','K8',80);
1.3 要求二
用SQL語言完成如下要求:
(1) 檢索至少選修"程軍"老師所授全部課程的學生姓名(SNAME);
本題的另外一種說法就是,檢索選修了“程軍”老師所授全部課程的學生姓名
- select sname from student
- where not exists
- (
- select * from course
- where teacher='程軍' and not exists
- (
- select * from sc
- where sc.sno=student.sno and sc.cno=course.cno
- )
- );
這裡的檢索結果是:
+-------+
| sname |
+-------+
| 張友 |
+-------+
1 row in set (0.00 sec)
(2) 檢索"李強"同學不學課程的課程號(CNO);
- select course.cno from course
- where course.cno not in
- (
- select sc.cno from sc,student
- where student.sname='李強' and student.sno=sc.sno
- );
+-----+
| cno |
+-----+
| K5 |
| K8 |
+-----+
2 rows in set (0.22 sec)
(3) 檢索選修不少於3門課程的學生學號(SNO);
- SELECT sc.sno
- FROM sc
- GROUP BY sc.sno HAVING count(*)>=3
- ;
+-----+
| sno |
+-----+
| 5 |
+-----+
1 row in set (0.09 sec)
(4) 檢索選修全部課程的學生姓名(SNAME)。
- SELECT sname FROM student
- WHERE NOT EXISTS
- (
- SELECT * FROM course
- WHERE NOT EXISTS
- (
- SELECT * FROM sc
- WHERE course.cno=sc.cno and student.sno=sc.sno
- )
- );
+-------+
| sname |
+-------+
| 張友 |
+-------+
1 row in set (0.02 sec)
(5) 檢索不學"C語言"的學生資訊
- SELECT * FROM student
- WHERE sno not in
- (
- SELECT sc.sno FROM sc,course
- WHERE course.cno=sc.cno
- );
1.4 要求三
請用SQL語言完成如下查詢:
(1)查詢“程軍”老師所教授的所有課程;
- SELECT * FROM course
- WHERE teacher='程軍';
- SELECT score FROM student,sc
- WHERE student.sname='李強' AND student.sno=sc.sno
(3)查詢課程名為“C語言”的平均成績;
- SELECT AVG(score) FROM sc,course
- WHERE course.cname='C語言' AND course.cno=sc.cno
(4)查詢選修了所有課程的同學資訊。
- SELECT * FROM student
- WHERE NOT EXISTS
- (
- SELECT * FROM course
- WHERE NOT EXISTS
- (
- SELECT * FROM sc
- WHERE course.cno=sc.cno AND student.sno=sc.sno
- )
- );
1.5 要求四
(1)檢索王老師所授課程的課程號和課程名。
- SELECT cno,cname FROM course WHERE teacher LIKE '王%';
(2)檢索年齡大於23歲的男學生的學號和姓名。
- SELECT sno,sname FROM student
- WHERE age>23;
(3)檢索至少選修王老師所授課程中一門課程的女學生姓名。
- SELECT sname FROM student
- WHERE sex='女' AND sno IN
- (
- SELECT distinct sno FROM sc,course
- WHERE teacher LIKE '王%' AND sc.cno=course.cno
- );
- SELECT course.cno FROM course
- WHERE course.cno NOT IN
- (
- SELECT sc.cno FROM sc,student
- WHERE student.sname LIKE '李%' AND sc.sno=student.sno
- );
- SELECT sno FROM sc
- GROUP BY sno HAVING (count(*)>=2);
- SELECT course.cno,course.cname FROM course
- WHERE course.cno IN
- (
- SELECT cno FROM sc
- GROUP BY cno HAVing count(*)=(SELECT count(*) FROM student)
- );
- SELECT DISTINCT sno FROM sc
- WHERE cno IN
- (
- SELECT cno FROM course
- WHERE teacher LIKE '王%'
- );
- SELECT count(*) NumberOfCourse
- FROM
- (
- SELECT DISTINCT cno FROM sc
- GROUP BY cno
- ) AS testTab;
- SELECT AVG(age) AverageOfAge
- FROM student
- WHERE sno IN
- (
- SELECT sno FROM sc
- WHERE cno='K1'
- );
- SELECT sc.cno,AVG(sc.score)
- FROM sc
- WHERE sc.cno IN
- (
- SELECT course.cno FROM course
- WHERE course.teacher LIKE '王%'
- )
- GROUP BY sc.cno
- SELECT cno,Num
- FROM
- (
- SELECT cno,count(*) as Num
- FROM sc
- GROUP BY cno HAVING (count(*) >= 2)
- ) tb_temp
- ORDER BY Num DESC,cno ASC
- ;
- SELECT stu1.sname
- FROM student stu1,
- (
- SELECT max(sno) snoLi,min(age) ageLi FROM student
- WHERE sname LIKE '李%'
- ) AS stuLi
- WHERE (stu1.age < stuLi.ageLi) AND (stu1.sno>stuLi.snoLi)
- ;
- SELECT sname,age FROM student
- WHERE sname LIKE '李%'
- ;
- SELECT sno,cno FROM sc
- WHERE score IS NULL;
- SELECT sname,age FROM student
- WHERE
- sex='男' AND age > (
- SELECT AVG(age)
- FROM student
- WHERE sex='女'
- )
- ;
- SELECT sname,age
- FROM student
- WHERE sex='男' AND age > (
- SELECT max(age)
- FROM student
- WHERE sex='女'
- )
- ;