MySQL study notes. The use of views

View concept

The view is derived from one or more tables. The behavior of the view is very similar to that of the table, but the view is a virtual table. In the view can also be like a table, you can use INSERT, UPDATE, DELETE.
A virtual table is a table derived from one or several basic tables (or views).
Only the definition of the view is stored in the database, and the data corresponding to the view is not stored.
The data in the base table changes, and the data queried from the view also changes.

View function

1.
Simplification The view can not only simplify the user's understanding of the data, but also simplify the operation of the data.
2. Security
Through the view, users can only query and modify the data they can see, but other data in the database cannot As you can see, it cannot be modified. The database authorization command allows each user to restrict database retrieval to specific database objects. Through the view, users can be restricted to different subsets of the data, which greatly enhances the security of the database.
3. Logical data independence.
Views can help users shield the impact of changes in the real table structure.

Create view

Grammatical format

create view 视图名
AS
select 
from 
where 

Prepare the data
Here are three tables created, and the data has been added.
Insert picture description here
Create a view with gender as male

create view view_male
AS
select *
from student
where sex='male';

Insert picture description here
Here you will find that a view with only male data has been created, which effectively guarantees the security of the data

View view

The method is similar to that of viewing the table. SELECT * FROM view name;

Update view

Updating a view refers to inserting, updating, and deleting data in the table through the view. Because the view is a virtual table, there is no data in it. When the view is updated, it is transferred to the basic table for update operations, so if the view is The operation of deleting records is added, and the same operation is actually performed on the basic table.
1. Modification operation
Here, modify the record named "zhangsan" to "张三"

update view_male
set sname='张三'
where sname='zhangsan';
select * from view_male;

Insert picture description here
You will find that "zhangsan" has become "Zhangsan".
After querying the basic table, I
Insert picture description here
found that the data has been updated.
2. Insertion operation
Here is an example of inserting such a piece of data:'2017001001','刘备','male',30,'IS'

insert into view_male values('2017001001','刘备','male',30,'IS');

Insert picture description here
A record has been added.
3. Delete operation

DELETE from view_male where sno="2017001001";

Insert picture description here
OK, it has been deleted successfully.

Multi-table view

1. Two-table view
Establish a view of students who have taken course No. 1 (including student ID, name, and grades)

create view view_c001(sno,sname,grade)
AS
select student.sno,sname,grade
from student,sc
where student.sno=sc.sno and cno='c001';
SELECT * from view_c001;

Insert picture description here
2. Three-table view
Establish a view, select the student view of the data structure (student number, name, course name, grade)

create view view_sg
as
select student.sno,sname,cname,grade
from student,course,sc
where student.sno=sc.sno and course.cno=sc.cno and cname='数据结构';

Insert picture description here
Of course, you can also create a view in the view.
Example: Create view_003 in view: view_sg, that is, create a view with student ID less than 2018001003.

create view view_003
as
select *
from view_sg
where sno<'2018001003';
SELECT * from view_003;

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/109579803