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.
原文地址:https://www.cnblogs.com/javawen/p/10802769.html