11.创建和管理表
(1)创建表dept1
name Null? type
id int(7)
name varchar(25)
CREATE TABLE dept1(
id INT(7),
NAME VARCHAR(25)
);
(2)将表 departments 中的数据插入新表 dept2 中
CREATE TABLE dept2 SELECT * FROM departments;
(3)创建表 emp5
name Null? type
id int(7)
First_name Varchar (25)
Last_name Varchar(25)
Dept_id int(7)
CREATE TABLE emp5(
id INT(7),
first_name VARCHAR(25),
last_name VARCHAR(25),
dept_id INT(7)
);
(4)将列 Last_name 的长度增加到 50
ALTER TABLE emp5 MODIFY COLUMN last_name VARCHAR(50);
(5)根据表 employees 创建 employees2
CREATE TABLE employees2 SELECT * FROM employees;
(6)删除表 emp5
DROP TABLE emp5;
(7)将表 employees2 重命名为 emp5
ALTER TABLE employees2 RENAME emp5;
(8)在表 dept 和 emp5 中添加新列 test_column,并检查所作的操作
ALTER TABLE dept1 ADD COLUMN test_column VARCHAR(25);
ALTER TABLE emp5 ADD COLUMN test_column VARCHAR(25);
SELECT * FROM dept1;
SELECT * FROM emp5;
(9)直接删除表 emp5 中的列 dept_id
ALTER TABLE emp5 DROP COLUMN department_id;
12.约束
(1)向表 emp2 的 id 列中添加 PRIMARY KEY 约束(my_emp_id_pk)
ALTER TABLE emp2 ADD CONSTRAINT my_emp_id_pk PRIMARY KEY(id);
(2)向表 dept2 的 id 列中添加 PRIMARY KEY 约束(my_dept_id_pk)
ALTER TABLE dept2 ADD CONSTRAINT my_dept_id_pk PRIMARY KEY(id);
(3)向表 emp2 中添加列 dept_id,并在其中定义 FOREIGN KEY 约束,与之相关联的列是
dept2 表中的 id 列。
ALTER TABLE emp2 ADD COLUMN dept_id INT;
ALTER TABLE emp2 ADD CONSTRAINT fk_emp2_dept2 FOREIGN KEY(dept_id) REFERENCES dept2(id);
13.事务
(1)创建一个表,里面有 id 为主键,stuname 唯一键,seat 座位号,要求将 id 设置成自增
(2)要求用事务的方式插入 3 行数据
(3)要求用事务的方式删除数据,并回滚
14.视图
(1)创建视图 emp_v1,要求查询电话号码以‘011’开头的员工姓名和工资、邮箱
CREATE OR REPLACE VIEW emp_v1
AS
SELECT last_name, salary, email
FROM employees
WHERE phone_number LIKE "011%";
(2)要求将视图 emp_v1 修改为查询电话号码以‘011’开头的并且邮箱中包含 e 字符的员
工姓名和邮箱、电话号码
CREATE OR REPLACE VIEW emp_v1
AS
SELECT last_name, email, phone_number
FROM employees
WHERE phone_number LIKE "011%" AND email LIKE "%e%";
(3)向 emp_v1 插入一条记录,是否可以?
可以,插入的记录会插到真实的表中
INSERT INTO emp_v1 VALUES('Trump', 'TTT', '011.44.465.4555');
但在以下几种情况不能插入记录
1.包含分组函数、group by、distinct、having、union、
2.join
3.常量视图
4.where后的子查询用到了from中的表
5.用到了不可更新的视图
(4)创建视图 emp_v2,要求查询部门的最高工资高于 12000 的部门信息
CREATE OR REPLACE VIEW emp_v2
AS
SELECT MAX(salary), department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) > 12000;
(5)向 emp_v2 中插入一条记录,是否可以?
不可以
(6)删除刚才的 emp_v2 和 emp_v1
DROP VIEW emp_v1, emp_v2;
15.存储过程
(1)创建存储过程或函数实现传入用户名和密码,插入到 admin 表中
CREATE PROCEDURE test_pro1(IN username VARCHAR(20),IN loginPwd VARCHAR(20))
BEGIN
INSERT INTO admin(admin.username,PASSWORD)
VALUES(username,loginpwd);
END $
(2)创建存储过程或函数实现传入女神编号,返回女神名称和女神电话
CREATE PROCEDURE test_pro2(IN id INT,OUT NAME VARCHAR(20),OUT phone VARCHAR(20))
BEGIN
SELECT b.name ,b.phone INTO NAME,phone
FROM beauty b
WHERE b.id = id;
END $
(3)创建存储存储过程或函数实现输入两个女神生日,返回大小
CREATE PROCEDURE test_pro3(IN birth1 DATETIME,IN birth2 DATETIME,OUT result INT)
BEGIN
SELECT DATEDIFF(birth1,birth2) INTO result;
END $
(4)创建存储过程或函数实现传入一个日期,格式化成 xx 年 xx 月 xx 日并返回
CREATE PROCEDURE test_pro4(IN mydate DATETIME,OUT strDate VARCHAR(50))
BEGIN
SELECT DATE_FORMAT(mydate,'%y年%m月%d日') INTO strDate;
END $
CALL test_pro4(NOW(),@str)$
SELECT @str $
(5)创建存储过程或函数实现传入女神名称,返回:女神 and 男神 格式的字符串。如 传入 :小昭,返回: 小昭 and 张无忌
DROP PROCEDURE test_pro5 $
CREATE PROCEDURE test_pro5(IN beautyName VARCHAR(20),OUT str VARCHAR(50))
BEGIN
SELECT CONCAT(beautyName,' and ',IFNULL(boyName,'null')) INTO str
FROM boys bo
RIGHT JOIN beauty b ON b.boyfriend_id = bo.id
WHERE b.name=beautyName;
SET str=
END $
CALL test_pro5('柳岩',@str)$
SELECT @str $
(6)创建存储过程或函数,根据传入的条目数和起始索引,查询 beauty 表的记录
DROP PROCEDURE test_pro6$
CREATE PROCEDURE test_pro6(IN startIndex INT,IN size INT)
BEGIN
SELECT * FROM beauty LIMIT startIndex,size;
END $
CALL test_pro6(3,5)$
16.函数
(1)创建函数,实现传入两个 float,返回二者之和
CREATE FUNCTION test_fun1(num1 FLOAT,num2 FLOAT) RETURNS FLOAT
BEGIN
DECLARE SUM FLOAT DEFAULT 0;
SET SUM=num1+num2;
RETURN SUM;
END $
SELECT test_fun1(1,2)$
17.流程控制结构
(1)已知表 stringcontent
其中字段:
id 自增长
content varchar(20)
向该表插入指定个数的,随机的字符串
DROP TABLE IF EXISTS stringcontent;
CREATE TABLE stringcontent(
id INT PRIMARY KEY AUTO_INCREMENT,
content VARCHAR(20)
);
DELIMITER $
CREATE PROCEDURE test_randstr_insert(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE str VARCHAR(26) DEFAULT 'abcdefghijklmnopqrstuvwxyz';
DECLARE startIndex INT;#代表初始索引
DECLARE len INT;#代表截取的字符长度
WHILE i<=insertcount DO
SET startIndex=FLOOR(RAND()*26+1);#代表初始索引,随机范围1-26
SET len=FLOOR(RAND()*(20-startIndex+1)+1);#代表截取长度,随机范围1-(20-startIndex+1)
INSERT INTO stringcontent(content) VALUES(SUBSTR(str,startIndex,len));
SET i=i+1;
END WHILE;
END $
CALL test_randstr_insert(10)$
18.子查询经典题目
(1)查询工资最低的员工信息: last_name, salary
SELECT last_name, salary
FROM employees
WHERE salary = (
SELECT MIN(salary)
FROM employees
);
(2)查询平均工资最低的部门信息
-- 方法1
-- 1.查询各部门的平均工资
SELECT AVG(salary)ag, department_id
FROM employees
GROUP BY department_id;
-- 2.在1结果上查询最低平均工资
SELECT MIN(ag)
FROM (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id
) ag_dep
;
-- 3.查询哪个部门的平均工资 = 2.
SELECT AVG(salary), department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(ag)
FROM (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id
) ag_dep
);
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(ag)
FROM (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id
) ag_dep
)
)
-- 方法2
-- 1.查询各部门的平均工资
SELECT AVG(salary)ag, department_id
FROM employees
GROUP BY department_id;
-- 2.求出最低平均工资的部门编号
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1;
-- 3.查询部门信息
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1
);
(3)查询平均工资最低的部门信息和该部门的平均工资
-- 1.查询各部门的平均工资
SELECT AVG(salary)ag, department_id
FROM employees
GROUP BY department_id;
-- 2.求出最低平均工资的部门编号
SELECT AVG(salary), department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1;
-- 3.查询部门信息
SELECT d.*, ag
FROM departments d
JOIN (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1
) ag_dep
ON d.`department_id` = ag_dep.department_id;
(4)查询平均工资最高的 job 信息
-- 1.
SELECT AVG(salary), job_id
FROM employees
GROUP BY job_id
ORDER BY AVG(salary) DESC
LIMIT 1;
-- 2.
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
ORDER BY AVG(salary) DESC
LIMIT 1
);
(5)查询平均工资高于公司平均工资的部门有哪些?
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) > (
SELECT AVG(salary) FROM employees
)
;
(6)查询出公司中所有 manager 的详细信息
SELECT *
FROM employees
WHERE employee_id = ANY (
SELECT DISTINCT manager_id
FROM employees
);
(7)各个部门中 最高工资中最低的那个部门的 最低工资是多少
SELECT MIN(salary)
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY MAX(salary)
LIMIT 1
);
(8)查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email,
salary
SELECT last_name, department_id, email, salary
FROM employees
WHERE employee_id = (
SELECT DISTINCT manager_id -- 查询manage_id
FROM employees
WHERE department_id = (
SELECT department_id -- 查询部门id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1
) AND manager_id IS NOT NULL
);