1
Murali Mani
SQL: Updates (DML) and Views (DDL)
Murali Mani
SQL DML (Updating the Data)
- Insert
- Delete
- Update
SQL: Updates (DML) and Views (DDL) Murali Mani SQL DML (Updating - - PDF document
SQL: Updates (DML) and Views (DDL) Murali Mani SQL DML (Updating the Data) Insert Delete Update Murali Mani 1 Inserting tuples INSERT INTO Student VALUES (6, Emily, 324 FL, NULL); INSERT INTO Student (sNumber,
Murali Mani
Murali Mani
Murali Mani
Murali Mani
Deleting tuples
Updating tuples
Murali Mani
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.
Murali Mani
View is a virtual relation
Convenience: Queries on base relations might be
Logical Data Independence: “base tables” may
Provide different views of the same data. Security: Expose only necessary data to users
Views can be queried like any “base” relation.
Murali Mani
CREATE VIEW <viewName> as <query> DROP VIEW <viewName>
Murali Mani
2 320FL Matt 3 1 320FL Greg 2 1 320FL Dave 1 professor address sName sNumber
241FL ER 2 235FL MM 1 address pName pNumber
CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; SELECT * from studentProfessor
ER Matt MM Greg MM Dave professor student
Murali Mani
Consider views defined with only one
Murali Mani
Murali Mani
Murali Mani
If the SELECT clause specifies DISTINCT,
Murali Mani
Note that the WHERE clause may specify
Murali Mani
CREATE VIEW studentProf(student, professor) AS SELECT sName, pName FROM Student, Professor WHERE professor=pNumber;
see that sNumber is a key for Student and also for the view (though sNumber does not appear in the result). So deleting from the views are possible by deleting appropriate sNumbers from the Student table.
Murali Mani
DELETE FROM Studentprof WHERE professor='MM';
the student table.
relation views if there is a table such that the view and the table have the same key.
Murali Mani
Suppose we drop the key constraint on the
Now delete will fail because there is no table
Murali Mani
CREATE VIEW studentProf(student, professor) AS SELECT sNumber, pName FROM Student, Professor WHERE professor=pNumber; INSERT INTO Studentprof VALUES (4, 'ER');
INTO Professor TABLE AS WELL. INSERT INTO Studentprof(student) VALUES (4);
Murali Mani
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