Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

DATABASE

VIEWS
VIEWS

• Views in SQL are a kind of virtual table.


• A view also has rows and columns as they are in a real
table in the database.
• 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.
• A view is a database object that has no values.
TYPES OF
VIEWS
USER DEFINED SYSTEM DEFINED
VIEWS VIEWS
USER DEFINED VIEW

SIMPLE VIEW COMPLEX VIEWS


Simple View Updates:
• Updates are more likely to be allowed on simple views.
• Simple views are those based on a single table, often with a
straightforward mapping to the underlying data.
• You can typically update a simple view if it meets certain criteria,
such as having a clear mapping to the columns of the underlying
table and not involving complex calculations.

Complex View Updates:


• Updates on complex views, especially those involving multiple
tables, aggregates, or calculations, might not be allowed.
• When making updates, the database might struggle to figure out
how to apply the changes accurately to the original data in a way
that makes sense.
SYSTEM-DEFINED VIEWS
• Think of a database as a well-organized library. The books in the
library are like the data tables in a database, and the system-defined
views are like the catalog or index that tells you about all the books
and where to find them.
• In a database, these system-defined views are pre-built tables that
store information about the database itself. They contain details about
tables, columns, indexes, and other objects within the database.
• System-defined views are pre-built by the database management
system (DBMS) and are part of the system's catalog or information
schema.

Who can use this view?


• Database administrators, developers, and users with the appropriate
permissions
CREATING AND MANAGING 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

A view can be created using the CREATE VIEW statement.


We can create a view from a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
UPDATE VIEW
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 VIEW 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 VIEWS

CREATE OR REPLACE VIEW film_view AS


SELECT film_id,title,release_year,rating FROM film
WHERE rating="PG";
DROP VIEW

A view can be deleted using the Drop View statement.


•Syntax
DROP VIEW view_name;

Example:
drop view film_view;
select * from film_view;
REAL-TIME CASE:
FLIPKART INVENTORY MANAGEMENT

• Flipkart, a leading Indian e-commerce giant,


relies heavily on efficient inventory
management to fulfill customer orders
promptly and minimize costs.
• Scenario: During the peak festive season,
Flipkart anticipates a surge in demand for
smartphones.
CODE:
•CREATE TABLE orders (prod_id INT, prod_name VARCHAR(50),
order_quantity INT);
•INSERT INTO orders(prod_id, prod_name, order_quantity)
VALUES(1,'Shirt',5000);
•INSERT INTO orders(prod_id, prod_name, order_quantity)
CREATING VALUES(2,'Wall Decoration',3500);
•INSERT INTO orders(prod_id, prod_name, order_quantity)
A TABLE VALUES(3,'Bottle',6200);
•INSERT INTO orders(prod_id, prod_name, order_quantity)
VALUES(4,'Festive lights',1700);
•INSERT INTO orders(prod_id, prod_name, order_quantity)
VALUES(5,'Jewellery',2950);
•INSERT INTO orders(prod_id, prod_name, order_quantity)
VALUES(6,'Hair clip',4200);
OUTPUT:

prod_id prod_name order_quantity

1 Shirt 5000

2 Wall Decoration 3500

3 Bottle 6200

4 Festive lights 1700

5 Jewellery 2950

6 Hair clip 4200


CODE:

•CREATE VIEW top_selling_products AS


CREATING VIEW - •SELECT prod_id, prod_name, SUM(order_quantity) AS
TOP SELLING total_sold
PRODUCTS •FROM orders
•GROUP BY prod_id, prod_name
•ORDER BY total_sold;
•SELECT * FROM top_selling_products;
OUTPUT:

prod_id prod_name Total_sold

4 Festive lights 1700

5 Jewellery 2950

2 Wall Decoration 3500

6 Hair clip 4200

1 Shirt 5000

3 Bottle 6200
UPDATE VIEW
• Update top_selling_products:

CODE:

ALTER VIEW top_selling_products AS


SELECT prod_id, prod_name, SUM(order_quantity) AS total_sold
FROM orders
GROUP BY prod_id, prod_name
ORDER BY total_sold DESC
LIMIT 3;
SELECT * FROM top_selling_products;
OUTPUT

prod_id prod_name total_sold

3 Bottle 6200

1 Shirt 5000

6 Hair clip 4200


DELETE VIEW
• Delete top_selling_products view:

CODE:

DROP VIEW top_selling_products;


QUESTION:
• Create a Product table with product ID, product
name, and price.
• Create 3 products with product ID, price, and
product name
• Create a view for the table
• Update the view by adding 1 more product to it.
• Delete view that was created.

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;

ALTER VIEW pricing AS


SELECT prod_id, prod_name, SUM(price) AS total_cost
FROM product
GROUP BY prod_id, prod_name
ORDER BY total_cost DESC
LIMIT 1;
SELECT * FROM pricing;

DROP VIEW pricing;

You might also like