mysql笔记(十) 视图

视图:虚拟表,,和普通表一样使用。mysql 5.1版本出现的新特性,是通过表动态生成的数据

一、创建视图

语法:

create view 视图名

as

查询语句;

eg:查询姓名中包含a字符的员工名、部门名和公种信息
#创建视图
CREATE VIEW myv1
AS
SELECT last_name,department_name,job_title
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON j.job_id = e.job_id;
#使用
SELECT * FROM myv1 WHERE last_name LIKE '%a%';

二、修改视图

方式一: create or replace view 视图名 as 查询语句;

eg:CREATE OR REPLACE VIEW myv3
AS 
SELECT avg(salary),job_id
FROM employees
GROUP BY job_id;

方式二:alter view 视图名 as 查询语句

eg:

ALTER VIEW myv3
AS
SELECT * FROM employees;

三、删除视图

语法:drop view 视图名,视图名,...;


DROP VIEW myv1,myv3;

四、查看视图

DESC myv3;
SHOW CREATE VIEW myv3;

五、视图的更新

1.插入

INSERT INTO myv1 VALUES('zhangfei','[email protected]');

2.修改

UPDATE myv1 SET last_name='mmmmmm' WHERE last_name='zhangfei';
3.删除

DELETE FROM myv1 WHERE last_name='mmmmmm';

mysql笔记(十) 视图_第1张图片

 

你可能感兴趣的:(mysql)