SQL Views
SQL Views
SQL Views
Views in SQL are a kind of virtual table. A view also has rows and
columns like tables, but a view doesn’t store data on the disk like a
table. View defines a customized query that retrieves data from
one or more tables, and represents the data as if it was coming
from a single source.
We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a
table or specific rows based on certain conditions.
After the view has been created , we can perform following SQL
commands to refer to that view:
Select
Insert
Update
delete
Syntax
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
Parameters:
view_name: Name for the View
table_name: Name of the table
condition: Condition to select rows
StudentDetails
StudentMarks
To see the data in the View, we can query the view in the same
manner as we query a table.
Syntax
DROP VIEW view_name;
Example
In this example, we are deleting the View MarksView.
DROP VIEW MarksView;
Syntax
UPDATE view_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
If you want to update the view definition without affecting the data,
use the CREATE OR REPLACE VIEW statement. you can use
this syntax
If we want to update the view MarksView and add the field AGE to
this View from StudentMarks Table, we can do this by:
Output:
Example 2: Update View to Insert a row in a view
We can insert a row in a View in the same way as we do in a
table.
INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("Suresh","Gurgaon");
Output:
Uses of a View