Download presentation
Presentation is loading. Please wait.
1
CSCI 52– Introduction to SQL Fall 2016
Advanced SQL VIEW (Topic not covered in the textbook) Week-10 Plan
2
Week-10 activities include
Week-10 PowerPoint slide Week-10 lecture summary notes Week-10 Lab Assignment due 4/9 Wee-10 Discussion due 4/9 Exam-2 (available 4/6 – 4/10)
3
Quick Review When you want to query a table to determine which values are null, what types of predicate should you use? NULL predicate What type of data sources that can be used in an “IN” predicate? A defined list of elements or subquery (in the future) Which SELECT statement clause do you include a predicate? WHERE Clause
4
Quick Review (cont.) END
Which statement or expression do you use to set up a series of conditions that modify values? CASE What is the last word in a CASE value expression? END A(n)____ constraint enforces referential integrity between two tables by ensuring that no action is taken to either table that affects the data protected by the constraint? Foreign Key
5
Quick Review (cont.) True or False: Normalization is the process of grouping data into logical related groups? True True or False: If data is in the third normal form, it is automatically in the first and second normal form. You want to drop the Emp_bday View definition from your database. What SQL statement should you use? DROP View Emp_bday;
6
Advanced SQL VIEWS (review)
A database View Virtual table based on the result set of an SQL statement.
8
SQL VIEWS Benefits Benefits of using Views Increase security
Help simplify working with legacy code Reduce complexity of data Increase reusability of SQL statements Simplify complex SQL operations Format Data
10
New changes with PSQL 9.5.1! With the recent release of the new version of PSQL 9.5.1, on February 11, 2016 Advanced PSQL VIEW features just got easier than ever been before. We no longer need to create VIEW Rules to overcome PSQL limitation as previously done…. Very tedious work that required few steps! We now can just perform the INSERT INTO, UPDATE or DELETE command to a VIEW, which automatically modifies data in the table without creating our own overwrite rules. Thanks to the PSQL team for removing this tedious work!
11
PostgreSQL Views as previously done
Although data in a view looks similar to a table, in PostgreSQL (PSQL), data in a view are not modifiable by default, which means: You cannot : Insert to a view Update data in a view Delete from a view But… PSQL supports VIEW RULES that allow to overcome those limitations.
12
VIEW RULES for Override and Replace
View Rules allow to Modify data in a View by: Overriding Insert into Update Delete From Replacing DML commands (Insert into, Update, and Delete) with other SQL commands.
13
Three SQL View Rules Whenever an insert, update, or delete transaction on a table is performed, PSQL does three (3) things: First, makes internal copies of the table Second, maintains copies: OLD and NEW data The OLD copy contains the data in the table before the update. The NEW copy contains the data after the update.
14
Three SQL View Rules (cont.)
Third, the PSQL makes the NEW data view Insertable Translate the INSERT INTO the VIEW command to an INSERT INTO the underlying TABLE. Thus, override the VIEW’s limitation with the rule for INSERT using the DO INSTEAD. Updateable or Deletetable Translate the View’s UPDATE or DELETE command to an UPDATE to or DELETE from the underlying TABLE. Thus, override the VIEW’s limitation with the rule for UPDATE/DELETE using the DO INSTEAD.
15
PSQL VIEWS- Example - Step-1 : Create a view of patients who are still admitted CREATE VIEW Admitted_View AS SELECT * FROM Patient WHERE release_date IS NULL; Step-2: Make the Admitted_view insertable CREATE RULE admitted_insert AS ON INSERT TO admitted_view DO INSTEAD ( INSERT INTO Patient VALUES (NEW.patient_id, NEW.last_name, new.first_name, NEW.birth_date, NEW.admit_date, NEW.release_date) ; );
16
Inserting a record into Admitted_View
- Step-3: Now, test the Admitted_view with data to insert: INSERT INTO admitted_view VALUES (803, ‘Diane’, ‘Keaton', ' ', ' ', NULL); The INSERT INTO statement of the View is being translated into an ON INSERT INTO underlying table by Replacing the INSERT INTO view command with INSERT INTO table command Thus, overriding the VIEW limitation rules to rewrite the INSERT INTO rule using the DO INSTEAD.
17
Updating a record into Admitted_View
Step-2: Update records to Admitted_view CREATE RULE admitted_update AS ON UPDATE TO admitted_view DO INSTEAD ( UPDATE patient SET patient_id = NEW.patient_id, last_name = NEW.Last_name, birth_date = NEW.birth_date, admit_date = NEW.admit_date, release_date = NEW.release_date, room_number = NEW.room_number WHERE patient_id = OLD.patient_id; ); - Step-3: Test the Admitted_view UPDATE Admitted_View SET room_number = WHERE last_name = 'Diane Keaton';
18
Deleting a record from a View
Step-2: To delete records from the Admitted_view CREATE RULE admitted_delete AS ON DELETE TO admitted_view DO INSTEAD ( DELETE FROM patient WHERE patient_id = OLD.patient_id; ); Step-3: To test the Admitted_view DELETE FROM admitted_view WHERE patient_id = 801; Should delete only one record whose patient_id = 801
19
Dropping Created Rules on a View
To drop created rules on the Admitted_View DROP RULE admitted_delete ON admitted_view; DROP RULE admitted_update ON admitted_view; DROP RULE admitted_insert ON Admitted_view; After dropping the Admitted_delete rule, you can no longer try to delete another record from the view. You should get an error message since the rule has been dropped.
20
Thank You! Exam-2 contains two parts
Please do not forget to take the test between 4/6 and 4/10. Exam-2 contains two parts Part I-Online (timed)- under the Quiz area Part II (no timed)- under regular assignment area. Good luck to All!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.