MySQL——DDL、DML操作强化练习
题目
完成以下操作:
①在部门表(t_department)中添加两条数据:
‘研发部’,’技术支持’
‘咨询部’,’产品咨询服务’
②在岗位表(t_job)中添加四条数据:
1,’JavaSE 工程师’,’Java 开发’
2,’Web 工程师’,’Web 开发’
3,’JavaEE 框架工程师’,’框架开发’
4,’产品顾问’,’产品咨询’
③在员工表(t_employee)中添加六条数据:
1,’孙红雷’,’男’,’123456789012345678′,’12345678901′,1,NULL,’1990-01- 01′,’2015-01-01′,’东北’,1
2,’张亮’,’男’,’123456789012345677′,’12345678902′,2,NULL,’1990-01-02′,’2015-01- 02′,’北京’,1
3,’鹿晗’,’男’,’123456789012345676′,’12345678903′,3,NULL,’1990-01-03′,’2015-01- 03′,’北京’,1
4,’邓超’,’男’,’123456789012345675′,’12345678904′,2,NULL,’1990-01-04′,’2015-01- 04′,’北京’,1
5,’孙俪’,’女’,’123456789012345674′,’12345678905′,3,NULL,’1990-01-05′,’2015-01- 05′,’北京’,1),
6,’Angelababy’,’女’,’123456789012345673′,’12345678906′,4,NULL,’1990-01- 06′,’2015-01-06′,’香港’,2
④在薪资表(t_salary)中添加六条数据:
1,'12000.00','6000.00','0.40','0.00'
2,'9000.00','5000.00','0.20',NULL
3,'11000.00','8000.00',NULL,NULL
4,'13000.00','5000.00',NULL,NULL
5,'8000.00','8000.00','0.30',NULL
6,'15000.00','6000.00',NULL,NULL
⑤完成以下查询
修改所有人的基本工资,涨薪 5%
修改”孙俪”的手机号码为”13709098765″,生日为”1982-09-26″
修改”邓超”的入职日期为今天
修改”咨询部”的主管 id 为 6
修改”研发部”的主管 id 为 1
修改”研发部”的主管 id 为”孙红雷”的编号
修改所有员工的领导编号为该员工所在部门的主管编号
修改研发部的主管编号,以及该部门所有员工的领导编号为”邓超”的编号
删除名为 “Angelababy”的员工信息 及 薪资信息
查询所有员工信息
查询所有员工信息并使用中文别名
查询员工姓名以及手机号码
查询’教学部’名为 ‘孙红雷’的员工详细信息
查询 employees 表中的所有部门编号若有重复请去重
查询月薪>5000 的员工信息
查询月薪=12000 的员工信息
查询月薪不等于 12000 的员工信息
查询没有奖金的员工名
查询月薪在 5000 到 12000 的员工工资和姓名
查询月薪不在 5000 到 12000 的员工工资和姓名
查询部门编号=90 或 月薪>10000 并且月薪<15000 的员工信息
显示出表 employees 中的全部 job_id[不能重复]
显示出表 employees 的全部列,各个列之间用逗号连接,列头显示成"信息汇总"
显示出表 employees 部门编号在 80-100 之间 的姓名、职位
显示出表 employees 的 manager_id 是 100,101,110 的员工姓名、职位
答案
DROP TABLE IF EXISTS t_salary;
DROP TABLE IF EXISTS t_employee;
DROP TABLE IF EXISTS t_job;
DROP TABLE IF EXISTS t_department;
CREATE TABLE t_department(
did INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(100) NOT NULL,
description VARCHAR(200),
manager_id INT
);
CREATE TABLE t_job (
job_id INT(11) PRIMARY KEY AUTO_INCREMENT,
job_name VARCHAR(100) DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL
);
CREATE TABLE t_employee(
eid INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(100) NOT NULL,
gender CHAR NOT NULL DEFAULT '男',
card_id CHAR(18) UNIQUE,
tel CHAR(11),
job_id INT,
mid INT,
birthday DATE,
hiredate DATE,
address VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES t_department(did),
FOREIGN KEY (job_id) REFERENCES t_job(job_id)
);
CREATE TABLE t_salary(
eid INT PRIMARY KEY,
basic_salary DECIMAL(10,2),
performance_salary DECIMAL(10,2),
commission_pct DECIMAL(10,2),
deduct_wages DECIMAL(10,2),
FOREIGN KEY (eid) REFERENCES t_employee(eid)
);
INSERT INTO t_department(dname,description) VALUES
('研发部','技术支持'),
('咨询部','产品咨询服务');
INSERT INTO t_job VALUES
(NULL,'JavaSE 工程师','Java 开发'),
(NULL,'Web 工程师','Web 开发'),
(NULL,'JavaEE 框架工程师','框架开发'),
(NULL,'产品顾问','产品咨询');
INSERT INTO t_employee(eid,ename,gender,card_id,tel,
job_id,mid,birthday,hiredate,address,dept_id) VALUES
(1,'孙红雷','男','123456789012345678','12345678901',1,NULL,'1990-01-01','2015-01-01','东北',1),
(2,'张亮','男','123456789012345677','12345678902',2,NULL,'1990-01-02','2015-01-02','北京',1),
(3,'鹿晗','男','123456789012345676','12345678903',3,NULL,'1990-01-03','2015-01-03','北京',1),
(4,'邓超','男','123456789012345675','12345678904',2,NULL,'1990-01-04','2015-01-04','北京',1),
(5,'孙俪','女','123456789012345674','12345678905',3,NULL,'1990-01-05','2015-01-05','北京',1),
(6,'Angelababy','女','123456789012345673','12345678906',4,NULL,'1990-01-06','2015-01-06','香港',2);
INSERT INTO t_salary(eid,basic_salary,performance_salary,commission_pct,deduct_wages)
VALUES
(1,'12000.00','6000.00','0.40','0.00'),
(2,'9000.00','5000.00','0.20',NULL),
(3,'11000.00','8000.00',NULL,NULL),
(4,'13000.00','5000.00',NULL,NULL),
(5,'8000.00','8000.00','0.30',NULL),
(6,'15000.00','6000.00',NULL,NULL);
SELECT * FROM t_department;
SELECT * FROM t_job;
SELECT * FROM t_employee;
SELECT * FROM t_salary;
UPDATE t_salary SET basic_salary=basic_salary*1.05;
UPDATE t_employee SET tel='13709098765',birthday='1982-09-26' WHERE ename='孙俪';
UPDATE t_employee SET hiredate=CURDATE() WHERE ename='邓超';
UPDATE t_department SET manager_id=6 WHERE did=1;
UPDATE t_department SET manager_id=1 WHERE did=1;
UPDATE t_department,t_employee
SET t_department.manager_id=t_employee.eid
WHERE t_department.dname='研发部'
AND t_employee.ename='孙红雷';
UPDATE t_employee,t_department
SET t_employee.mid=t_department.manager_id
WHERE t_employee.dept_id=t_department.did;
UPDATE t_department,t_employee
SET t_department.manager_id =t_employee.eid
WHERE t_department.dname='研发部'
AND t_employee.ename='邓超';
DELETE FROM t_salary WHERE eid=
(
SELECT eid FROM t_employee WHERE ename='Angelababy'
);
DELETE FROM t_employee WHERE eid=
(
SELECT eid FROM t_employee WHERE ename='Angelababy'
);
SELECT * FROM t_employee;
SELECT eid '编号',ename '姓名',gender '性别',card_id '身份证号码',tel '电话',
job_id '岗位编号',mid '主管编号',birthday '生日',hiredate '入职日期',
address '家庭住址',dept_id '部门编号' FROM t_employee;
SELECT ename AS '员工姓名', tel AS '手机号码' FROM t_employee;
SELECT * from t_employee WHERE ename='孙红雷' and dept_id =
(
SELECT did FROM t_department where dname='研发部'
);
SELECT dept_id FROM t_employee;
SELECT DISTINCT dept_id FROM t_employee;
SELECT * from t_employee WHERE eid in
(
SELECT eid FROM t_salary WHERE basic_salary>5000
);
SELECT * from t_employee WHERE eid in
(
SELECT eid FROM t_salary WHERE basic_salary=12600
);
SELECT * from t_employee WHERE eid in
(
SELECT eid FROM t_salary WHERE basic_salary!=12600
);
SELECT * from t_employee WHERE eid in
(
SELECT eid FROM t_salary WHERE commission_pct IS NULL
);
SELECT * FROM t_employee WHERE eid IN
(
SELECT eid FROM t_salary WHERE basic_salary BETWEEN 5000 AND 12000
);
SELECT * from t_employee WHERE eid in
(
SELECT eid FROM t_salary WHERE basic_salary NOT BETWEEN 5000 and 12000
);
SELECT * from t_employee WHERE dept_id=1 and eid in
(
SELECT eid FROM t_salary WHERE basic_salary>10000 and basic_salary<15000
);
SELECT DISTINCT job_id FROM t_employee;
SELECT CONCAT(eid,',',ename,',',gender,',',card_id,',',tel,',',job_id,',',birthday,',',hiredate,',',address,',',dept_id) 信息汇总 FROM t_employee;
SELECT * FROM t_department;
SELECT * FROM t_job;
SELECT * FROM t_employee;
SELECT * FROM t_salary;