含义:虚拟表,和普通表一样使用,
mysql5.1开始出现的新版本,通过普通表动态生成的数据,
行和列的数据来自定义视图的查询中使用的表,
只保存了SQL逻辑,不保存查询结果
特点:①临时性②重复使用性
优点:
1、重用SQL语句
2、简化复杂的SQL操作,不必知道它的查询细节
3、保护数据,提高安全性
视图和表的对比
创建的关键字 | 是否占用实际的物理空间 | 使用 | |
视图 | create view | 只保存sql逻辑 | 增删改查,一般不进行这样操作 |
表 | create table | 保存数据 | 增删改查 |
案例:查询姓张的学生名和专业名
USE student;
SELECT stuname,majorname
FROM stuinfo s
JOIN major m ON s.majorid=m.id
WHERE stuname='张%';
CREATE VIEW tab_s_m AS
SELECT studentname,majorname
FROM student s
JOIN major m ON s.majorid=m.majorid;
SELECT * FROM tab_s_m
WHERE studentname LIKE '张%';
语法:
CREATE VIEW 视图名
AS
查询语句;案例1.查询姓名中包含a字符的员工名、部门名和工种信息
USE myemployees;
DROP VIEW ainfo;
CREATE VIEW ainfo
AS
SELECT last_name,department_name,j.*
FROM employees e
JOIN departments d ON e.department_id=d.department_id
JOIN jobs j ON e.job_id=j.job_id;
SELECT * FROM ainfo
WHERE last_name LIKE '%a%';
案例2.查询各部门的平均工资级别
CREATE VIEW avgs_table
AS
SELECT AVG(Salary) avgs,department_id
FROM employees
GROUP BY department_id;
SELECT department_id,grade_level
FROM avgs_table
JOIN job_grades ON avgs_table.avgs BETWEEN lowest_sal AND highest_sal
案例3.查询平均工资最低的部门信息
SELECT *
FROM avgs_table
ORDER BY avgs ASC
LIMIT 1;
案例4.查询平均工资最低的部门名和工资
SELECT d.*,ag.avgs
FROM departments d
JOIN avgs_table ag ON ag.department_id=d.department_id
WHERE d.department_id=(
SELECT department_id
FROM avgs_table
ORDER BY avgs ASC
LIMIT 1
);
方式一:
语法:
CREATE OR REPLACE VIEW 视图名
AS
查询语句;
方式二:
语法:
ALTER VIEW 视图名
AS
查询语句;
DROP VIEW 视图名,视图名......
DROP VIEW emp_v1,emp_v2,test_v1;
CREATE VIEW test_v1
AS
SELECT * FROM employees;
DESC test_v1;
SHOW CREATE VIEW test_v1;
1、创建视图 emp_v1,要求查询电话号码以‘011’开头的员工姓名和工资、邮箱
CREATE VIEW emp_v1
AS
SELECT phone_number,last_name,Salary,email
FROM employees;
SELECT *
FROM emp_v1
WHERE phone_number LIKE '011%';
2、要求将视图 emp_v1 修改为查询电话号码以‘011’开头的并且邮箱中包含 e 字符的员工
姓名和邮箱、电话号码
ALTER VIEW emp_v1
AS
SELECT last_name,email,phone_number
FROM employees
WHERE phone_number LIKE '011%'
AND email LIKE '%e%';
SELECT *
FROM emp_v1;
CREATE OR REPLACE VIEW emp_v1
AS
SELECT last_name,email,phone_number
FROM employees
WHERE phone_number LIKE '011%'
AND email LIKE '%e%';
SELECT *
FROM emp_v1;
6、创建视图 emp_v2,要求查询部门的最高工资高于 12000 的部门信息
CREATE OR REPLACE VIEW emp_v2
AS
SELECT d.*
FROM employees e
JOIN departments d ON e.department_id=d.department_id
GROUP BY e.department_id
HAVING MAX(e.salary)>12000;
SELECT * FROM emp_v2;
五、视图的更新
CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email
FROM employees;
1、插入数据
SELECT * FROM myv1;
SELECT * FROM employees;
INSERT INTO myv1 VALUES('张飞','[email protected]');#对视图插入数据同时将数据插入到原始表中
2、修改数据
UPDATE myv1 SET last_name='张无忌' WHERE last_name='张飞';
3、删除数据
DELETE FROM myv1 WHERE last_name='张无忌';
视图的可更新性和视图中查询的定义有关系,以下类型的视图是不能更新的。
• 包含以下关键字的SQL语句:分组函数、DISTINCT、GROUP BY、HAVING、UNION或者UNION ALL
CREATE OR REPLACE VIEW test_v1
AS
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id;
SELECT * FROM test_v1;
INSERT INTO test_v1 VALUES(10000,120);
•常量视图
CREATE OR REPLACE VIEW test_v2
AS
SELECT 'john' NAME;
SELECT * FROM test_v2;
UPDATE test_v2 SET NAME='little baby' WHERE NAME='john';
• SELECT中包含子查询
CREATE OR REPLACE VIEW test_v3
AS
SELECT (SELECT MAX(Salary) FROM employees) 最高工资;
SELECT * FROM test_v3;
UPDATE test_v3 SET 最高工资=12000;
• JOIN
CREATE OR REPLACE VIEW test_v4
AS
SELECT last_name,department_name,j.*
FROM employees e
JOIN departments d ON e.department_id=d.department_id
JOIN jobs j ON e.job_id=j.job_id;
SELECT * FROM test_v4 WHERE last_name='little baby';
UPDATE test_v4 SET department_name='little baby' WHERE job_id='AC_MGR';#join可以update
INSERT INTO test_v4(last_name) VALUES('little baby'); #执行正常,但结果未插入
• FROM一个不能更新的视图
CREATE OR REPLACE VIEW test_v5
AS
SELECT * FROM test_v3
SELECT * FROM test_v5;
UPDATE test_v5 SET 最高工资=12000;
• WHERE子句的子查询引用了FROM子句中的表
CREATE OR REPLACE VIEW test_v6
AS
SELECT last_name,UPDATE test_v3 SET 最高工资=12000;
FROM employees e1
WHERE e1.employee_id IN (
SELECT e2.manager_id
FROM employees e2
);
SELECT * FROM test_v6;
INSERT INTO test_v6 VALUES('ll',2000);#可以
DELETE FROM test_v6 WHERE Last_name='ll';#不可以
UPDATE test_v6 SET last_name='xwg' WHERE last_name='ll';#不可以