Views in DBMS
About views in DBMS
In this article, we will learn about views in DBMS.
Views in DBMS are used to view filtered data out of the complete set of data based on some conditions by writing a sql query for the same.
Views in DBMS
- A
view
in SQL is a virtual table that is based upon the result-set of an SQL statement - A view will also have rows and columns just like a real table in a database
- Simply a view is nothing but
a stored SQL Query
- A view can contain all the rows of a table or specific rows based on some condition
- SQL functions conditions and join statements to a view and present the data just like the data is produced from a single table
Creating a view
A view is created by selecting fields from one or more tables present in a database
Syntax
CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
Note:
Whenever a user creates a view, database engine recreates the data using the views SQL statement i.e. view always shows upto date data
Consider the tables StudentDetails and StudentMarks
StudentDetails
S_ID | NAME | ADDRESS |
---|---|---|
1 | Harini | Kolkata |
2 | Preity | Hyderabad |
3 | Divya | Chennai |
4 | Kushi | Mumbai |
5 | Amitha | Bangalore |
StudentMarks
ID | NAME | MARKS | AGE |
---|---|---|---|
1 | Harini | 96 | 20 |
2 | Manisha | 90 | 19 |
3 | Divya | 94 | 21 |
4 | Kushi | 92 | 19 |
5 | Amitha | 95 | 21 |
Simple Views in DBMS: Creating a view from a single table
In this example, we will create a view named as DetailsView from a single table StudentDetails
CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM StudentDetails WHERE S_ID < 5;
The data present in a view can be seen just like a normal table select query
SELECT * FROM DetailsView;
Output:
NAME | ADDRESS |
---|---|
Harini | Kolkata |
Preity | Hyderabad |
Divya | Chennai |
Kushi | Mumbai |
Complex view: Creating a view from multiple tables
- In this example will create a view named MarksView by taking data from both the table’s student details and student marks
- To create a View from multiple tables just simply include multiple tables in the SELECT statement.
CREATE VIEW MarksView AS SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS FROM StudentDetails, StudentMarks WHERE StudentDetails.NAME = StudentMarks.NAME;
To display data of View Marks:
SELECT * FROM MarksView;
Output:
NAME | MARKS | ADDRESS |
---|---|---|
Harini | 96 | Kolkata |
Divya | 94 | Chennai |
Kushi | 92 | Mumbai |
Amitha | 95 | Bangalore |
Deleting views in DBMS
- You can simply delete a view by using the Drop statement
- That view is not used anymore
Syntax:
DROP VIEW view_name;
Example
DROP VIEW MarksView;
Updating views
Views are updated only if certain conditions are met otherwise if any one of the conditions are not met views will not be updated
Criteria for View Updating
- The select statement used in the create view statement should not include group by clause or order by clause
- The select statement must not contain distinct keyword
- A view should not be created from nested or Complex queries
- A view should be created from a single table but if the view is created from more than one table then it is not allowed for updating
CREATE OR REPLACE VIEW
Create or replace view statement is used to add or remove fields from existing views
Syntax:
CREATE OR REPLACE VIEW view_name AS SELECT column1,coulmn2,.. FROM table_name WHERE condition;
Update the view MarksView and add the field AGE to this View from StudentMarks Table,
CREATE OR REPLACE VIEW MarksView AS SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks. AGE FROM StudentDetails, StudentMarks WHERE StudentDetails.NAME = StudentMarks.NAME;
Fetch all the data from MarksView now as:
SELECT * FROM MarksView;
Output
NAME | ADDRESS | MARKS | AGE |
---|---|---|---|
HARINI | Kolkata | 96 | 20 |
Divya | Chennai | 94 | 21 |
Kushi | Mumbai | 92 | 19 |
Amitha | Bangalore | 95 | 21 |
Inserting a row into a view
We can use insert into statement of SQL to insert a row in a view just like inserting a row in an ordinary table
Syntax:
INSERT view_name(column1, column2 , column3,..) VALUES(value1, value2, value3..);
Example
INSERT INTO DetailsView(NAME, ADDRESS) VALUES("Preity","Hyderabad");
Fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
Output
NAME | ADDRESS |
---|---|
Harini | Kolkotta |
Divya | Chennai |
Kushi | Mumbai |
Amitha | Bangalore |
Preity | Hyderabad |
Deleting a row from a view
- A row in a view can be deleted just like simply deleting rows from a Table using delete statement
- But remember a row in a view can be deleted only if the row is actually deleted in the original table from which it is created
Syntax:
DELETE FROM view_name WHERE condition;
Example
DELETE FROM DetailsView WHERE NAME="Preity";
Fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
Output:
NAME | ADDRESS |
---|---|
Harini | Kolkotta |
Divya | Chennai |
Kushi | Mumbai |
Amitha | Bangalore |
Preity | Hyderabad |
Advantages and disadvantages of views
Advantages
- Enforce Business Rules: By placing complicated or misunderstood business logic into the view, you can be sure to present a unified portrayal of the data which increases use and quality.
- Consistency: Once defined their calculations are referenced from the view rather than being restated in separate queries. This makes for less mistakes and easier maintenance of code.
- Security: For example, you can restrict access to the employee table, that contains social security numbers, but allow access to a view containing name and phone number.
- Simplicity: Databases with many tables possess complex relationships, which can be difficult to navigate if you aren’t comfortable using Joins.
- Space: Views take up very little space, as the data is stored once in the source table.
Limitations
- Modifications: Not all views support INSERT, UPDATE, or DELETE operations. Complex multi-table views are generally read-only.
- Performance: Hugely complex job for the database engine. That is because each time a view is referenced, the query used to define it, is rerun.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment