# 例题练习

Create Table student(
Sno char(12) Primary Key,
Sname char(12),
Ssex char(2),
Sdept char(40),
Sage SmallInt
);
Insert Into Student Values ('20191102062','刘哲轩','男','Computer Science',19)
Insert Into Student Values ('20191102063','叶晓晓','男','information security',20)
Insert Into Student Values ('20191102064','刘小轩','女','Computer Science',21)
Insert Into Student Values ('20191102065','叶大大','男','information security',20)
Insert Into Student Values ('20191102066','刘大轩','男','Computer Science',22)
Insert Into Student Values ('20191102067','刘轩','女','information security',20)
Insert Into Student Values ('20191102068','刘车干','男','Computer Science',19)
Insert Into Student Values ('20191102069','刘刘','女','information security',20)
Insert Into Student Values ('20191102070','刘刘流','男','Computer Science',22)
Insert Into Student Values ('20191102071','叶小刘','女','information security',20)
Create Table Course(
Cno Char(12) Primary Key,--课序号
Cname Char(12),
Cpno Char(12),--先行课的课序号
Ccredit SmallInt,
Foreign Key(Cpno) References Course(Cno)
);
Insert Into Course Values('1','数据库',null,4)
Insert Into Course Values('2','数学',null,2)
Insert Into Course Values('3','信息系统',null,4)
Insert Into Course Values('4','操作系统',null,3)
Insert Into Course Values('5','数据结构',null,4)
Insert Into Course Values('6','数据处理',null,2)
Insert Into Course Values('7','PASCAL语言',null,4)
update course set cpno='5' where cno='1'
update course set cpno='1' where cno='3'
update course set cpno='6' where cno='4'
update course set cpno='7' where cno='5'
update course set cpno='6'where cno='7'
Create Table SC(--学生选课表
Sno Char(12),
Cno Char(12),
Primary Key(Sno, Cno),
Foreign Key(Sno) References Student(Sno),
Foreign Key(Cno) References Course(Cno)
);
Insert Into SC Values('20191102062', '1', 100)
Insert Into SC Values('20191102062', '2', 51)
Insert Into SC Values('20191102063', '1', 65)
Insert Into SC Values('20191102064', '2', 46)
Insert Into SC Values('20191102065', '3', 85)
Insert Into SC Values('20191102066', '3', 95)
Insert Into SC Values('20191102067', '7', 99)
Insert Into SC Values('20191102068', '6', 93)

[<表名1>.]<列名1><比较运算符>[<表名2>.]<列名2>

[<表名1>.]<列名1> BETWEEN [<表名2>.]<列名2>AND[<表名2>.]<列名3>

### 例3.49 查询每个学生及其选修课程情况。

Select Student.*, SC.* From Student, SC Where Student.Sno = SC.sno;

Select * From Student, SC Where Student.Sno = SC.sno;

Select * From Student, SC;

### 例3.50 用自然连接完成

Select Student.Sno, Sname, Ssex, Sage, Sdept, Cno,Grade From Student, SC
Where Student.Sno = SC.Sno;

### 3.51 查询选修1号课程且成绩在90分以上的所有的同学的学号和姓名。

Select Student.Sno, Sname From Student, SC
Where Student.Sno = SC.Sno And SC.Cno = '1' And SC.Grade > 90;

### 3.52 查询每一门课的间接先修课（即先修课的先修课）

Select First.Cno, Second.Cpno From Course First, Course Second
Where First.Cpno = Second.Cno;

### 3.53

Select Student.Sno, Sname, Ssex, Sage, Sdept, Cno, Grade
From Student Left Outer Join SC ON(Student.Sno = SC.Sno);