Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 2141 – Intro to Database Systems

Similar presentations


Presentation on theme: "CSCI 2141 – Intro to Database Systems"— Presentation transcript:

1 CSCI 2141 – Intro to Database Systems
Advanced SQL – Views

2 Virtual Tables – Views

3 Virtual Tables Output of a relational operator such as SELECT is another relation (or table) What if you require a query to be executed frequently to obtain the most updated data Example: For the store database, you might need to see quantity of items remaining at the end of each day For the library database, you might need to see how many books were borrowed or returned It might make sense to save the query in the database

4 What is a View? View is a virtual table based on a SELECT query
Query can contain columns, aliases and aggregate functions from one or more tables The tables on which the query is based are called base tables Why not have tables instead? The information you need is already present in the base tables Views simply organize/present that information in the way you need it Having more tables would create duplicate data and data consistency issues

5 How are Views Created? Views can be created using the CREATE VIEW command Syntax: CREATE VIEW viewname AS SELECT query CREATE VIEW command is a DDL command It stores the sub-query specification in the data dictionary Sub-query (SELECT statement) is used to generate the virtual table or View

6 How are Views Created? Example: Create a view to generate the product listing with the quantity on hand of products with price over 50 CREATE VIEW PRICEGT50 AS SELECT P_DESCRIPT, P_QOH, P_PRICE FROM PRODUCT WHERE P_PRICE > 50; How to use this VIEW? Same way you would use a table The viewname can be used exactly as you would use a table in queries

7 How are Views Used? Example: CREATE VIEW P_STAT AS SELECT V_CODE, SUM(P_QOH*P_PRICE) AS TOTCOST, MAX(P_QOH) AS MAXQTY, MIN(P_QOH) AS MINQTY, AVG(P_QOH) AS AVGQTY FROM PRODUCT GROUP BY V_CODE; SELECT * FROM P_STAT;

8 What is the Advantage of Using a View
Views can hide complexity Complex queries that requires joining several tables, or have complex logic or calculations can be saved as Views Views can be used as a security mechanism Only make selected columns available to users Views can provide logical data independence If you need to change the structure of your tables, you can provide a view based on the old table structure This helps avoid breaking the legacy code that references the table Security -> Hide the Credit Card column from the Sales people for enhanced security If you need to refactor a table that would break a lot of code, you can replace the table with a view of the same name. The view provides the exact same schema as the original table, while the actual schema has changed. This keeps the legacy code that references the table from breaking, allowing you to change the legacy code at your leisure.

9 Won’t Views Introduce Data Inconsistency?
No! Views are dynamically updated Re-created on demand each time they are invoked If data changes, the changes are automatically incorporated

10 Can Views be Updated? Views are stored SELECT statements posing as a table The actual data is stored in the original tables If views are like tables, can they be updated? Updatable views can be updated Not all views are updatable What are updatable views? Views that do not have any of the following: Aggregate functions or GROUP BY expression SET operators like UNION, INTERSECT or MINUS Views without all NOT NULL columns from the base tables only "assembled" into the view when you want to look at it If the view contains joins between multiple tables, you can only insert and update one table in the view, and you can't delete rows.

11 Should Views be Used for Updates?
Views are indispensable for reasons specified earlier However, updating through views depends on usage Could be employed for batch update routines Generally not used very frequently Normally, it is preferable to update tables directly, or use procedures Finally, can we see which views are updatable? SELECT TABLE_NAME, IS_UPDATABLE FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA= 'schema name'; INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.


Download ppt "CSCI 2141 – Intro to Database Systems"

Similar presentations


Ads by Google