SQL Views CS542.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
Murali Mani The Relational Model. Murali Mani Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still.
Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
Cs3431 Transactions, Logging and Security. cs3431 Transactions: What and Why? A set of operations on a database must appear as one “unit”. Example: Consider.
Database Systems More SQL Database Design -- More SQL1.
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
The Relational Model1 ER-to-Relational Mapping and Views.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Trigger used in PosgreSQL
SQL Query Getting to the data ……..
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
Tables and Triggers.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Schema Refinement and Normal Forms
Database Management Systems (CS 564)
Database Construction (and Usage)
SQL : Query Language Part II CS3431.
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
SQL : Query Language CS3431.
Normalization Murali Mani.
Functional Dependencies and Normalization
SQL Views and Updates cs3431.
Advanced SQL: Views & Triggers
Chapter 2 Views.
Instructor: Mohamed Eltabakh
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
The Relational Model Transparencies
Instructor: Mohamed Eltabakh
Normalization Part II cs3431.
Chapter 2 Views.
Structured Query Language (3)
Normalization cs3431.
Instructor: Mohamed Eltabakh
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
SQL: Structured Query Language
Views 1.
SQL: Structured Query Language
SQL: Structured Query Language
IST 318 Database Administration
SQL – Constraints & Triggers
SQL-Views and External Schemas
SQL Views Presented by: Dr. Samir Tartir
Advanced Topics: Indexes & Transactions
SQL: Structured Query Language
Presentation transcript:

SQL Views CS542

SQL DML (Updating Data) Insert Delete Update CS542

Inserting tuples INSERT INTO Student VALUES (6, ‘Emily’, ‘324 FL’, NULL); INSERT INTO Student (sNumber, sName) VALUES (6, ‘Emily’); INSERT INTO Professor (pNumber) SELECT professor.id FROM Student; CS542

Delete DELETE FROM Student WHERE sNumber=‘6’; CS542

SET professor=‘MickyMouse’ WHERE sNumber=‘6’ Update UPDATE Student SET professor=‘MickyMouse’ WHERE sNumber=‘6’ CS542

Views NOTE: You can present logical subsets or combinations of the data by creating views of tables. A view is a virtual table based on a table or another view. A view contains no data of its own but is like a window through which data from tables can be viewed or changed. The tables on which a view is based are called base tables. The view is stored as a SELECT statement in the data dictionary. CS542

Views View is a virtual relation defined by: Named stored SQL query Views can be queried like any “base” relation. CS542

Views CREATE VIEW <viewName> as <query> CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; DROP VIEW studentProfessor CS542

Views - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 235FL 2 ER 241FL CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; student professor Dave MM Greg Matt ER SELECT * from studentProfessor CS542

Views - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 235FL 2 ER 241FL student professor Dave MM Greg Matt ER CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; advisees professor 2 MM 1 ER SELECT professor, count(*) as advisees FROM studentProfessor GROUPBY professor; CS542

Querying Views CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; student professor Dave MM Greg Matt ER SELECT pnumber as professor, count(*) FROM Student, Professor WHERE Student.professor = Professor.pNumber GROUPBY professor; SELECT professor, count(*) FROM studentProfessor GROUPBY professor; CS542

Views ? Why ? View is a virtual relation ? Convenience: Queries on base relations might be “complex” Logical Data Independence: “Base tables” may change, but still queries using views need not change. Customization: Provide different views of the same data. Security: Expose only necessary data to users CS542

Updating Views CREATE VIEW MyStudent(num,name)AS SELECT sNumber, sName Consider views defined with only one relation in the FROM clause such as: CREATE VIEW MyStudent(num,name)AS SELECT sNumber, sName FROM Student; Question: Are these views updatable? How ? Answer: Updating these views is done by updating the underlying Student base tables. CS542

Updating Single Relation Views DELETE FROM MyStudent WHERE name=`Dave'; -- This will delete corresponding row from Student table DELETE FROM Student WHERE name=`Dave'; This update is valid ! CS542

Updating Single Relation Views INSERT INTO MyStudent VALUES (4, `Mary’); -- This will be translated to: INSERT INTO Student (sNumber, sName) VALUES (4, `Mary’); -- What happens to other values of Student tuple ? -- What if there is a tuple with sNumber of 4 already ? CS542

