class-notes/最后两周/MySQL数据库操作综合练习题答案.md
2025-03-22 19:49:55 +08:00

18 KiB
Raw Blame History

练习一

image-20250315225248590

#(1)创建数据库
CREATE DATABASE test04_Market DEFAULT CHARACTER SET utf8;
USE test04_Market;
#(2)创建数据表
CREATE TABLE customers(
	c_num INT(11) PRIMARY KEY	AUTO_INCREMENT,
	c_name VARCHAR(50),
	c_contact VARCHAR(50),
	c_city VARCHAR(50),
	c_birth DATETIME NOT NULL
);
#(3)更改字段的位置
#AFTER/BEFORE 字段名 设置位置在指定字段前/
ALTER TABLE 表名 MODIFY 字段名 数据类型 AFTER/BEFORE 字段名;
ALTER TABLE customers MODIFY c_contact VARCHAR(50) AFTER c_birth;
#(4)更改字段的数据类型
ALTER TABLE customers MODIFY c_name VARCHAR(70);
#(5)更改字段名
ALTER TABLE customers CHANGE c_contact c_phone VARCHAR(50);
#(6)添加字段
ALTER TABLE customers ADD c_gender CHAR(1);
#(7)更改表名
ALTER TABLE customers RENAME TO customers_info;
#(8)删除字段
ALTER TABLE customers_info DROP c_city;

image-20250315225304324

#(1)创建表时 添加外键约束并指定外键约束名称
CREATE TABLE orders(
	o_num INT(11) PRIMARY KEY AUTO_INCREMENT,
	o_date DATE,
	c_id INT(11),
	CONSTRAINT fk_cid FOREIGN KEY(c_id) REFERENCES customers_info(c_num)
);
#(2)
#删除指定外键
ALTER TABLE orders DROP FOREIGN KEY orders_ibfk_1;
#删除指定表
DROP TABLE orders;

练习二

image-20250315225318753

image-20250315225328264

#(1)创建表、插入记录
CREATE TABLE pet(
	`name` VARCHAR(20) NOT NULL COMMENT '宠物名称',
	`owner` VARCHAR(20) COMMENT '宠物主人',
	species VARCHAR(20) NOT NULL COMMENT '种类',
	sex CHAR(1) NOT NULL COMMENT '性别',
	birth YEAR NOT NULL COMMENT '出生日期',
	death YEAR COMMENT '死亡日期'
);
INSERT INTO pet()
VALUES
('Fluffy', 'Harold', 'cat', 'f', 2003, 2010),
('Claws', 'Gwen', 'cat', 'm', 2004,NULL ),
('Buffy', NULL, 'dog', 'f', 2009, NULL),
('Fang', 'Benny', 'dog', 'm', 2000, NULL),
('Bowser', 'Diane', 'dog', 'm', 2003, 2009),
('Chirpy', NULL, 'bird', 'f', 2008, NULL);
#(2)更新记录
UPDATE pet
SET `owner` = 'Kevin'
WHERE `name` = 'Fang';
#(3)
UPDATE pet
SET `owner` = 'Kevin'
WHERE `owner` IS NULL;
#(4)
DELETE FROM pet
WHERE death IS NOT NULL;
#(5)
TRUNCATE pet;

练习三

image-20250315225417117

image-20250315225431297

#1.创建数据库
CREATE DATABASE test_compay DEFAULT CHARACTER SET utf8;

#2.创建表
CREATE TABLE department(
	depid INT PRIMARY KEY AUTO_INCREMENT,
	depname VARCHAR(20) NOT NULL,
	deinfo VARCHAR(50)
);
CREATE TABLE employee(
	empid INT PRIMARY KEY AUTO_INCREMENT,
	`name` VARCHAR(20) NOT NULL,
	sex CHAR(1) NOT NULL DEFAULT '男',
	title VARCHAR(20) NOT NULL,
	birthday DATE NOT NULL,
	depid INT,
	CONSTRAINT fk_eid_did FOREIGN KEY(depid) REFERENCES department(depid)
);
CREATE TABLE salary(
	empid INT,
	basesalary DECIMAL(10, 2),
	titleSalary DECIMAL(10, 2),
	decuction DECIMAL(10, 2)
);
#3添加外键并设置级联更新、级联删除
ALTER TABLE salary ADD CONSTRAINT fk_sal_eid FOREIGN KEY(empid) 
REFERENCES employee(empid) ON UPDATE CASCADE ON DELETE CASCADE;
#4插入记录
INSERT INTO department (depid, depname, deinfo)
VALUES
(111, '生产部', NULL),
(222, '销售部', NULL),
(333, '人事部', NULL),
(444, '财务部', '负责公司财务管理'),
(555, '技术部', '负责技术研发与支持'),
(666, '市场部', '负责市场推广与品牌建设'),
(777, '客服部', '负责客户服务与支持'),
(888, '采购部', '负责公司物资采购'),
(999, '法务部', '负责公司法律事务'),
(1010, '行政部', '负责公司日常行政管理'),
(1011, '研发部', '负责新产品研发'),
(1012, '培训部', '负责员工培训与发展');

