class-notes/2207/MySQL数据库操作综合练习题答案.md
2025-03-15 23:14:59 +08:00

6.6 KiB

练习一

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


练习四

image-20250315225803115

image-20250315225818026

image-20250315225829272


练习五

image-20250315230015174

image-20250315230025538


练习六

image-20250315230057816


练习七

image-20250315230113710

image-20250315230128412


练习八

image-20250315230214156

image-20250315230227129