Professional Documents
Culture Documents
DATABASE VIEWS Final
DATABASE VIEWS Final
VIEWS
VIEWS
1 2 3
CREATE VIEW Altering Views: Modify the Dropping Views: Use the
statement: CREATE VIEW definition of an existing DROP VIEW statement to
statement is used to create view with the ALTER VIEW remove a view from the
a new view and defines the statement. database.
view with its name, SELECT
query.
ADVANTAGES
1.Reduced Complexity: Views help to reduce the complexity. Different views can be created
on the same base table for different users.
2.Security: It increases security by excluding sensitive information from the view.
3.Query Simplicity: It helps to simplify commands from the user. A view can draw data from
several different tables and present it as a single table.
4.Consistency: A view can present a consistent, unchanged image of the structure of the
database. Views can be used to rename the columns without affecting the base table.
5.Data Integrity: If data is accessed and entered through a view, the DBMS can
automatically check the data to ensure that it meets the specified integrity constraints.
6.Storage Capacity: Views take very little space to store the data.
DISADVANTAGES
The DML statements that can be performed on a view created using a single base table have certain
restrictions:
1. You can't execute INSERT, UPDATE, or DELETE statements on a view if with read-only option
is enabled.
2. You can’t create a view on temporary tables.
3. You cannot INSERT, UPDATE, or DELETE if the view contains group functions GROUP BY,
DISTINCT, or a reference to a pseudo column row num.
4. You can't pass parameters to the SQL server views.
5. You can't associate rules and defaults with views.
CREATING 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 VIEWS
Example:
drop view film_view;
select * from film_view;
REAL-TIME CASE:
FLIPKART INVENTORY MANAGEMENT
1 Shirt 5000
3 Bottle 6200
5 Jewellery 2950
5 Jewellery 2950
1 Shirt 5000
3 Bottle 6200
UPDATE VIEW
• Update top_selling_products:
CODE:
3 Bottle 6200
1 Shirt 5000
CODE:
ACTIVITY
ANSWERS
CREATE TABLE product (prod_id INT, prod_name SELECT * FROM product;
VARCHAR(50), price INT);
INSERT INTO product(prod_id, prod_name, price) CREATE VIEW pricing AS
VALUES(1,'Shirt',250); SELECT prod_id, prod_name, SUM (price) AS total_cost
INSERT INTO product(prod_id, prod_name, price) FROM product
VALUES(2,'Wall Decoration',480); GROUP BY prod_id, prod_name
INSERT INTO product(prod_id, prod_name, price) ORDER BY total_cost;
VALUES(3,'Bottle',150); SELECT * FROM pricing;