INSERT INTO employee (empid, name, sex, title, birthday, depid)
VALUES
(1001, '张三', '男', '高级工程师', '1975-1-1', 111),
(1002, '李四', '女', '助工', '1985-1-1', 111),
(1003, '王五', '男', '工程师', '1978-11-11', 222),
(1004, '赵六', '男', '工程师', '1999-1-1', 222),
(1005, '陈七', '女', '会计师', '1980-5-15', 444),
(1006, '刘八', '男', '软件工程师', '1990-8-20', 555),
(1007, '孙九', '女', '市场经理', '1985-12-25', 666),
(1008, '周十', '男', '客服专员', '1992-3-10', 777),
(1009, '吴十一', '女', '采购经理', '1988-7-22', 888),
(1010, '郑十二', '男', '法务顾问', '1983-9-30', 999),
(1011, '王十三', '女', '行政助理', '1995-4-18', 1010),
(1012, '李十四', '男', '研发工程师', '1991-11-5', 1011),
(1013, '赵十五', '女', '培训讲师', '1987-6-12', 1012),
(1014, '孙十六', '男', '技术支持', '1993-2-14', 555),
(1015, '杨十七', '女', '销售代表', '1994-8-8', 222);

INSERT INTO salary (empid, basesalary, titleSalary, decuction)
VALUES
(1001, 2200, 1100, 200),
(1002, 1200, 200, NULL),
(1003, 2900, 700, 200),
(1004, 1950, 700, 150),
(1005, 2500, 800, 100),
(1006, 3000, 1200, 250),
(1007, 2800, 1000, 200),
(1008, 1800, 300, 50),
(1009, 3200, 900, 300),
(1010, 3500, 1000, 400),
(1011, 2000, 400, 100),
(1012, 3100, 1100, 200),
(1013, 2700, 800, 150),
(1014, 2300, 700, 100),
(1015, 2600, 600, 200);

image-20250315225441802

#5
SELECT e.empid, e.`name`, e.title, d.depname, s.basesalary + s.titleSalary, s.basesalary + s.titleSalary-IFNULL(s.decuction, 0)
FROM employee e, department d, salary s
WHERE e.depid = d.depid AND e.empid = s.empid;
#6
SELECT e.`name`, s.basesalary
FROM employee e, department d, salary s
WHERE e.depid = d.depid AND e.empid = s.empid AND d.depname = '销售部';
#7
SELECT *
FROM employee e
WHERe e.`name` LIKE '张%' AND TIMESTAMPDIFF(YEAR,e.birthday, NOW()) < 40;
#8
SELECT e.`name`, s.basesalary, s.titleSalary
FROM employee e, salary s
WHERE e.sex = '男' AND e.empid AND s.empid;
#9
SELECT e.`name`, e.title, d.depname
FROM employee e, department d, salary s
WHERE e.depid = d.depid AND e.empid = s.empid AND s.basesalary < 2000;
#10
SELECT COUNT(*)
FROM employee;
#11
SELECT COUNT(*)
FROM department d;
#12
SELECT AVG(s.basesalary + s.titleSalary), MAX(s.basesalary + s.titleSalary), MIN(s.basesalary + s.titleSalary)
FROM employee e, salary s
WHERE e.empid = s.empid;
#13
SELECT d.depname, AVG(s.basesalary + s.titleSalary)
FROM employee e, salary s, department d
WHERE e.empid = s.empid AND e.depid = d.depid
GROUP BY d.depid;
#14
SELECT d.depname, AVG(s.basesalary + s.titleSalary) avg
FROM employee e, salary s, department d
WHERE e.empid = s.empid AND e.depid = d.depid
GROUP BY d.depid
HAVING avg<2000;
#15
SELECT e.empid, e.`name`, s.basesalary, s.titleSalary, s.decuction
FROM employee e, salary s, department d
WHERE e.empid = s.empid AND e.depid = d.depid
ORDER BY s.titleSalary, s.basesalary;
#16
SELECT e.empid, e.`name`, e.birthday, 
	IF(YEAR(e.birthday) < 1980, '老年', 
	IF(YEAR(e.birthday)< 1990, '中年', '青壮年'))
