What are SQL Views?
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
where,
There are two types of view:
Consider the STUDENT table having the following records −
ID | NAME | ADDRESS |
---|---|---|
1 | Stephan | Delhi |
2 | Kathrin | Noida |
3 | David | Ghaziabad |
4 | Alina | Gurugram |
Query to Create a View from the above table:
CREATE VIEW Details AS
SELECT NAME, ADDRESS
FROM STUDENT
WHERE ID < 3;
Note: The results of the SELECT statement will be saved in another object called Detail.
To see the data in the View, we can query the view in the same manner as we query a table.
SELECT * FROM Details;
Here the “Details” is a view created in the previous example.
In SQL, we can use the create VIEW statement with the FORCE option to build a VIEW even if the underlying table or tables do not exist. Syntax:
CREATE FORCE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
here the FORCE
keyword is used to create the VIEW.
In SQL, you can update a view using the UPDATE
statement in a similar way to updating a table. However, there are a few important considerations to keep in mind:
Syntax:
UPDATE view-name
SET column1=value1, column2=value2, ...
WHERE condition;
A read-only view cannot be changed, added to, or removed. It is used to give a simplified or limited data interface without allowing changes to the underlying tables.
Syntax:
CREATE FORCE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition WITH read-only;
Note: Creating a read-only view is similar to creating a regular view, but with the addition of the WITH READ ONLY
clause.
Note: also read about SQL: Sequence
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…