Frame view VIEW in SQL
In SQL, a VIEW view is a virtual table in a database whose content is defined through a certain SQL statement. A VIEW consists of rows and columns like a real table. Fields in a view are fields from one or more real tables in Database.
The difference between VIEW view and table is that VIEW is not considered a data storage structure that exists in the database. Essentially the observed data in VIEW is taken from tables through data query statements and used to restrict database access or to hide complex data.
In this article, Quantrimang will give you detailed instructions on how to use VIEW in SQL with syntax and specific examples to make it easier to visualize and capture commands.
Create VIEW in SQL
VIEW is created by CREATE VIEW statement . VIEWs can be created from a single table, multiple tables or from other VIEW.
The basic syntax of CREATE VIEW command in SQL is as follows:
CREATE VIEW ten_view AS
SELECT cot1, cot2.
FROM ten_bang
WHERE [dieu_kien];
You can add multiple tables in the SELECT statement similar to using them in a normal SQL SELECT query.
Examples of VIEW in SQL
Suppose the NHANVIEN table has the following records:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
The following is an example to create a VIEW from the NHANVIEN table . This VIEW is used to get the name and age from the NHANVIEN table .
SQL > CREATE VIEW NHANVIEN_VIEW AS
SELECT ten, tuoi
FROM NHANVIEN;
Now, you can query NHANVIEN_VIEW in the same way as you query the actual table, for example:
SQL > SELECT * FROM NHANVIEN_VIEW;
The above command returns the result:
+----------+-----+ | ten | tuoi| +----------+-----+ | Thanh | 32 | | Loan | 25 | | Nga | 23 | | Manh | 25 | | Huy | 27 | | Cao | 22 | | Lam | 24 | +----------+-----+
WITH CHECK OPTION option in SQL
WITH CHECK OPTION is an option of CREATE VIEW command. The purpose of WITH CHECK OPTION is to ensure that all UPDATE and INSERT satisfy the conditions in the VIEW definition.
If they do not satisfy the conditions, UPDATE and INSERT will return an error.
The following example creates the view NHANVIEN_VIEW with the WITH CHECK OPTION option.
CREATE VIEW NHANVIEN_VIEW AS
SELECT ten, tuoi
FROM NHANVIEN
WHERE tuoi IS NOT NULL
WITH CHECK OPTION;
In this case, if you try to UPDATE or INSERT NHANVIEN_VIEW with the value tuoi = null , an error will occur, but if another NULL, UPDATE or INSERT will succeed.
UPDATE a VIEW in SQL
A VIEW can be updated under the following specific conditions:
- The SELECT clause cannot contain the keyword DISTINCT.
- SELECT clause cannot contain sum functions.
- SELECT clause cannot contain aggregation functions.
- The SELECT clause cannot contain calculation expressions.
- SELECT clause must not contain the ORDER BY clause.
- The FROM clause cannot contain multiple tables.
- The WHERE clause cannot contain subqueries.
- The query does not contain GROUP BY or HAVING.
- The estimated columns cannot be updated.
- All NOT NULL columns from the original table must be selected in the VIEW to trigger INSERT queries.
So if a VIEW satisfies all of the above rules, you can use the UPDATE statement for that VIEW. The following example updates for an employee named Thanh.
SQL > UPDATE NHANVIEN_VIEW
SET AGE = 35
WHERE ten = 'Thanh';
Finally, the original NHANVIEN table was updated and accordingly VIEW was updated. Now, try to query the original table and the SELECT statement will produce the result:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 35 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Insert rows into VIEW in SQL
Data rows can be inserted in a VIEW. UPDATE similar rules also apply to INSERT statements .
Here, it is not possible to insert rows into NHANVIEN_VIEW because we do not select all NOT NULL columns from the original table in VIEW. We insert rows into a VIEW in the same way when you insert them into a table.
Delete rows from VIEW in SQL
Data rows may be deleted from a VIEW. The same UPDATE and INSERT rules also apply to DELETE statements in SQL.
The following example will delete a row with TUOI = 22 :
SQL > DELETE FROM NHANVIEN_VIEW
WHERE tuoi = 22;
The result of a row in the original NHANVIEN table will be deleted and the result is similar to that VIEW itself. Now, try querying the original table, and the SELECT statement will produce the result:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Delete VIEW in SQL
You can delete VIEW if it is no longer needed. The syntax is as follows:
DROP VIEW ten_view;
Example delete view NHANVIEN_VIEW from the original table:
DROP VIEW NHANVIEN_VIEW;
In the next section, we will learn how to use the HAVING clause in SQL , remember to follow it.
Last lesson: TRUNCATE TABLE command in SQL
Next lesson: HAVING clause in SQL
You should read it
- How to view RAM RAM on a PC
- Instructions to turn off Protected View feature in Office 2013
- What is Protected View? Turn on and off the Protected View feature in Office 2016
- This is how to view the cache of any Web site
- How to Choose Point of View in Storytelling
- How to set the right to view Zalo Logs
- Steps to use Outline View in Microsoft Word
- Steps to temporarily view Excel files when working together
May be interested
- How to fix iPhone with blue frame 99% effectivesimple and effective instructions to fix iphone showing blue frame. if you are annoyed with this problem, apply my tips
- How to fix drop frame and connection problems in OBSthis guide covers all network-related tips, drop frames, and disconnections that you may apply.
- Full-Frame FE 33MM F1.4 GM lens from Sonythe fe 35mm f1.4 gm lens is the first choice for e-mount camera users. it is perfect for both photography and movie shooting in all genres from landscapes, portraits to street portraits.
- How to create and insert livestream frames for bookslivestream is gradually becoming a trend that many people are aiming for when there is great development. join hacom to learn about how to create and insert beautiful and attractive game livestream frames through the following article.
- How to draw a frame in Wordhow to draw a frame in word. creating a cover page in word or highlighting certain text with a border will make your word document more impressive and professional. if you do not know how to draw frames in word, please join
- Instructions for creating a Valentine double heart photo framein addition to colorful valentine cards to send your feelings, you can also create a sweet valentine double heart photo frame with photos of you and that person.
- How to make a photo frame in Photoshopif you own photoshop software on your computer, then you can make your portrait photos more unique and fancy by framing. if you do not know how to create a photo frame in photoshop, please refer to the following article.
- Things to know about Samsung Smart View applicationhave you ever wondered what is samsung smart view? if the answer is yes, please join us to find out details about this application through the following article.
- How to draw a frame, create a border using Photoshopto draw a frame to create a border in photoshop you can follow the following steps of tipsmake.com to learn the fastest way, quickly create a frame and border for your photos. there are two types of border formation: border outside the image and border right on the image.
- How many FPS can the human eye see?first and foremost, humans don't really see every frame per second, as this is simply how a monitor displays an image.