FROM employee e;
#17
SELECT e.*, depname
FROM employee e, department d
WHERE e.depid = d.depid;
#18
SELECT d.*, e.*
FROM employee e, department d
WHERE e.depid = d.depid;
#19
SELECT *
FROM employee e
WHERE e.title LIKE '%工程师%' AND e.sex = '男';
#20
SELECT d.depname, e.sex, COUNT(*), AVG(s.basesalary)
FROM employee e, department d, salary s
WHERE e.depid = d.depid AND e.empid = s.empid
GROUP BY d.depid, e.sex;

练习四

image-20250315225803115

image-20250315225818026

#1
CREATE DATABASE test_school DEFAULT CHARACTER SET 'UTF8';
#2
DROP TABLE department;
CREATE TABLE department(
	depNo INT(10) PRIMARY KEY COMMENT '部门号',
	depName VARCHAR(50) NOT NULL COMMENT '部门名称',
	depNote VARCHAR(20) COMMENT '部门备注'
);
CREATE TABLE teacher(
	number INT PRIMARY KEY COMMENT '教工号',
	`name` VARCHAR(30) NOT NULL COMMENT '姓名',
	sex VARCHAR(4) COMMENT '性别',
	birth DATE COMMENT '出生日期',
	depNo INT COMMENT '部门号',
	salary FLOAT COMMENT '工资',
	address VARCHAR(100) COMMENT '家庭住址'
);
INSERT INTO department()
VALUES
(601, '软件技术系', '软件技术等专业'),
(602, '网络技术系', '多媒体技术等专业'),
(603, '艺术设计系', '广告艺术设计等专业'),
(604, '管理工程系', '连锁经营管理等专业');

INSERT INTO teacher()
VALUES
(2001, 'Tom', '女', '1970-01-10', 602, 4500, '四川省绵阳市'),
(2002, 'Lucy', '男', '1983-12-18', 601, 2500, '北京市昌平区'),
(2003, 'Mike', '男', '1990-06-01', 604, 1500, '重庆市渝中区'),
(2004, 'James', '女', '1980-10-20', 602, 3500, '四川省成都市'),
(2005, 'Jack', '男', '1975-05-30', 603, 1200, '重庆市南岸区');

image-20250315225829272

#4
SELECT *
FROM teacher;
#5
SELECT t.number '教工号', d.depName '部门名称'
FROM teacher t, department d
WHERE t.depNo = d.depNo AND t.address LIKE '%北京%';
#6
SELECT t.number, t.`name`
FROM teacher t
ORDER BY t.salary DESC
LIMIT 1;
#7
SELECT t.number, t.`name`
FROM teacher t
WHERE t.salary BETWEEN 2500 AND 4000;
#8
SELECT t.`name`, t.sex, t.salary
FROM teacher t, department d
WHERE t.depNo = d.depNo AND d.depName = '网络技术系';

练习五

image-20250315230015174

-- 创建数据库并设置字符集为 utf8mb4
CREATE DATABASE test_student CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test_student;

-- 创建表 Classes
CREATE TABLE Classes (
    专业 VARCHAR(50),
    班级 VARCHAR(50),
    姓名 VARCHAR(50),
    性别 VARCHAR(10),
    座位 INT
);

-- 向表 Classes 插入数据
INSERT INTO Classes (专业, 班级, 姓名, 性别, 座位) VALUES
('计算机网络', '1班', '张三', '男', 8),
('软件工程', '2班', '李四', '男', 12),
('计算机维护', '1班', '王五', '男', 9),
('计算机网络', '2班', 'LILY', '女', 15),
('软件工程', '1班', '小强', '男', 20),
('计算机维护', '1班', 'CoCo', '女', 18);

-- 创建表 Score
CREATE TABLE Score (
    姓名 VARCHAR(50),
    英语 INT,
    数学 INT,
    语文 INT
);

