当前位置:网站首页>View of MySQL

View of MySQL

2022-06-26 03:38:00 Fire eye Dragon

Concept :

  • View (view) It's a virtual table , It's not real , Its essence is to follow SQL Statement to get a dynamic data set , And clearly , When users use the view name, they can get the result set , And you can use it as a table .
  • The database only stores the definition of the view , There is no data stored in view . The data is stored in the original table .
  • When using views to query data , The database system will remove the corresponding data from the original table . therefore , The data in the view depends on the data in the original table . Once the data in the table changes , The data displayed in the view will also change .

effect :

  • Simplify the code , Reusable queries can be encapsulated into views for reuse , At the same time, it can make complex queries easy to understand and use .
  • Security reasons , If there are a lot of data in a table , A lot of information doesn't want everyone to see , You can use the view , Such as : Social security fund table , You can use the view to display only names , Address , Instead of showing the social security number and the number of wages , You can set different views for different users .

Method :
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE} ]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED |LOCAL] CHECK OPTION]

notes :
(1)algorithm: optional , Algorithm for view selection .
(2)view_name: Represents the name of the view to be created .
(3)column_list: optional , Specify the name of each attribute in the view , By default, it is the same as SELECT The properties of the query in the statement are the same .
(4)selcet_statement: Represents a complete query statement , Import the query record into the view .
(5)[with [cascaded |local ] check option]: optional , It means that when updating a view, it must be within the permission range of the view .

for example :
Create tables and data first

