SQL Views and Updates cs3431
SQL DML (Updating the Data) Insert Delete Update cs3431
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 FROM Student; cs3431
Delete and Update Deleting tuples DELETE FROM Student WHERE sNumber=‘6’; Updating tuples UPDATE Student SET professor=‘ER’ WHERE sNumber=‘6’ cs3431
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. cs3431
Views View is a virtual relation defined by: Named stored SQL query Views can be queried like any “base” relation. cs3431
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 cs3431
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 cs3431
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 cs3431
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 professor, count(*) FROM studentProfessor GROUPBY professor; cs3431
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; cs3431
Updating Views 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? Answer: Updating these views are done by updating the underlying Student tables. cs3431
Updating Single Relation Views DELETE FROM MyStudent WHERE name=`Dave'; -- This will delete the corresponding row from the Student table DELETE FROM Student WHERE name=`Dave'; The update is valid ! cs3431
Updating Single Relation Views INSERT INTO MyStudent VALUES (4, `Mary’); -- This will be translated to: INSERT INTO Student (sNumber, sName) VALUES (4, `Mary’); cs3431
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 must not be null. cs3431
Updating Single Relation views If the SELECT clause specifies DISTINCT, then the view is not updatable. For instance, the following view is not updatable. CREATE VIEW MyStudent2(num) AS SELECT DISTINCT sNumber FROM Student; cs3431
Updating Single Relation Views WHERE clause may specify subqueries. CREATE VIEW MyStudent3 (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, though that tuple does not appear in the view. cs3431
Multiple relation views: Delete Consider a multi-relation view such as 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 ! cs3431
Multiple relation views Consider a multi-relation view such as 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 cs3431
Multi-Relation View Deletes can be done against multi-relation views if there is a table such that the view and the table have the same key. cs3431
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 professor 1 MM 2 3 ER CREATE VIEW studentProfessor (student, professor) 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 cs3431
Deleting from multi-relation views Try the following update statements: DELETE FROM studentProfessor WHERE professor='MM'; -- What will be deleted ? cs3431
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 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 professor='MM'; -- This will actually delete the two rows in the student table. cs3431
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 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 the professor table for this view. cs3431
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 VALUES (4, 'ER'); -- THIS ABOVE INSERT WILL FAIL AS IT TRIES TO INSERT INTO Professor TABLE AS WELL. INSERT INTO Studentprof(student) VALUES (4); -- THIS ABOVE INSERT WILL SUCCEED. cs3431
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. cs3431
Views Conclusion Views are useful – Virtual relations Querying through views is always possible Updating through views has limitations DBA can set up INSTEAD OF triggers cs3431