-- 向表 Score 插入数据
INSERT INTO Score (姓名, 英语, 数学, 语文) VALUES
('张三', 65, 75, 98),
('李四', 87, 45, 86),
('王五', 98, 85, 65),
('LILY', 75, 86, 87),
('小强', 85, 60, 58),
('CoCo', 96, 87, 70);

-- 创建表 Records
CREATE TABLE Records (
    姓名 VARCHAR(50),
    记录 VARCHAR(50)
);

-- 向表 Records 插入数据
INSERT INTO Records (姓名, 记录) VALUES
('小强', '迟到'),
('小强', '事假'),
('李四', '旷课'),
('李四', '旷课'),
('李四', '迟到'),
('CoCo', '病假'),
('LILY', '事假');

image-20250315230025538

#3
UPDATE score s
SET s.`语文` = 88
WHERE s.`姓名` = '张三';

#4
SELECT AVG(s.`英语`), AVG(s.`数学`), AVG(s.`语文`)
FROM classes c, score s
WHERE c.`姓名` = s.`姓名` AND c.`班级` = 1 AND c.`专业` = '计算机维护';

#5
SELECT *
FROM score s
WHERE s.`数学` < 60 OR s.`英语` < 60 OR s.`语文` < 60;

#6
SELECT *
FROM score s
WHERE s.`姓名` IN (
	SELECT r.`姓名`
	FROM records r
	GROUP BY r.`姓名`
	HAVING COUNT(*) > 2
);

练习六

image-20250315230057816

-- 创建数据库 test_xuankedb 并设置字符集为 utf8mb4
CREATE DATABASE test_xuankedb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test_xuankedb;

-- 创建学生表 Student
CREATE TABLE Student (
    Sn0 VARCHAR(20) PRIMARY KEY,  -- 学号,主键
    Sname VARCHAR(50),            -- 姓名
    Ssex VARCHAR(10),             -- 性别
    Sage INT,                     -- 年龄
    Sdept VARCHAR(50)             -- 所在系
);

-- 创建课程表 Course
CREATE TABLE Course (
    Cn0 VARCHAR(20) PRIMARY KEY,  -- 课程号,主键
    Cname VARCHAR(100),           -- 课程名
    Cpn0 VARCHAR(20),             -- 选修课号
    Ccredit INT                   -- 学分
);

-- 创建成绩表 SG
CREATE TABLE SG (
    Sn0 VARCHAR(20),              -- 学号
    Cn0 VARCHAR(20),              -- 课程号
    Grade INT,                    -- 成绩
    PRIMARY KEY (Sn0, Cn0),       -- 复合主键 (学号, 课程号)
    FOREIGN KEY (Sn0) REFERENCES Student(Sn0),  -- 外键,引用 Student 表
    FOREIGN KEY (Cn0) REFERENCES Course(Cn0)    -- 外键,引用 Course 表
);

INSERT INTO Student (Sno, Sname, Ssex, Sage, Sdept, Scome) VALUES
('05001', '张三', '男', 20, '计算机系', '2021-09-01'),
('05002', '李四', '女', 21, '数学系', '2021-09-01'),
('05003', '王五', '男', 22, '计算机系', '2021-09-01'),
('05004', '李洋', '男', 19, '物理系', '2021-09-01'),
('05019', '赵六', '女', 20, '计算机系', '2021-09-01');

INSERT INTO Course (Cno, Cname, Cpno, Ccredit) VALUES
('1', '数据库', NULL, 4),
('2', '数学', NULL, 3),
('3', '物理', NULL, 3),
('4', '操作系统', '1', 4);

INSERT INTO SG (Sno, Cno, Grade) VALUES
('05001', '1', 85),
('05001', '2', 90),
('05002', '1', 78),
('05002', '3', 88),
('05003', '1', 92),
('05003', '4', 76),
('05004', '3', 95),
('05019', '1', 80),
('05019', '4', 85);

练习七

image-20250315230113710

image-20250315230128412

-- 创建数据库 test_library 并设置字符集为 utf8mb4
CREATE DATABASE test_library CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test_library;

-- 创建表 press出版社
CREATE TABLE press (
    pressid INT PRIMARY KEY,  -- 出版社编号,主键
    pressname VARCHAR(100),   -- 出版社名称
    address VARCHAR(100)      -- 出版社地址
);

