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.
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…
A Binary Search Tree (BST) is a type of binary tree that satisfies the following…