1.创建S-T模式(即数据库的名称)
create schema `S-T`;
2.建立一个“学生”表Student
create table Student
(Sno char(9) primary key,
Sname char(40) unique,
Ssex char(2),
Sage smallint,
sDept char(20)
);
3.建立一个“课程”表Course
create table Course
(Cno char(4) primary key, /*列优先完整性约束条件,Cno是主码*/
Cname char(40),
Cpno char(4), /*Cpno是先修课*/
Ccredit smallint,
foreign key(Cpno) references Course(Cno)
/*表级完整性约束条件,Cpno是外码,被参照表是Course,参照列是Cno*/
);
4.建立学生选课表SC
create table SC
(Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
/*主码由两个属性构成,必须作为表级完整性进行定义*/
foreign key (Sno) references Student(Sno),
/*表级完整性条件,Sno是外码,被参照表是Student*/
foreign key (Cno) references Course(Cno)
/*表级完整性条件,Cno是外码,被参照表是CourseSC*/
);
5.建索引
create unique index stusno on Student(Sno);
6.删除索引
drop index stusno;
7-1.查询全体学生的学号与姓名
select Sno,Sname from Student;
7-2.查询全体学生的姓名、学号、所在系
select Sname,Sno,Sdept from Student;
7-3. 查询全体学生的姓名及其出生年份
select Sname,2013-Sage from Student;
7-4. 查询全体学生的姓名、出生年份和所在院系,要求用小写字母表示所在系名
(1)select Sname,'Year of Brith:',2013-Sage,lower(Sdept) from Student;
(2)select Sname,'Year of Brith:'BIREH,2013-Sage BIRTHDY,lower(Sdept)DEPARTMENT from Student;/*取别名*/
8-1.查询选修了课程的学生的学号(取消取值重复的行)
(1)select Sno from SC; /*有重复的学号,等价与“select all Sno from SC;”*/
(2)select distinct Sno from SC; /*加distinct取消重复的*/
8-2 查询满足条件的元组
比较大小
(1)查询所有年龄在20岁以下的学生的姓名及其年龄
select Sname,Sage from Student where Sage<20;
(2)查询成绩不及格的学生的学号
select distinct Sno from SC where Grade<60;
确定范围
(3)查询年龄在20~30岁之间的学生的姓名和所在系及年龄
select Sname,Sdept,Sage from Student where Sage between 20 and 23;
(4)查询年龄不在20~30岁之间的学生的姓名和所在系及年龄
select Sname,Sdept,Sage from Student where Sage not between 20 and 23;
确定集合
(5)查询计算机系(CS)、数学系(MA)、信息系(IS)学生额度姓名和性别
select Sname,Ssex from Student where Sdept in('CS','MA','IS');
(5)查询不是计算机系(CS)、数学系(MA)、信息系(IS)学生额度姓名和性别
select Sname,Ssex from Student where Sdept not in('CS','MA','IS');
字符匹配
(7)查询学号为2002121的学生的详细情况
select * from Student where Sno like '200215121';
或 select * from Student where Sno='200215121';
(8)查询所有姓刘的学生的姓名、学号和性别
select Sname,sno,Ssex from Student where Sname like '刘%';
(8)查询姓“欧阳”且全名为3个汉字的学生的姓名
select Sname from Student where Sname like '欧阳_ _';
(9)查询名字种第2个子为“阳”的学生的姓名和学号
select Sname,Sno from Student where Sname like '_阳%';
(10)查询所有不姓刘的学生的姓名
select Sname from Student where Sname not like '刘%';
(11)查询DB_Design课程的课程号和学分
select Cno,Ccredit from cource where Cname like 'DB\_Design' ESCAPE`\`;/*ESCAPE`\表示“\”为转换字符,后面不再具有通配符的含义*/
(12)查询以”DB——开头的,且倒数第三个字符为i的课程的详细情况
select * from cource where Cname like 'DB\_%i__'ESCAPE`\`;
设计空值的查询
(13)查询学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的详细情况
select * from Student where Grade is null;/*is不能用=代替*/
(14)查询所有成绩的学生学号和课程
select Sno,Cno from SC where Grade is not null;
多条件查询
(15)查询计算机系年龄在10岁以下的学生姓名
select Sname from Student where Sdept='CS' and Sage<20;
(16)
create schema `S-T`;
2.建立一个“学生”表Student
create table Student
(Sno char(9) primary key,
Sname char(40) unique,
Ssex char(2),
Sage smallint,
sDept char(20)
);
3.建立一个“课程”表Course
create table Course
(Cno char(4) primary key, /*列优先完整性约束条件,Cno是主码*/
Cname char(40),
Cpno char(4), /*Cpno是先修课*/
Ccredit smallint,
foreign key(Cpno) references Course(Cno)
/*表级完整性约束条件,Cpno是外码,被参照表是Course,参照列是Cno*/
);
4.建立学生选课表SC
create table SC
(Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
/*主码由两个属性构成,必须作为表级完整性进行定义*/
foreign key (Sno) references Student(Sno),
/*表级完整性条件,Sno是外码,被参照表是Student*/
foreign key (Cno) references Course(Cno)
/*表级完整性条件,Cno是外码,被参照表是CourseSC*/
);
5.建索引
create unique index stusno on Student(Sno);
6.删除索引
drop index stusno;
7-1.查询全体学生的学号与姓名
select Sno,Sname from Student;
7-2.查询全体学生的姓名、学号、所在系
select Sname,Sno,Sdept from Student;
7-3. 查询全体学生的姓名及其出生年份
select Sname,2013-Sage from Student;
7-4. 查询全体学生的姓名、出生年份和所在院系,要求用小写字母表示所在系名
(1)select Sname,'Year of Brith:',2013-Sage,lower(Sdept) from Student;
(2)select Sname,'Year of Brith:'BIREH,2013-Sage BIRTHDY,lower(Sdept)DEPARTMENT from Student;/*取别名*/
8-1.查询选修了课程的学生的学号(取消取值重复的行)
(1)select Sno from SC; /*有重复的学号,等价与“select all Sno from SC;”*/
(2)select distinct Sno from SC; /*加distinct取消重复的*/
8-2 查询满足条件的元组
比较大小
(1)查询所有年龄在20岁以下的学生的姓名及其年龄
select Sname,Sage from Student where Sage<20;
(2)查询成绩不及格的学生的学号
select distinct Sno from SC where Grade<60;
确定范围
(3)查询年龄在20~30岁之间的学生的姓名和所在系及年龄
select Sname,Sdept,Sage from Student where Sage between 20 and 23;
(4)查询年龄不在20~30岁之间的学生的姓名和所在系及年龄
select Sname,Sdept,Sage from Student where Sage not between 20 and 23;
确定集合
(5)查询计算机系(CS)、数学系(MA)、信息系(IS)学生额度姓名和性别
select Sname,Ssex from Student where Sdept in('CS','MA','IS');
(5)查询不是计算机系(CS)、数学系(MA)、信息系(IS)学生额度姓名和性别
select Sname,Ssex from Student where Sdept not in('CS','MA','IS');
字符匹配
(7)查询学号为2002121的学生的详细情况
select * from Student where Sno like '200215121';
或 select * from Student where Sno='200215121';
(8)查询所有姓刘的学生的姓名、学号和性别
select Sname,sno,Ssex from Student where Sname like '刘%';
(8)查询姓“欧阳”且全名为3个汉字的学生的姓名
select Sname from Student where Sname like '欧阳_ _';
(9)查询名字种第2个子为“阳”的学生的姓名和学号
select Sname,Sno from Student where Sname like '_阳%';
(10)查询所有不姓刘的学生的姓名
select Sname from Student where Sname not like '刘%';
(11)查询DB_Design课程的课程号和学分
select Cno,Ccredit from cource where Cname like 'DB\_Design' ESCAPE`\`;/*ESCAPE`\表示“\”为转换字符,后面不再具有通配符的含义*/
(12)查询以”DB——开头的,且倒数第三个字符为i的课程的详细情况
select * from cource where Cname like 'DB\_%i__'ESCAPE`\`;
设计空值的查询
(13)查询学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的详细情况
select * from Student where Grade is null;/*is不能用=代替*/
(14)查询所有成绩的学生学号和课程
select Sno,Cno from SC where Grade is not null;
多条件查询
(15)查询计算机系年龄在10岁以下的学生姓名
select Sname from Student where Sdept='CS' and Sage<20;
(16)