CREATE TABLE dept  (
  deptno int NOT NULL,
  dname varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  loc varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`deptno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO dept VALUES (10, ' Teaching and Research Department ', ' Beijing ');
INSERT INTO dept VALUES (20, ' Department of science and Engineering ', ' Shanghai ');
INSERT INTO dept VALUES (30, ' The sales department ', ' Guangzhou ');
INSERT INTO dept VALUES (40, ' Finance Department ', ' wuhan ');

CREATE TABLE emp  (
  empno int NOT NULL,
  ename varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  job varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  mgr int NULL DEFAULT NULL,
  hiredate date NULL DEFAULT NULL,
  sal decimal(7, 2) NULL DEFAULT NULL,
  COMM decimal(7, 2) NULL DEFAULT NULL,
  deptno int NULL DEFAULT NULL,
  PRIMARY KEY (empno) USING BTREE,
  INDEX fk_emp(mgr) USING BTREE,
  CONSTRAINT fk_emp FOREIGN KEY (mgr) REFERENCES emp (empno) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO emp VALUES (1001, ' Gan Ning ', ' Clerk ', 1013, '2000-12-17', 8000.00, NULL, 20);
INSERT INTO emp VALUES (1002, ' Daisy ', ' Salesperson ', 1006, '2001-02-20', 16000.00, 3000.00, 30);
INSERT INTO emp VALUES (1003, ' Yin Tianzheng ', ' Salesperson ', 1006, '2001-02-22', 12500.00, 5000.00, 30);
INSERT INTO emp VALUES (1004, ' Liu bei ', ' The manager ', 1009, '2001-04-02', 29750.00, NULL, 20);
INSERT INTO emp VALUES (1005, ' Thank sun ', ' Salesperson ', 1006, '2001-09-28', 12500.00, 14000.00, 30);
INSERT INTO emp VALUES (1006, ' Guan yu ', ' The manager ', 1009, '2001-05-01', 28500.00, NULL, 30);
INSERT INTO emp VALUES (1007, ' Zhang Fei ', ' The manager ', 1009, '2001-09-01', 24500.00, NULL, 10);
INSERT INTO emp VALUES (1008, ' Zhugeliang ', ' analysts ', 1004, '2007-04-19', 30000.00, NULL, 20);
INSERT INTO emp VALUES (1009, ' Zeng a Niu ', ' Chairman of the board of directors ', NULL, '2001-11-17', 50000.00, NULL, 10);
INSERT INTO emp VALUES (1010, ' Xiangr ', ' Salesperson ', 1006, '2001-09-08', 15000.00, 0.00, 30);
INSERT INTO emp VALUES (1011, ' Zhou Tai ', ' Clerk ', 1008, '2007-05-23', 11000.00, NULL, 20);
INSERT INTO emp VALUES (1012, ' Cheng pu ', ' Clerk ', 1006, '2001-12-03', 9500.00, NULL, 30);
INSERT INTO emp VALUES (1013, ' Pang Tong ', ' analysts ', 1004, '2001-12-03', 30000.00, NULL, 20);
INSERT INTO emp VALUES (1014, ' Huang Gai ', ' Clerk ', 1007, '2002-01-23', 13000.00, NULL, 10);

CREATE TABLE salgrade  (
  grade int NOT NULL,
  losal int NULL DEFAULT NULL,
  hisal int NULL DEFAULT NULL,
  PRIMARY KEY (grade) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO salgrade VALUES (1, 7000, 12000);
INSERT INTO salgrade VALUES (2, 12010, 14000);
INSERT INTO salgrade VALUES (3, 14010, 20000);
INSERT INTO salgrade VALUES (4, 20010, 30000);
INSERT INTO salgrade VALUES (5, 30010, 99990);

Start trying

CREATE OR REPLACE VIEW view_emp
AS
SELECT ename,job FROM emp;

SHOW TABLES;
SHOW FULL TABLES;

SELECT * FROM view_emp;

Modify the view

Concept : Modifying a view is to modify the definition of a table that already exists in the database . When some fields of the basic table change , You can modify the view to keep it consistent with the basic table .MySQL Pass through CREATE OR REPLACE VIEW Statement and ALTER VIEW Statement to modify the view .

Method :
ALTER VIEW View name AS SELECT sentence

for example :

ALTER VIEW view_emp
AS 
SELECT a.deptno,a.dname,a.loc,b.ename,b.sal FROM dept a,emp b WHERE a.deptno = b.deptno;

SELECT * FROM view_emp;

Update the view

Concept : Some views are updatable . in other words , Can be in UPDATE、DELETE or INSERT And so on , To update the contents of the base table . For updatable views , There must be a one-to-one concern between the rows in the view and the rows in the base table . If the view contains any of the following structures , So it's not updatable :

  • Aggregate functions (SUM(),MIN(),MAX(),COUNT() etc. )
  • DISTINCT
  • GROUP BY
  • HAVING
  • UNION or UNION ALL
  • Subqueries in the selection list
  • JOIN
  • FROM Non updatable view in Clause
  • WHERE A subquery of a clause , quote FROM Table in clause
  • Reference text values only ( In this case , There are no basic tables to update )

notes : Although the data can be updated in the view , But there are a lot of restrictions . In general , It's best to use the view as a virtual table for querying data , Instead of updating data through views . because , When using views to update data , If you don't fully consider the limitations of updating data in the view , Data update may fail .

for example :

CREATE OR REPLACE VIEW view1_emp
AS 
SELECT ename,job FROM emp;

SELECT * FROM view1_emp;

UPDATE view1_emp SET ename=' Zhou Yu ' WHERE ename =' Lu Su ';

--  This does not work , because emp The data inserted in is missing other values 
INSERT INTO view1_emp VALUES(' king of Wu in the Three Kingdoms Era ',' Clerk ');

--  The view contains aggregate functions that are not updatable 
CREATE OR REPLACE VIEW view2_emp
AS 
SELECT COUNT(*) FROM emp;

SELECT * FROM view2_emp;

INSERT INTO view2_emp VALUES(100);
UPDATE view2_emp SET cnt = 100;

--  The view contains distinct Not updatable 
CREATE OR REPLACE VIEW view3_emp
AS 
SELECT DISTINCT job FROM emp;

SELECT * FROM view3_emp;

INSERT INTO view3_emp VALUES(' financial ');

--  The view contains group by HAVING Not updatable 
CREATE OR REPLACE VIEW view4_emp
AS 
SELECT deptno,COUNT(*) cnt  FROM emp GROUP BY deptno HAVING cnt>2;

SELECT * FROM view4_emp;

INSERT INTO view4_emp VALUES(30,100);

--  The view contains UNION or HUNION ALL  Not updatable 
CREATE OR REPLACE VIEW view5_emp
AS 
SELECT empno,ename  FROM emp WHERE empno<= 1005
UNION
SELECT empno,ename  FROM emp WHERE empno> 1005;

SELECT * FROM view5_emp;

INSERT INTO view5_emp VALUES(1015,' Trinket ');

--  Subqueries in the selection list are not updatable 
CREATE OR REPLACE VIEW view6_emp
AS 
SELECT empno,ename,sal  FROM emp WHERE sal = (SELECT MAX(sal) FROM emp);

SELECT * FROM view6_emp;

INSERT INTO view6_emp VALUES(1015,' Trinket ',300000);

--  Two tables are used for comparison JOIN Not updatable 
CREATE OR REPLACE VIEW view7_emp
AS 
SELECT dname,ename,sal  FROM emp a JOIN dept b on a.deptno=b.deptno; 

SELECT * FROM view7_emp;

INSERT INTO view7_emp(dname,ename,sal) VALUES(' The administration department ',' Trinket ',300000);

-- FROM Non updatable in clause 
CREATE OR REPLACE VIEW view8_emp
AS 
SELECT ' The administration department ' dname ,' Yang2 guo4 ' ename;

SELECT * FROM view8_emp;

INSERT INTO view8_emp VALUES(' The administration department ',' Trinket ');



Other operating

Rename view

Method :RENAME TABLE View name TO New view name ;

Delete view

Method :DROP VIEW View name [, View name …];

notes : When deleting a view , You can only delete the definition of a view , Data will not be deleted .


--  Rename view 
RENAME TABLE view1_emp to myview1;

--  Delete view 
DROP VIEW IF EXISTS myview1;

原网站

版权声明
本文为[Fire eye Dragon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260310240267.html