SQL views and triggers

Source: Internet
Author: User

View (virtual table)

The result set queried by select can be used as an alias as a virtual table

Views can only be added using cannot add modifications

Views cannot be built on other views, only one other entity table as the basis

Depending on the chart, the data will change with the entity table.


The role of the view:

Skip the process of writing table links

Two three tables can be linked together to make the query more visual.

/*============================ Code Demo =========================*/

--Code Creation view

CREATE VIEW SHITU2
As
Select *from Score,grade where score.degree between low and UPP
Go
Select *from shitu2 --Query the data inside the view

--Modify View
Alter VIEW Shitu--Modifying the view is equivalent to creating this
As
Select *from Student
Go

--Delete View
Drop View Shitu

--Triggers---Special stored procedures, which are automatically triggered by adding or deleting database tables.
Create Trigger Tr_student_insert
On student--Create a trigger on that table
For insert--insect is added meaning
As
Begin
Select *from Student
End
Go

INSERT into Student values (303, ' Yang ', ' Male ', ' 19901115 ', ' 95033 ')--Automatically triggers select *from Student query statements when adding data to the table

Create Trigger Tr_student_delete
On student
instead of delete --delete is the meaning of the deletion
As
Begin
print ' cannot be deleted '
End
Go
Delete from Student --Delete the Student table (not deleted due to the cause of the trigger)

Create Trigger Tr_student_inseted
On student
Instead of update--update is modified
As
Begin
--It's not a very similar story here, excuse me
End
Go

Inserted--Just a data table that can temporarily store data

Deleted--Just a data table that can temporarily store data

Insteadof--is triggered before the command

for--is triggered after the command, and for can also be written after

SQL views and triggers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.