SQL Updates Views
-
Upload
prabhanshu-pandey -
Category
Documents
-
view
227 -
download
0
Transcript of SQL Updates Views
-
8/8/2019 SQL Updates Views
1/18
Murali Mani
SQL: Updates (DML)and Views (DDL)
-
8/8/2019 SQL Updates Views
2/18
Murali Mani
SQL DML (Updating the Data)
Insert Delete
Update
-
8/8/2019 SQL Updates Views
3/18
Murali Mani
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;
-
8/8/2019 SQL Updates Views
4/18
Murali Mani
Delete and Update
Deleting tuples
DELETE FROM Student
WHERE sNumber=6;
Updating tuples
UPDATE Student SET professor=ERWHERE sNumer=6
-
8/8/2019 SQL Updates Views
5/18
Murali Mani
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.
-
8/8/2019 SQL Updates Views
6/18
Murali Mani
Views
View is a virtual relation
Convenience: Queries on base relations might be
complex
Logical Data Independence: base tables maychange, but still queries using views need not
change.
Provide different views of the same data.
Security: Expose only necessary data to users
Views can be queried like any base relation.
-
8/8/2019 SQL Updates Views
7/18
-
8/8/2019 SQL Updates Views
8/18
Murali Mani
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, pName
FROM Student, Professor
WHERE Student.professor =Professor.pNumber;
SELECT * from studentProfessor
student professor
Dave MM
Greg MM
Matt ER
-
8/8/2019 SQL Updates Views
9/18
Murali Mani
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;
These views are updatable. Updating theseviews are done by updating the underlying
Student tables.
-
8/8/2019 SQL Updates Views
10/18
Murali Mani
Updating Single relation Views
For instance, the following updates are valid:DELETE FROM MyStudent WHERE name=`Dave';
-- This will delete the corresponding rowfrom the Student table
INSERT INTO MyStudent VALUES (4, `Mary);
-- This will be translated to INSERT INTO
Student(sNumber, sName) VALUES (4,
`Mary);
-
8/8/2019 SQL Updates Views
11/18
Murali Mani
Inserting into single relation
views
Consider the viewCREATE 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
-
8/8/2019 SQL Updates Views
12/18
Murali Mani
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;
-
8/8/2019 SQL Updates Views
13/18
Murali Mani
Updating Single Relation
Views
Note that the WHERE clause may specifysubqueries. Let us consider an extremeexample.
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.
-
8/8/2019 SQL Updates Views
14/18
Murali Mani
Multiple relation views: Delete
Consider a multi-relation view such asCREATE VIEW studentProf(student, professor)
AS SELECT sName, pName
FROM Student, ProfessorWHERE professor=pNumber;
-- Note that the pNumber is key for professor. We
see that sNumber is a key for Student and also for
the view (though sNumber does not appear in theresult). So deleting from the views are possible
by deleting appropriate sNumbers from the Student
table.
-
8/8/2019 SQL Updates Views
15/18
-
8/8/2019 SQL Updates Views
16/18
Murali Mani
Deleting from multirelation
views
Suppose we drop the key constraint on the
professor table for the same view.
Now delete will fail because there is no table
whose key is the key of the view.
-
8/8/2019 SQL Updates Views
17/18
Murali Mani
Inserting into multi-relation
views
Consider the following slightly modified view
definitionCREATE 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 INSERTINTO Professor TABLE AS WELL.
INSERT INTO Studentprof(student) VALUES (4);
-- THIS ABOVE INSERT WILL SUCCEED.
-
8/8/2019 SQL Updates Views
18/18
Murali Mani
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 akey for the view.