1. 程式人生 > 實用技巧 >MYSQL 第十三課 檢視

MYSQL 第十三課 檢視

#檢視

/*
含義:虛擬表,和普通表一樣使用
mysql5.1版本出現的新特性,是通過表動態生成的資料

比如:舞蹈班和普通班級的對比

建立語法的關鍵字 是否實際佔用物理空間 使用

檢視 create view 只是儲存了sql邏輯,沒有結果 增刪改查,只是一般不能增刪改

表 create table 儲存了資料 增刪改查
*/

#案例:查詢姓張的學生名和專業名
SELECT stuname,majorname
FROM stuinfo s
INNER JOIN major m ON s.`majorid`= m.`id`
WHERE s.`stuname` LIKE '張%';

CREATE VIEW v1
AS
SELECT stuname,majorname
FROM stuinfo s
INNER JOIN major m ON s.`majorid`= m.`id`; #封裝了

SELECT * FROM v1 WHERE stuname LIKE '張%'; # 以後只執行這句ok


#一、建立檢視
/*
語法:
create view 檢視名
as
查詢語句;
*/
USE myemployees;

#1.查詢姓名中包含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%';

#2.查詢各部門的平均工資級別

#①建立檢視檢視每個部門的平均工資

CREATE VIEW myv2
AS
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id;

#②使用

SELECT
myv2.`ag`,g.grade_level FROM myv2 JOIN job_grades g ON myv2.`ag` BETWEEN g.`lowest_sal` AND g.`highest_sal`;

#3.查詢平均工資最低的部門資訊

SELECT * FROM myv2 ORDER BY ag LIMIT 1;

#4.查詢平均工資最低的部門名和工資

CREATE VIEW myv3
AS
SELECT * FROM myv2 ORDER BY ag LIMIT 1;
SELECT d.*,m.ag
FROM myv3 m
JOIN departments d
ON m.`department_id`=d.`department_id`;

#二、檢視的修改

#方式一:
/*
create or replace view 檢視名
as
查詢語句;
*/
SELECT * FROM myv3

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

#方式二:
/*
語法:
alter view 檢視名
as
查詢語句;
*/
ALTER VIEW myv3
AS
SELECT * FROM employees;

#三、刪除檢視

/*
語法:drop view 檢視名,檢視名,...;
*/

DROP VIEW emp_v1,emp_v2,myv3;

#四、檢視檢視

DESC myv3; #1

SHOW CREATE VIEW myv3; # 2

#五、檢視的更新

CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email,salary*12*(1+IFNULL(commission_pct,0)) "annual salary"
FROM employees;              #不能更新
CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email
FROM employees;             #可以更新

SELECT * FROM myv1;
SELECT * FROM employees;
#1.插入
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 myv1
AS
SELECT MAX(salary) m,department_id
FROM employees
GROUP BY department_id;
#更新
SELECT * FROM myv1;
UPDATE myv1 SET m=9000 WHERE department_id=10;

#②常量檢視 不能更新

CREATE OR REPLACE VIEW myv2
AS
SELECT 'john' NAME;
#更新
SELECT * FROM myv2;
UPDATE myv2 SET NAME='lucy';

#③Select中包含子查詢 不能更新

CREATE OR REPLACE VIEW myv3
AS
SELECT department_id,(SELECT MAX(salary) FROM employees) 最高工資
FROM departments;
#更新
SELECT * FROM myv3;
UPDATE myv3 SET 最高工資=100000;

#④join 不能更新

CREATE OR REPLACE VIEW myv4
AS
SELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;
#更新
SELECT * FROM myv4;
UPDATE myv4 SET last_name = '張飛' WHERE last_name='Whalen'; #沒有報錯
INSERT INTO myv4 VALUES('陳真','xxxx');

#⑤from一個不能更新的檢視 不能更新

CREATE OR REPLACE VIEW myv5
AS
SELECT * FROM myv3;
#更新
SELECT * FROM myv5;
UPDATE myv5 SET 最高工資=10000 WHERE department_id=60;

#⑥where子句的子查詢引用了from子句中的表 不能更新

CREATE OR REPLACE VIEW myv6
AS
SELECT last_name,email,salary
FROM employees
WHERE employee_id IN(
SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL
);
#更新
SELECT * FROM myv6;
UPDATE myv6 SET salary=10000 WHERE last_name = 'k_ing';

#一、建立檢視emp_v1,要求查詢電話號碼以‘011’開頭的員工姓名和工資、郵箱

CREATE OR REPLACE VIEW emp_v1
AS
SELECT last_name,salary,email
FROM employees
WHERE phone_number LIKE '011%';

#二、建立檢視emp_v2,要求查詢部門的最高工資高於12000的部門資訊

CREATE OR REPLACE VIEW emp_v2
AS
SELECT MAX(salary) mx_dep,department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary)>12000;


SELECT d.*,m.mx_dep
FROM departments d
JOIN emp_v2 m
ON m.department_id = d.`department_id`;