SQL經典練習題48道之一(1-10)

feri發表於2018-06-05

資料庫表
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;

相關文章