1 / 25

Teammembers: HuangWenjuan HeJu WanJianmei YangWenjing ZhangJinlian ZhengLinjie Made By:

The Standard Language Of DB SQL. Teammembers: HuangWenjuan HeJu WanJianmei YangWenjing ZhangJinlian ZhengLinjie Made By: HuangWenjuan ZhangJinlian. 3.6View.

pippa
Download Presentation

Teammembers: HuangWenjuan HeJu WanJianmei YangWenjing ZhangJinlian ZhengLinjie Made By:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Standard Language Of DB SQL Teammembers: HuangWenjuan HeJu WanJianmei YangWenjing ZhangJinlian ZhengLinjie Made By: HuangWenjuan ZhangJinlian

  2. 3.6View • First of all we have to claim that most of our examples are from the book,but some are made up by ourselves.If there are any mistakes,you can point them out pointedly ,we are too glad to accept your advice. • Then let’s begin our lesson.(WHO?)

  3. What is view? View is also a kind of table,which is derived from one or more basic tables.It’s a virtual table. • In the DB,only the definition of the view exists without the data which remain in the old tables. • The view changes with the basic tables. It’s a special kind of table ,so you can also do the updating manipulations with some restrictions.

  4. 3.6.1 Definition Of View • 1.How to create view? We can use the command CREATE VIEW to create a view.The common form is as follows: CREATE VIEW <view_name>[(<col1_name>[,<col2_name>]…)] as <sub SQL> [WITH CHECK OPTION];

  5. The subqueries can be any kind of SQLbut ORDER BYorDISTINCTcommand. • WITH CHECK OPTIONrequests that when making UPDATE,INSERT,DELETE manipulations the rows you are to delete should agree with the conditions. • Tips: all the attributes of the view should be given in the situation as follows:

  6. First, some of the attributes are Aggregated functions(聚集函数) or expressions. • Eg1 CREATE VIEW S_C(Sno,SDPcount) as SELECT Sno,COUNT(Sdept) FROM Student ORDER BY Sno;

  7. Second, create view with the attributes of one name in different tables when making multi_table connection. • Eg2 CREATE VIEW IS_S1(Sno,Sname,Grade) AS SELECT Student.Sno,Sname,Grade FROM Student,SC WHERE Sdept=‘IS’ AND Student.Sno=SC.Sno AND SC.Cno=‘1’;

  8. Third ,when you rename some attributes. Eg3 CREATE VIEW IS_S2(StudentNo,StudentName) AS SELECT Sno,Sname FROM Student WHERE Student.Sdept=‘IS’;

  9. If you leave out the attributes’ names of the view,it implicates that all the attributes are decided by the SELECT command • Eg4 CREATE VIEW IS_Student AS SELECT Sno,Sname,Sage FROM Student WHERE Sdept=‘IS’;

  10. RDBMS just stores the definition of the view into the data dictionary without execute the SELECT command. • Eg5 CREATE VIEW IS_Student AS SELECT Sno, Sname,Sage FROM Student WHERE Sdept=‘IS’; WITH CHECK OPTION;

  11. In the example above, because you have add the sentence of WITH CHECK OPTION, RDBMS will attach the condition of Sdept=‘IS’ automatically whenever you execute any manipulation on the view in the future. • 行列子视图 a view that is derived from one single basic table being deleted some rows and some columns and remaining the primary key.

  12. View can also be based on several basic tables . • Eg6: create a view of the IS students who have chosen NO.1 course. • CREATE VIE IS_S1(Sno, Sname , Grade) AS SELECT Student. Sno, Sname,Grade FROM Student, SC WHERE Sdept=‘IS’ AND Student .Sno=SC. Sno AND SC. Cno=‘ 1’;

  13. In Eg6 the IS_S1 view contains the attribute Sno which has the same name in both table Student and table SC,so each attribute name should be given. • View can be builded on the views too.

  14. Eg6: build a view based on the information of IS students who have chosen NO.1 course as well as the grade is not less than 90. CREATE VIEW IS_S2 AS SELECT Sno,Sname,Grade FROM IS_S1 WHERE Grade>=90;

  15. Virtual column the derived attribute columns in the view, whose data come from other data but do not exist exactly. View with the expression the view with the virtual columns.

  16. Eg7:define a view reflecting the information about the birthdays of the students. CREATE VIEW BT_S(Sno,Sname,Sbirth) AS SELECT Sno,Sname,2004-Sage FROM Student;

  17. Grouping View the view defined by the sentence of Aggregated functions(聚集函数) and the command of GROUP BY. Eg8: CREATE VIEW S_G(Sno, Gavg) AS SELECT SC FROM SC GROUP BY Sno; Here we use the function AVG() to produce Gavg,so the view should definitely list out each attribute name,S_G is a Grouping View

  18. Tip: To avoid some problems, we’d better delete the view derived from the table we have made changes to. 2.Drop The View the general form is as follows: DROP VIEW <view_name>[CASsCADE] One question: When we drop the view what should we pay attention to?

  19. attention: if you have derived views from the view you want to drop,you should use the command CASsCADE to drop all the derived views. • Assume that you create a view IS_S2 that is based on IS_S1,and now you are to delete the IS_S1,the exact sentence is as follows: DROP VIEW IS_S1 CASsCADE;

  20. 3.6.2 Query The View What is View Resolution? Before the query, RDBMS will check that whether the table, the view and items like that do exist. If so , the query about the view will be changed into the equivalent query about the basic tables. The process of the change is called View Resolution • Eg9 : • SELECT Sno, Sage • FROM IS_Student • WHERE Sage<20;

  21. So after the view resolution, the result is as follows: SELECT Sno,Sage FROM Student WHERE Sdept=‘IS’ AND Sage<20; • Eg10 • SELECT IS_Student. Sno,Sname • FROM IS_Student, SC • WHERE IS_Student.Sno=SC.Sno AND SC.Cno=‘1’;

  22. The query above refers to a virtual table (view) and a table. Sometimes the view resolution will give birth to potential problems .Please look at the example followed. • Eg11 • SELECT * • FROM S_G • WHERE Gavg>=90;

  23. The sub query is: • SELECT Sno, AVG(Grade) • FROM SC • GROUP BY Sno; So the eventual result is : • SELECT Sno, AVG(Grade) • FROM SC • WHERE AVG(Grade)> =90 • GROUP BY Sno;

  24. The problem is that in the sentence of WHERE we can not use the conditional expression. So it’s obvious that syntax error is to arise .The exact change is as follows : • SELECT Sno,AVG(Grade) • FROM SC • GROUP BY Sno • HAVING AVG(Grade)>=90;

  25. The End ,Thank You!

More Related