SQL经典练习题48道之一(1-10)

数据库表
1、学生信息表 Student
字段名 字段类型 字段约束 / 含义
Sno Varchar(3) Not null / 学员编号
Sname Varchar(4) Not null / 学员姓名
Ssex Varchar(2) Not null / 性别
Sbirthday Date 生日
Classnum Varchar(5) 班级号

2、课程信息表 course
字段名 字段类型 字段约束 / 含义
Cno Varchar(5) Not null / 课程编号
Cname Varchar(10) Not null / 课程名称
Tno Varchar(10) Not null / 授课老师编号

3、成绩表score
字段名 字段类型 字段约束 / 含义
Sno Varchar(3) Not null / 学员编号
Cno Varchar(5) Not null / 课程编号
Degree int Not null / 分数

4、讲师表teacher
字段名 字段类型 字段约束 / 含义
Tno Varchar(3) Not null / 讲师编号
Tname Varchar(4) Not null / 讲师姓名
Tsex Varchar(2) Not null / 讲师性别
Tbirthday Date Not null / 出生日期
Prof Varchar(6) 等级
Depart Varchar(10) 所属院系
按照要求完成下面的练习题:
1、写出建表语句
答:
create table Student(sno varchar(3) primary key,sname varchar(4) not null,ssex varchar(2) not null,sbirthday date,classnum varchar(5));
create table course(cno varchar(5) primary key,cname varchar(10) not null,tno varchar(10) not null);
create table score(sno varchar(3) ,cno varchar(5) not null,degree int not null,primary key(sno,cno));
create table teacher(tno varchar(3) primary key,tname varchar(4) not null,tsex varchar(2) not null,tbirthday date,prof varchar(6),depart varchar(10));
2、新增数据
答:
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASSNUM) VALUES (108 ,’曾华’ ,’男’ ,’1977-09-01’,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASSNUM) VALUES (105 ,’匡明’ ,’男’ ,’1975-10-02’,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASSNUM) VALUES (107 ,’王丽’ ,’女’ ,’1976-01-23’,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASSNUM) VALUES (101 ,’李军’ ,’男’ ,’1976-02-20’,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASSNUM) VALUES (109 ,’王芳’ ,’女’ ,’1975-02-10’,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASSNUM) VALUES (103 ,’陆君’ ,’男’ ,’1974-06-03’,95031);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,’李诚’,’男’,’1958-12-02’,’副教授’,’计算机系’);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,’张旭’,’男’,’1969-03-12’,’讲师’,’电子工程系’);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,’王萍’,’女’,’1972-05-05’,’助教’,’计算机系’);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,’刘冰’,’女’,’1977-08-14’,’助教’,’电子工程系’);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-105’ ,’计算机导论’,825);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-245’ ,’操作系统’ ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘6-166’ ,’数据电路’ ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘9-888’ ,’电子电路’ ,831);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,’3-245’,86);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,’3-245’,75);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,’3-245’,68);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,’3-105’,92);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,’3-105’,88);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,’3-105’,76);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,’3-105’,64);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,’3-105’,91);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,’3-105’,78);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,’6-166’,85);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,’6-106’,79);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,’6-166’,81);
3、查询Student表中的所有记录的Sname、Ssex和Classnum列。
答:
select sname,ssex,classnum from student;
4、 查询教师所有的院系即不重复的Depart列。
答:
select distinct depart from teacher;
5、 查询Student表的所有记录。
答:
select * from student;
6、 查询Score表中成绩在60到80之间的所有记录。
答:
select * from score where degree between 60 and 80;
7、查询Score表中成绩为85,86或88的记录。
答:
select * from score where degree in(85,86,88);
8、查询Student表中“95031”班或性别为“女”的同学记录。
答:
select * from student where classnum=’95031’ or ssex=’女’;
9、 以Classnum降序查询Student表的所有记录。
答:
select * from student order by classnum desc;
10、以Cno升序、Degree降序查询Score表的所有记录。
答:
select * from score order by cno,degree desc;

猜你喜欢

转载自blog.csdn.net/u014332200/article/details/80579213