




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、CS542SQL Views1CS542SQL DML (Updating the Data) Insert Delete UpdateCS542Inserting tuplesINSERT INTO Student VALUES (6, Emily, 324 FL, NULL);INSERT INTO Student (sNumber, sName) VALUES (6, Emily);INSERT INTO Professor (pNumber)SELECT professorFROM Student;3CS542Delete and UpdateDeleting tuplesDELETE
2、 FROM StudentWHERE sNumber=6;Updating tuplesUPDATE Student SET professor=MickyMouseWHERE sNumber=64CS542ViewsNOTE: 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
3、 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.5CS542ViewsView is a virtual relation defined by:Named stored SQL queryViews can be queried like any “b
4、ase” relation.6CS542ViewsCREATE VIEW as CREATE VIEW studentProfessor (student, professor) ASSELECT sName, pNameFROM Student, ProfessorWHERE Sfessor = Professor.pNumber; DROP VIEW studentProfessor7CS542Views - ExamplesNumbersNameaddressprofessor1Dave320FL12Greg320FL13Matt320FL2StudentpNumbe
5、rpNameaddress1MM235FL2ER241FLProfessorCREATE VIEW studentProfessor (student, professor) ASSELECT sName, pNameFROM Student, ProfessorWHERE Sfessor = Professor.pNumber;SELECT * from studentProfessorstudentprofessorDaveMMGregMMMattER8CS542Views - ExamplesNumbersNameaddressprofessor1Dave320FL1
6、2Greg320FL13Matt320FL2StudentpNumberpNameaddress1MM235FL2ER241FLProfessorCREATE VIEW studentProfessor (student, professor) ASSELECT sName, pNameFROM Student, ProfessorWHERE Sfessor = Professor.pNumber;SELECT professor, count(*) FROM studentProfessorGROUPBY professor;studentprofessorDaveMMG
7、regMMMattER9CS542Querying ViewsCREATE VIEW studentProfessor (student, professor) ASSELECT sName, pNameFROM Student, ProfessorWHERE Sfessor = Professor.pNumber;SELECT pnumber as professor, count(*) FROM Student, ProfessorWHERE Sfessor = Professor.pNumberGROUPBY professor;studentpr
8、ofessorDaveMMGregMMMattERSELECT professor, count(*) FROM studentProfessorGROUPBY professor;10CS542Views ? 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.Custo
9、mization: Provide different views of the same data.Security: Expose only necessary data to users11CS542Updating ViewsConsider 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?
10、How ?Answer: Updating these views are done by updating the underlying Student tables.12CS542Updating Single Relation ViewsDELETE FROM MyStudent WHERE name=Dave;- This will delete the corresponding row from the Student tableDELETE FROM Student WHERE name=Dave;This update is valid ! 13CS542Updating Si
11、ngle Relation ViewsINSERT 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 ? 14CS542Inserting into single relation viewsCREATE VIE
12、W 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.15CS542Updating Single Relation viewsWhat about the views with DISTINCT
13、? Are they updatable?CREATE VIEW MyStudent2(name) ASSELECT DISTINCT sNameFROM Student;CREATE VIEW MyStudent3(num) ASSELECT DISTINCT sNumber FROM Student;If the SELECT clause specifies DISTINCT, then the view is not updatable.16CS542Updating Single Relation ViewsWHERE clause may specify subqueries. C
14、REATE VIEW MyStudent4 (num, name) ASSELECT sNumber, sName FROM StudentWHERE sName IN (SELECT pName FROM Professor);- Insert into this view will insert into Student table17CS542Updating Single Relation ViewsWHERE clause may specify subqueries. CREATE VIEW MyStudent4 (num, name) ASSELECT sNumber, sNam
15、e 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, even though that tuple does not appear in the view.18CS542Multiple relation views: DeleteConsider a multi-relation view such asCREATE V
16、IEW studentProf (studentname, profname)AS SELECT sName, pNameFROM Student, ProfessorWHERE SName = PName;sNumbersNameaddressprofessor1MM320FL12MM320FL13Matt320FL2StudentpNumberpNameaddress1MM235FL2ER241FLProfessor19CS542Multiple relation views: DeleteUpdating this multi-relation view?CREATE VIEW stud
17、entProf (studentname, profname)AS SELECT sName, pNameFROM Student, ProfessorWHERE SName = PName;- Ambigious what base table to update ! - Side effects as other tuples may disappear out of the view !20CS542Multi-Relation ViewDeletes can be done against multi-relation views if there is a table such th
18、at the view and the table have the same key.21CS542Views - ExamplesNumbersNameaddressprofessor1Dave320FL12Greg320FL13Matt320FL2StudentpNumberpNameaddress1MM235FL2ER241FLProfessorCREATE VIEW studentProfessor (student, profname) ASSELECT sNumber, pNameFROM Student, ProfessorWHERE Sfessor = P
19、rofessor.pNumber;studentprofname1MM2MM3ERpNumber is key in ProfessorsNumber is key of StudentsNumber is key of view22What is key in studentProfessor table?CS542Multi-Relation ViewReminder : Deletes can be done against multi-relation views if there is a table such that the view and the table have the
20、 same key.23CS542Deleting from multi-relation viewsTry the following update statements:DELETE FROM studentProfessor WHERE profname =MM;- What will be deleted ?24CS542Views - ExamplesNumbersNameaddressprofessor1Dave320FL12Greg320FL13Matt320FL2StudentpNumberpNameaddress1MM235FL2ER241FLProfessorCREATE
21、VIEW studentProfessor (student, professor) ASSELECT sNumber, pNameFROM Student, ProfessorWHERE Sfessor = Professor.pNumber;studentprofname1MM2MM3ERDELETE FROM studentProfessor WHERE profname=MM;- This will actually delete the two rows in the student table.25CS542Views - ExamplesNumbersName
22、addressprofessor1Dave320FL12Greg320FL13Matt320FL2StudentpNumberpNameaddress1MM235FL1ER241FLProfessorCREATE VIEW studentProfessor (student, professor) ASSELECT sNumber, pNameFROM Student, ProfessorWHERE Sfessor = Professor.pNumber;Suppose we drop key constraint on professor table for this v
23、iew.Now delete will fail because there is no table whose key is the key of the view.26CS542Inserting into multi-relation viewsConsider view definition:CREATE VIEW studentProf(student, professor) AS SELECT sNumber, pName FROM Student, ProfessorWHERE professor=pNumber;INSERT INTO Studentprof(student)
24、VALUES (4);- the above insert will succeed; put into Student tableINSERT INTO Studentprof VALUES (4, ER);- THIS ABOVE INSERT WILL FAIL AS IT TRIES TO INSERT INTO Professor TABLE AS WELL.27CS542Inserting into multi-relation viewsInsert 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.28CS542Controlling
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 通过实习项目提升学生实践能力计划
- 企业调岗合同样本
- 农村购买合同标准文本
- 农机抵押货款合同样本
- 传媒公司摄制合同标准文本
- 军人创业合同样本
- 2025四川省土地租赁合同范本
- app项目转让合同样本
- 养老服务平台服务合同标准文本
- 农村开荒种地合同样本
- 心理健康教育与校园文化建设融合探讨
- 小学生公共安全教育
- 无人机操控知识培训课件
- 环境健康与安全EHS管理制度
- 2025广东省广州打捞局编外人员招聘21名高频重点提升(共500题)附带答案详解
- 产科护理临床案例分享
- 中电建新能源集团股份有限公司云南分公司招聘笔试冲刺题2024
- 经皮放射性粒子植入护理全程管理专家共识
- 【名校区域大联考】专题04.阅读理解D篇(说明文或议论文为主)(解析版)
- 专题四 二次函数综合题(含答案)2025年中考数学一轮题型专练(陕西)
- 北京理工大学《操作系统原理》2022-2023学年第一学期期末试卷
评论
0/150
提交评论