-- 创建表 sort种类
CREATE TABLE sort (
    sortno INT PRIMARY KEY,   -- 种类编号,主键
    scount INT,               -- 数量
    describes VARCHAR(100)    -- 描述
);

-- 创建表 book图书
CREATE TABLE book (
    bid INT PRIMARY KEY,      -- 图书编号,主键
    bname VARCHAR(100),      -- 图书名称
    bsortno INT,             -- 种类编号
    pressid INT,             -- 出版社编号
    FOREIGN KEY (bsortno) REFERENCES sort(sortno),  -- 外键,引用 sort 表
    FOREIGN KEY (pressid) REFERENCES press(pressid) -- 外键,引用 press 表
);

-- 向 press 表插入数据
INSERT INTO press (pressid, pressname, address) VALUES
(100, '外研社', '上海'),
(101, '北大出版社', '北京'),
(102, '教育出版社', '北京');

-- 向 sort 表插入数据
INSERT INTO sort (sortno, scount, describes) VALUES
(11, 50, '小说'),
(12, 100, '科幻'),
(13, 100, '神话');

-- 向 book 表插入数据
INSERT INTO book (bid, bname, bsortno, pressid) VALUES
(1, '红与黑', 11, 100),
(2, '幻城', 12, 102),
(3, '希腊神话', 13, 102);

#5
SELECT b.*
FROM book b, press p
WHERE p.pressid = b.pressid AND p.pressid = 100;

#6
SELECT b.*
FROM book b, press p
WHERE p.pressid = b.pressid AND p.pressname = '外研社';

#7
SELECT *
FROM sort s
WHERE s.scount > 100;

#8
SELECT p.*
FROM book b, press p
WHERE p.pressid = b.pressid
GROUP BY b.pressid
HAVING COUNT(*) = (
	SELECT COUNT(*)
	FROM book b, press p
	WHERE p.pressid = b.pressid
	GROUP BY b.pressid
	ORDER BY COUNT(*) DESC
	LIMIT 1
);

练习八

image-20250315230214156

image-20250315230227129

-- 创建数据库 test_tour 并设置字符集为 utf8mb4
CREATE DATABASE test_tour CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test_tour;

-- 创建表 agency旅行社表
CREATE TABLE agency (
    id INT PRIMARY KEY,        -- 旅行社编号,主键
    name VARCHAR(100) NOT NULL, -- 旅行社名
    address VARCHAR(100) NOT NULL, -- 旅行社地址
    areaid INT                -- 所属区域编号
);

-- 创建表 travel旅行线路表
CREATE TABLE travel (
    tid INT PRIMARY KEY,      -- 旅行线路编号,主键
    time VARCHAR(50) NOT NULL, -- 所需时间
    position VARCHAR(100) NOT NULL, -- 目的地
    money FLOAT,              -- 花费
    aid INT NOT NULL,         -- 所属旅行社编号
    count INT,                -- 报名人数
    FOREIGN KEY (aid) REFERENCES agency(id) -- 外键,引用 agency 表
);

-- 向 agency 表插入数据
INSERT INTO agency (id, name, address) VALUES
(101, '青年旅行社', '北京海淀'),
(102, '天天旅行社', '天津海院');

-- 向 travel 表插入数据
INSERT INTO travel (tid, time, position, money, aid, count) VALUES
(1, '5天', '八达岭', 3000, 101, 10),
(2, '7天', '水长城', 5000, 101, 14),
(3, '8天', '水长城', 6000, 102, 11);

#4
SELECT a.*
FROM travel t, agency a
WHERE t.aid =a.id
GROUP BY a.id
HAVING COUNT(*) = (
	SELECT COUNT(*)
	FROM travel t, agency a
	WHERE t.aid =a.id
	GROUP BY a.id
	ORDER BY COUNT(*) DESC
	LIMIT 1
);

#5
SELECT t.*
FROM travel t
WHERE t.count = (
	SELECT t.count
	FROM travel t
	ORDER BY t.count DESC
	LIMIT 1
);

#6
SELECT *
FROM travel t
WHERE t.money < 5000;

#7
SELECT a.`name`
FROM travel t, agency a
WHERE t.aid =a.id AND t.money = (
	SELECT t.money
	FROM travel t, agency a
	WHERE t.aid =a.id
	ORDER BY t.money DESC
	LIMIT 1
);

#8
SELECT SUM(t.time)
FROM travel t, agency a
WHERE t.aid =a.id AND a.`name` = '青年旅行社';