windows10操作系统,使用MySQL 5.5 Command Line完成实验
实验课教材(MySQL数据库技术与实验指导)第73页“实验内容与要求”第1题
基于jxgl数据库,使用SQL语句表达以下查询
CREATE DATABASE jxgl;
use jxgl;
CREATE TABLE IF NOT EXISTS student(
sno CHAR(7) NOT NULL,
sname VARCHAR(16),
sage SMALLINT CHECK(sage>=16 AND sage<=45),
ssex CHAR(2) DEFAULT '男' CHECK(ssex='男' OR ssex='女'),
sdept CHAR(2),
PRIMARY KEY(sno)
) ENGINE=InnoDB;
INSERT INTO student
VALUES
('2005001', '钱横', 18, '男','Cs'),
('2005002', '王林', 19, '女','Cs'),
('2005003', '李民', 23, '男','Is'),
('2005004', '赵欣然', 16, '女','Ma');
CREATE TABLE IF NOT EXISTS course(
cno CHAR(2) NOT NULL,
cname VARCHAR(20),
cpno CHAR(2),
ccredit SMALLINT,
PRIMARY KEY(cno)
) ENGINE=InnoDB;
INSERT INTO course
VALUES
('1', '数据库系统', '5', 4),
('2', '数学分析', '', 2),
('3', '信息系统导论', '1', 3),
('4', '操作系统原理', '6', 3),
('5', '数据结构', '7', 4),
('6', '数据处理基础', '', 4),
('7', 'C语言', '6', 3);
CREATE TABLE IF NOT EXISTS sc(
sno CHAR(7) NOT NULL,
cno CHAR(2) NOT NULL,
grade SMALLINT,
PRIMARY KEY(sno, cno),
FOREIGN KEY(sno) REFERENCES student(sno),
FOREIGN KEY(cno) REFERENCES course(cno)
) ENGINE=InnoDB;
INSERT INTO sc
VALUES
('2005001', '1', 87),
('2005001', '2', 67),
('2005001', '3', 90),
('2005001', '4', 90),
('2005001', '5', 90),
('2005001', '6', 90),
('2005001', '7', 90),
('2005002', '2', 95),
('2005002', '4', 88),
('2005003', '3', 88),
('2005003', '4', 88),
('2005004', '4', 88);
SELECT sno, sname
FROM student
WHERE sage>23 AND ssex='男';
SELECT student.sname
FROM student, sc
WHERE student.ssex='女' AND student.sno=sc.sno
GROUP BY student.sno
HAVING count(*)>=1;
SELECT cno
FROM course
WHERE NOT EXISTS
(SELECT * FROM student, sc
WHERE sc.sno=student.sno
AND sc.cno=course.cno AND student.sname='王林');
SELECT student.sno
FROM student, sc
WHERE student.sno=sc.sno
GROUP BY student.sno
HAVING count(*)>=2;
SELECT cno, cname
FROM course
WHERE NOT EXISTS
(SELECT * FROM student
WHERE NOT EXISTS
(SELECT * FROM sc
WHERE sno=student.sno AND cno=course.cno));
SELECT AVG(grade)
FROM course, sc
WHERE course.cno=sc.cno AND course.ccredit=3;