Views of mainstream databases

Article directory

content

Article directory

foreword

view

View overview

Create a view

Format

Pay attention to the problem:

Example

How to view a view

DESCRIBE statement to view the basic information of the view

SHOW TABLES statement to view the basic information of the view

Query the information schema.views table, you can view the detailed information of all views in the database

Modify view

The CREATE OR REPLACE VIEW statement modifies the view

Use the ALTER statement to modify the view

View manipulation

Update view using SQL statement

The view is automatically updated after the basic table data is added

delete data in view

delete view


foreword

Hello everyone, I am the color of ice three points.

Personal homepage: blog of ice three colors

This article talks about the knowledge of views, creating, viewing, modifying, deleting, etc.

Friends passing by, please like and follow before walking. Welcome to the comment area to communicate. It is never too late to start working hard, so it is better to start with this article!

Let's grow together! refill


view

View overview

1. A view is a virtual table that stores a specified query statement. The data in the view comes from the table referenced by the defined view, and can achieve dynamic reference, that is, when the data in the table changes, the data in the view changes accordingly. (In essence, it is a query statement, a query statement with a name. Generally, we write a common query statement in the query, and after the query is turned off, it will disappear. But it can be saved as a view)

2. A view is a database object. It is a virtual table derived from one or more data tables or views (a view can also be queried from a view). In the database, there is only the definition of the view, but it is not stored in the view. The data, because the view does not have real data, the real data is still in the table.

