數據庫查詢操作練習
CREATE TABLE `student` (
`sno` varchar(20) NOT NULL COMMENT ‘學號‘,
`sname` varchar(20) NOT NULL COMMENT ‘學生姓名‘,
`ssex` varchar(20) NOT NULL COMMENT ‘學生性別‘,
`sbirthday` datetime DEFAULT NULL COMMENT ‘學生出生年月‘,
`class` varchar(20) DEFAULT NULL COMMENT ‘學生所在班級‘,
PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `course` (
`cno` varchar(255) NOT NULL COMMENT ‘課程號‘,
`cname` varchar(255) NOT NULL COMMENT ‘課程名稱‘,
`tno` varchar(255) NOT NULL COMMENT ‘教工編號‘,
PRIMARY KEY (`cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `score` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sno` varchar(20) NOT NULL COMMENT ‘學號‘,
`cno` varchar(20) NOT NULL COMMENT ‘課程號‘,
`degree` decimal(4,1) DEFAULT NULL COMMENT ‘成績‘,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
CREATE TABLE `teacher` (
`tno` varchar(20) NOT NULL DEFAULT ‘‘ COMMENT ‘教工編號
`tname` varchar(20) NOT NULL DEFAULT ‘‘ COMMENT ‘教工姓名‘,
`tsex` varchar(20) NOT NULL DEFAULT ‘‘ COMMENT ‘教工性別‘,
`tbirthday` datetime DEFAULT NULL COMMENT ‘教工出生年月‘,
`prof` varchar(20) DEFAULT NULL COMMENT ‘職稱‘,
`depart` varchar(20) NOT NULL COMMENT ‘教工所在部門‘,
PRIMARY KEY (`tno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO student(sno,sname,ssex,sbirthday,class)VALUES
(‘101‘,‘李軍‘,‘男‘,‘1977-09-01 00:00:00‘,‘95033‘),
(‘103‘,‘陸君‘,‘男‘,‘1977-09-01 00:00:00‘,‘95031‘),
(‘105‘,‘匡明‘,‘男‘,‘1977-09-01 00:00:00‘,‘95031‘),
(‘107‘,‘王麗‘,‘女‘,‘1977-09-01 00:00:00‘,‘95033‘),
(‘108‘,‘曾華‘,‘男‘,‘1977-09-01 00:00:00‘,‘95033‘),
(‘109‘,‘王芳‘,‘女‘,‘1977-09-01 00:00:00‘,‘95031‘);
INSERT INTO course(cno,cname,tno)VALUES
(‘3-105‘,‘計算機導論‘,‘825‘),
(‘3-245‘,‘操作系統‘,‘804‘),
(‘6-166‘,‘數學電路‘,‘856‘),
(‘9-888‘,‘高等數學‘,‘831‘);
INSERT INTO score(sno,cno,degree)VALUES
(‘103‘,‘3-245‘,‘86‘),
(‘105‘,‘3-245‘,‘75‘),
(‘109‘,‘3-245‘,‘68‘),
(‘103‘,‘3-105‘,‘92‘),
(‘105‘,‘3-105‘,‘88‘),
(‘109‘,‘3-105‘,‘76‘),
(‘101‘,‘3-105‘,‘64‘),
(‘107‘,‘3-105‘,‘91‘),
(‘108‘,‘3-105‘,‘78‘),
(‘101‘,‘6-166‘,‘85‘),
(‘107‘,‘6-166‘,‘79‘),
(‘108‘,‘6-166‘,‘81‘);
INSERT INTO teacher(tno,tname,tsex,tbirthday,prof,depart)VALUES
(‘804‘,‘李誠‘,‘男‘,‘1958-12-02 00:00:00‘,‘副教授‘,‘計算機系‘),
(‘825‘,‘王萍‘,‘女‘,‘1972-05-05 00:00:00‘,‘助教‘,‘計算機系‘),
(‘831‘,‘劉冰‘,‘女‘,‘1977-08-14 00:00:00‘,‘助教‘,‘電子工程系‘),
(‘856‘,‘張旭‘,‘男‘,‘1969-03-12 00:00:00‘,‘講師‘,‘電子工程系‘);
SELECT sname,ssex,class FROM student;-- 1)查詢Student表中的所有記錄的Sname、Ssex和Class列。
SELECT DISTINCT depart FROM teacher;-- 2)查詢教師所有的單位即不重復的Depart列。
SELECT student.* FROM student;-- 3)查詢Student表的所有記錄。
SELECT score.* FROM score WHERE degree BETWEEN 60 and 80;-- 4)查詢Score表中成績在60到80之間的所有記錄。
SELECT score.* FROM score WHERE degree IN(‘85‘,‘86‘,‘88‘);-- 5)查詢Score表中成績為85,86或88的記錄。
SELECT student.* FROM student WHERE class=‘95031‘ OR ssex=‘女‘;-- 6)查詢Student表中“95031”班或性別為“女”的同學記錄。
SELECT student.* FROM student ORDER BY class DESC;-- 7)以Class降序查詢Student表的所有記錄。
SELECT score.* FROM score ORDER BY cno ASC,degree DESC;-- 8)以Cno升序、Degree降序查詢Score表的所有記錄。(多個排序使用“,”逗號隔開)
SELECT score.sno,score.cno FROM score ORDER BY degree DESC LIMIT 0,1;-- 9)查詢Score表中的最高分的學生學號和課程號。(排序後limit)
SELECT score.sno FROM score WHERE degree>70 AND degree<90;-- 10)查詢分數大於70,小於90的Sno列。
SELECT student.sname,score.cno,score.degree FROM student INNER JOIN score WHERE student.sno=score.sno;-- 11)查詢所有學生的Sname、Cno和Degree列。(多表查詢)
SELECT score.sno,course.cname,score.degree FROM score INNER JOIN course WHERE score.cno=course.cno;-- 12)查詢所有學生的Sno、Cname和Degree列。
SELECT student.sname,course.cname,score.degree FROM student INNER JOIN course INNER JOIN score WHERE student.sno=score.sno AND score.cno=course.cno;-- 13)查詢所有學生的Sname、Cname和Degree列。
SELECT score.degree FROM score WHERE score.degree>(SELECT score.degree FROM score WHERE sno=‘109‘ AND cno=‘3-105‘);-- 14)查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
SELECT student.sno,student.sname,student.sbirthday FROM student WHERE sbirthday=(SELECT student.sbirthday FROM student WHERE sno=‘108‘);-- 15)查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。
SELECT score.degree FROM score INNER JOIN course WHERE score.cno=course.cno AND course.cname=‘計算機導論‘;-- 16)查詢考計算機導論的學生成績
SELECT course.cname FROM course INNER JOIN teacher WHERE course.tno=teacher.tno AND teacher.tname=‘李誠‘;-- 17)查詢李誠老師教的課程名稱
SELECT teacher.depart FROM teacher INNER JOIN course WHERE teacher.tno=course.tno AND course.cname=‘高等數學‘;-- 18)教高等數學的老師是哪個系的
SELECT course.cno FROM course INNER JOIN score WHERE course.cno=score.cno AND score.degree>‘85‘;-- 19)查詢成績表中存在有85分以上成績的課程Cno.
SELECT teacher.tname ‘姓名‘,teacher.tsex ‘性別‘,teacher.tbirthday ‘出生年月‘ FROM teacher UNION SELECT student.sname,student.ssex,student.sbirthday FROM student;-- 20)查詢所有教師和同學的name、sex和birthday.
SELECT teacher.tname ‘姓名‘,teacher.tsex ‘性別‘,teacher.tbirthday ‘出生年月‘ FROM teacher WHERE teacher.tsex=‘女‘ UNION SELECT student.sname,student.ssex,student.sbirthday FROM student WHERE student.ssex=‘女‘;-- 21)查詢所有“女”教師和“女”同學的name、sex和birthday.
SELECT teacher.tname,teacher.depart FROM teacher INNER JOIN course WHERE teacher.tno=course.tno;-- 22)查詢所有任課教師的Tname和Depart.
SELECT student.* FROM student WHERE student.sname NOT LIKE ‘王%‘;-- 23)查詢Student表中不姓“王”的同學記錄。
SELECT student.* FROM student ORDER BY class DESC,sbirthday DESC;-- 24)以班號和年齡從大到小的順序查詢Student表中的全部記錄。
SELECT teacher.tname,course.cname FROM teacher INNER JOIN course WHERE teacher.tno=course.tno AND teacher.tsex=‘男‘;-- 25)查詢“男”教師及其所上的課程。
SELECT student.sname FROM student WHERE student.ssex=(SELECT student.ssex FROM student WHERE student.sname=‘李軍‘)AND sname<>‘李軍‘;-- 26)查詢和“李軍”同性別的所有同學的Sname.
數據庫查詢操作練習