cs3431 sql: updates (dml) and views (ddl). cs3431 sql dml (updating the data) insert delete update
Post on 21-Dec-2015
249 views
TRANSCRIPT
![Page 1: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/1.jpg)
cs3431
SQL: Updates (DML) and Views (DDL)
![Page 2: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/2.jpg)
cs3431
SQL DML (Updating the Data)• Insert• Delete• Update
![Page 3: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/3.jpg)
cs3431
Inserting tuplesINSERT INTO Student VALUES
(6, ‘Emily’, ‘324 FL’, NULL);
INSERT INTO Student (sNumber, sName) VALUES (6, ‘Emily’);
INSERT INTO Professor (pNumber)SELECT professorFROM Student;
![Page 4: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/4.jpg)
cs3431
Delete and Update
Deleting tuples
DELETE FROM Student
WHERE sNumber=‘6’;
Updating tuples
UPDATE Student SET professor=‘ER’
WHERE sNumber=‘6’
![Page 5: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/5.jpg)
cs3431
Views
NOTE: You can present logical subsets or combinations of the data by creatingviews of tables. A view is a virtual table based on a table or another view. A viewcontains no data of its own but is like a window through which data from tables canbe 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.
![Page 6: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/6.jpg)
cs3431
Views
View is a virtual relation defined by:
Named stored SQL query
Views can be queried like any “base” relation.
![Page 7: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/7.jpg)
cs3431
Views
CREATE VIEW <viewName> as <query>
CREATE VIEW studentProfessor (student, professor) ASSELECT sName, pNameFROM Student, ProfessorWHERE Student.professor = Professor.pNumber;
DROP VIEW studentProfessor
![Page 8: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/8.jpg)
cs3431
Views - Example
sNumber sName address professor
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 235FL
2 ER 241FL
Professor
CREATE VIEW studentProfessor (student, professor) AS
SELECT sName, pNameFROM Student, ProfessorWHERE Student.professor =
Professor.pNumber;
SELECT * from studentProfessor
student professor
Dave MM
Greg MM
Matt ER
![Page 9: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/9.jpg)
cs3431
Views - Example
sNumber sName address professor
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 235FL
2 ER 241FL
Professor
CREATE VIEW studentProfessor (student, professor) AS
SELECT sName, pNameFROM Student, ProfessorWHERE Student.professor =
Professor.pNumber;
SELECT professor, count(*) FROM studentProfessorGROUPBY professor;
student professor
Dave MM
Greg MM
Matt ER
![Page 10: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/10.jpg)
cs3431
Querying ViewsCREATE VIEW studentProfessor (student, professor) AS
SELECT sName, pNameFROM Student, ProfessorWHERE Student.professor =
Professor.pNumber;
SELECT pnumber as professor, count(*) FROM Student, ProfessorWHERE Student.professor = Professor.pNumberGROUPBY professor;
student professor
Dave MM
Greg MM
Matt ER
SELECT professor, count(*) FROM studentProfessorGROUPBY professor;
![Page 11: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/11.jpg)
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
![Page 12: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/12.jpg)
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 are updatable?
Answer: Updating these views are done by updating the underlying Student tables.
![Page 13: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/13.jpg)
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 !
![Page 14: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/14.jpg)
cs3431
Updating Single Relation Views
INSERT INTO MyStudent VALUES (4, `Mary’);
-- This will be translated to:
INSERT INTO Student (sNumber, sName) VALUES (4, `Mary’);
![Page 15: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/15.jpg)
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.
![Page 16: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/16.jpg)
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) ASSELECT DISTINCT sNumber FROM Student;
![Page 17: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/17.jpg)
cs3431
Updating Single Relation Views
WHERE clause may specify subqueries.
CREATE VIEW MyStudent3 (num, name) ASSELECT sNumber, sName FROM StudentWHERE 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.
![Page 18: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/18.jpg)
cs3431
Multiple relation views: Delete
Consider a multi-relation view such asCREATE VIEW studentProf (studentname, profname)AS SELECT sName, pNameFROM Student, ProfessorWHERE SName = PName;
-- Ambigious what base table to update ? -- Side effects of other tuples disappear out of the view.
![Page 19: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/19.jpg)
cs3431
Multiple relation viewsConsider a multi-relation view such as
CREATE VIEW studentProf (studentname, profname)AS SELECT sName, pNameFROM Student, ProfessorWHERE SName = PName;
sNumber sName address professor
1 MM 320FL 1
2 MM 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 235FL
2 ER 241FL
Professor
![Page 20: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/20.jpg)
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.
![Page 21: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/21.jpg)
cs3431
Views - Example
sNumber sName address professor
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 235FL
2 ER 241FL
Professor
CREATE VIEW studentProfessor (student, professor) AS
SELECT sNumber, pNameFROM Student, ProfessorWHERE Student.professor =
Professor.pNumber;
student professor
1 MM
2 MM
3 ER
1. pNumber is key in Professor2. sNumber is key of Student3. sNumber is key of view
![Page 22: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/22.jpg)
cs3431
Deleting from multi-relation views
Try the following update statements:
DELETE FROM studentProfessor WHERE professor='MM';
-- What will be deleted ?
![Page 23: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/23.jpg)
cs3431
Views - Example
sNumber sName address professor
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 235FL
2 ER 241FL
Professor
CREATE VIEW studentProfessor (student, professor) AS
SELECT sNumber, pNameFROM Student, ProfessorWHERE Student.professor =
Professor.pNumber;
student professor
1 MM
2 MM
3 ER
DELETE FROM studentProfessor WHERE professor='MM';-- This will actually delete the two rows in the student table.
![Page 24: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/24.jpg)
cs3431
Views - Example
sNumber sName address professor
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 235FL
2 ER 241FL
Professor
CREATE VIEW studentProfessor (student, professor) AS
SELECT sNumber, pNameFROM Student, ProfessorWHERE Student.professor =
Professor.pNumber;
Suppose we drop key constraint on the professor table for this view.
Now delete will fail because there is no table whose key is the key of the view.
![Page 25: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/25.jpg)
cs3431
Inserting into multi-relation views
Consider view definition:CREATE VIEW studentProf(student, professor) AS SELECT sNumber, pName FROM Student, ProfessorWHERE 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.
![Page 26: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/26.jpg)
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.
![Page 27: Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update](https://reader036.vdocuments.net/reader036/viewer/2022062300/56649d595503460f94a3929a/html5/thumbnails/27.jpg)
cs3431
Views - Summary
Views are useful – Virtual relations
Querying through views is always possible
Updating through views has limitations