3. Browse the row and column data of the corresponding data to customize the table referenced by the view query, and dynamically generate it when referencing the view. Views can be used to query and modify base table data . (Can't directly insert operation)

4. Views make it more convenient for database users, including:

Simplify data query and processing. Views can centralize data in multiple tables for users, simplifying users' query and processing of data.

Mask the complexity of the database. Changes to database tables do not affect the use of the database by users, and users do not have to understand the complex table structure in the database. (For a view that defines several table connections, the connection operation between tables is hidden from the user.)

safety. If the user can only query or modify the data that the user has permission to access, you can only grant the user the permission to access the view, but not the permission to access the table, so as to improve the security of the database.

Create a view

Format

Views can be built on one table, or on multiple tables or existing views. The format is as follows:

CREATE [OR REPLACE] VIEW view name [(column_list)]AS select query statement

[ WITH [ CASCADED|LOCAL] CHECK OPTION] ;其中:

REPLACE means to replace the view that has been created; WITH[CASCADED|LOCAL]CHECK OPTION means that the view is guaranteed to be within the permission scope of the view when it is updated. CHECK OPTION is also called a mandatory view and is not recommended. CASCADED is the default value, which means that the conditions of all related views and tables must be satisfied when updating the view; LOCAL means that the conditions defined by the view itself can be satisfied when updating the view, but it is generally not allowed.

The view belongs to the database. By default, the view will be created in the current database. If you want to see a common view in a given database, you should specify the name as the name of the library. The name of the view.

Creating a view requires the user to have the CREATE VIEW privilege on the view and the SELECT privilege on the columns of the query design.

Example: query the user table to obtain permission information through the SELECT statement (where Select priv indicates whether the user has the SELECT permission, and Create_view indicates whether the user has the CREATE VIEW permission)

SELECT Select_priv,Create_view_priv

FROM mysql.user WHERE user='root';

Pay attention to the problem:

Note when creating views using the CREATE VIEW statement:

1. Subqueries cannot be used in the from clause of a view.

2. System or user variables cannot be referenced in the view's select statement.

3. Prepared statement parameters cannot be referenced in the select statement of the view.

4. Order by is allowed in view definitions, however, if a selection is made from a particular view that uses a statement with its own order by, it will be ignored.

5. The table or view referenced in the definition must exist. The existence of a view definition can be checked using the check table statement.

6. The temporary table cannot be referenced in the definition, and the temporary view cannot be created.

7. You cannot associate a trigger with a view.

Example

Create a basic employee information view that includes employee ID, name, job title, and department ID, sorted by employee ID in ascending order

CREATE VIEW v_emp_base

AS SELECT empno,ename,job,deptno FROM employee ORDER BY empno;

Create a view that includes multi-table joins and grouped queries (a view's SELECT query can contain functions, data groups, or computed data, or it can fetch data from multiple tables.)

CREATE OR REPLACE VIEW v_dept_sal(name,minsal,maxsal,avgsal)

AS

SELECT d.dname,MIN(e.sal),MAX(e.sal),AVG(e.sal)

FROM department d,employee e

WHERE d.deptno = e.deptno GROUP BY d.dname;

query this view

SELECT *from v_dept_sal;

[Example] Create a view of the annual salary information of employees whose salary is greater than 2000 (if the SELECT clause in the view contains limited conditions, you can use the WITHCHECK OPTION option when creating the view.)

-- Indicates that when updating the view, the record data must meet the condition that the salary is greater than 2000

CREATE VIEW v_emp_salary

AS

SELECT empno,ename,sal*12 salary

FROM employee

WHERE sal>2000 WITH CHECK OPTION;

How to view a view

describe, show tables, show table status, show create view statement, query the views table under the information_schema database, etc.

DESCRIBE statement to view the basic information of the view

DESCRIBE | DESC view name;

The demo is as follows:

DESCRIBE v_emp_salary;

SHOW TABLES statement to view the basic information of the view

Starting from MySQL 5.1, not only the name of the table, but also the name of the view will be displayed when the SHOW TABLES statement is executed.

SHOW TABLES;

The demo is as follows:

show tables;

Query the information schema.views table, you can view the detailed information of all views in the database

In the MySQL database, the definitions of all views are stored in the views table under the information_schema database.

SELECT * FROM information_schema.views WHERE table name = 'view name';

Example: Query v_emp_salary view definition

SELECT * FROM information_schema.views WHERE table_name='v_emp_salary';

Modify view

Modify the view that exists in the database. When some fields of the basic table are changed, you can modify the view to keep the view consistent with the basic table.

Views are modified by CREATE OR REPLACE and ALTER statements in MySQL

The CREATE OR REPLACE VIEW statement modifies the view

CREATE OR REPLACE VIEW view name [[(field list)]As select query statement

[ WITH [ CASCADED|LOCAL] CHECK OPTION] ;

Create or replace view vw_dept

AS

Select *from dept;

View view

When the view already exists, the modify statement modifies the view; when the view does not exist, the view is created.

Use the ALTER statement to modify the view

ALTER VIEW view name[(field list)]As select query statement

[ WITH [ CASCADED|LOCAL] CHECK OPTION];

Modify the v_emp_base view to change the deptno column to the sal column display

ALTER VIEW v_emp_base

AS SELECT empno,ename,job,sal FROM employee ORDER BY empno;

View manipulation

DML operations (INSERT, UPDATE, and DELETE) that modify data can also be performed on views. Because views are "virtual tables", operations on views eventually translate to operations on base tables.

DML operations on views have the following restrictions:

1. If a view depends on multiple base tables, only the data of one base table can be modified at a time, and the data of multiple base tables cannot be modified at the same time;

2. If the modification violates the constraints of the base table or the with check option of the view, the view cannot be updated;

3. If the view contains join operators, DISTINCT keywords, set operators (UNION), aggregate functions (COUNT, SUM, MAX, etc.), GROUP BY, ORDER BY, HAVING clauses, the view will not be updated;

4. If the view contains a subquery of the SELECT column, a subquery in the WHERE clause, and a subquery in the FROM clause, the view cannot be updated.

Update view using SQL statement

Update base table data through update operations on views

--Create empview table based on employee table

CREATE TABLE empview AS SELECT * FROM employee;

--Create view v_emp_update

CREATE OR REPLACE VIEW v_emp_update

AS SELECT empno,ename,sal,deptno FROM empview WHERE deptno=20 WITH CHECK OPTION;

--Operation view v_emp_update update data

UPDATE v_emp_update SET sal=1000 WHERE ename='SMITH';

--View the changes in the data in the basic table empview

SELECT empno,ename,sal,deptn o FROM empview WHERE ename='SMITH';

The final result is as follows:

The view is automatically updated after the basic table data is added

Insert data into the basic table, and the view data is updated accordingly--insert a record into the basic table empview

INSERT INTO empview VALUES(7777,'TEST','CLERK',7902,2020-12-17,800,NULL,20);

--Query the data changes of the view v_emp_update

SELECT * FROMv_emp_update;

delete data in view

Use the DELETE operation view to update the basic table data--delete the data in the view

DELETE FROM v_emp_update WHERE empno=7777;

--Query the changes in the data in the basic table

SELECT* FROM empview;

delete view

When you delete a view, you can only delete the definition of the view, not the data.

In a MySQL database, a user must have the drop privilege to use the drop view statement to drop a view.

The drop view command can delete multiple views, and separate view names with commas.

DROP VIEW [if exists] view name [, view name]

delete view v_emp_update

DROP VIEW IF EXISTS v_emp_update;

no view

Guess you like

Origin blog.csdn.net/qq_46007633/article/details/124112403