Inserting into single relation views CREATE VIEW MyStudent1(name) AS SELECT sName FROM Student; INSERT INTO MyStudent1 VALUES (‘Mary’) will be translated to: INSERT INTO Student(sName) VALUES (‘Mary’). This will return an error as sNumber is primary key, i.e., it must not be null. CS542

Updating Single Relation views What about views with DISTINCT ? Updatable? CREATE VIEW MyStudent2(name) AS SELECT DISTINCT sName FROM Student; CREATE VIEW MyStudent3(num) AS SELECT DISTINCT sNumber If the SELECT clause specifies DISTINCT, then the view is not updatable. CS542

Updating Single Relation Views WHERE clause may specify subqueries. CREATE VIEW MyStudent4 (num, name) AS SELECT sNumber, sName FROM Student WHERE sName IN (SELECT pName FROM Professor); -- Insert into this view will insert into Student table CS542

Updating Single Relation Views WHERE clause may specify subqueries. CREATE VIEW MyStudent4 (num, name) AS SELECT sNumber, sName FROM Student WHERE sNumber NOT IN (SELECT sNumber FROM Student); -- This view will always have 0 tuples. -- Insert into this view will still insert into Student table, even though that tuple does not appear in the view. CS542

Multiple Relation Views: Delete CREATE VIEW studentProf (studentname, profname) AS SELECT sName, pName FROM Student, Professor WHERE sName = pName; Student Professor sNumber sName address professor 1 MM 320FL 2 3 Matt pNumber pName address 1 MM 235FL 2 ER 241FL CS542

Multiple Relation Views: Delete Updating this multi-relation view? CREATE VIEW studentProf (studentname, profname) AS SELECT sName, pName FROM Student, Professor WHERE SName = PName; -- Ambigious what base table to update ! -- Side effects as other tuples may disappear out of the view ! CS542

Multi-Relation View Deletes can be done against multi-relation views if there is a table with same key as the view. CS542

Views - Example Student Professor pNumber is key in Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 235FL 2 ER 241FL student profname 1 MM 2 3 ER CREATE VIEW studentProfessor (student, profname) AS SELECT sNumber, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; pNumber is key in Professor sNumber is key of Student sNumber is key of view What is key in studentProfessor table? CS542

Multi-Relation View Reminder : Deletes can be done against multi-relation views if there is a table such that the view and table have same key. CS542

Deleting from multi-relation views Try following update statements: DELETE FROM studentProfessor WHERE profname ='MM'; -- What will be deleted ? CS542

Views - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 235FL 2 ER 241FL student profname 1 MM 2 3 ER CREATE VIEW studentProfessor (student, professor) AS SELECT sNumber, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; DELETE FROM studentProfessor WHERE profname='MM'; -- This will actually delete the two rows in Student table. CS542

Views - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 235FL ER 241FL CREATE VIEW studentProfessor (student, professor) AS SELECT sNumber, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; Now delete will fail because there is no table whose key is the key of the view. Suppose we drop key constraint on professor table for this view. CS542

Inserting into multi-relation views Consider view definition: CREATE VIEW studentProf(student, professor) AS SELECT sNumber, pName FROM Student, Professor WHERE professor=pNumber; INSERT INTO Studentprof(student) VALUES (4); -- the above insert will succeed; put into Student table INSERT INTO Studentprof VALUES (4, 'ER'); -- THIS ABOVE INSERT WILL FAIL AS IT TRIES TO INSERT INTO Professor TABLE AS WELL. CS542

Inserting into multi-relation views Insert will succeed only if The insert translates to insert into only one table. The key for the table to be inserted will also be a key for the view. CS542

Controlling Updates on Views CREATE VIEW studentProf (student, professor, salary) AS SELECT sName, pName, salary FROM Student s, Professor p WHERE s.advisor = pName; Application can use TRIGGER to create desired effect of updates on view: CREATE OR REPLACE TRIGGER DeleteMyView INSTEAD OF DELETE ON studentProf FOR EACH ROW DECLARE name varchar (10); BEGIN name := :old.student; DELETE FROM Student where sName=name; END; . CS542

Materialized Views Materialized View : Cache content of view Why ? Issues : Type of “connectivity” to base data Keeping the view content up-to-date, also called view maintenance Utilization of INSTEAD OF TRIGGERS for maintenance CS542

Views - Summary Views are useful – Virtual relations Querying through views is always possible Updating through views has limitations – consider use of INSTEAD OF TRIGGERS CS542