5.23 Views in SQL
Views are virtual tabular variables. This means that in SQL a view is referenced
for retrieval purposes in exactly the same way a tabular variable is referenced.
The only difference is that a view does not have a physical existence. It exists
only as a definition in the database catalog. We refer to “real” tabular variables
(that is, the tabular variables that have a physical existence in the database) as
base tabular variables.
Views are supported in both SQLPlus and in Transact SQL but not in the
current version (4.1) of MySQL.
To illustrate the notion of view, let us consider the following example.
Example 5.23.1 Suppose that we write:
create view STC as
select STUDENTS.name, GRADES.cno
from STUDENTS, GRADES
where STUDENTS.stno = GRADES.stno;
The select construct contained by this create view retrieves all pairs of
student names and course numbers such that the student whose name is s has
registered for the course c.
When this directive is executed by SQL, no data retrieval takes place. The
database system simply stores this definition in its catalog. The definition of the
view STC becomes a persistent object, that is, an object that exists after our
interaction with the DBMS has ceased. From a conceptual point of view, the
user treats STC exactly like any other tabular variable. Suppose, for instance
that we wish to retrieve the names of students who took cs110. In this case it
is sufficient to write the query:
5.23 Views in SQL 155
select name from STC where cno =’cs110’;
In reality, SQL combines this select phrase with the query just shown and
executes the modified query:
select STUDENTS.name from STUDENTS, GRADES
where STUDENTS.stno = GRADES.stno
and GRADES.cno =’cs110’;
The previous example shows that views in SQL play a role similar to the role
played by macros in programming languages.
Views are important for data security. A user who needs to have access only
to list of names of students and the courses they are taking needs to be aware
only of the existence of STC. If the user is authorized to use only select con-
structs, then the user can ignore whether STC is a table or a view. Confidential
data (such as grades obtained in specific courses) can be completely protected
in this manner. Also, the queries that this limited-access user may write are
simpler and easier to understand. No space is wasted with the view STC, and
the view remains current always, reflecting the contents of the tabular variables
STUDENTS and GRADES.
SQL treats views exactly as it treats the tabular variables as far as retrieval
is concerned. We can also delegate the select privilege to a view in exactly
the same way as we did for a tabular variable. For instance, if the user george
created the view STC, then he can give the select right to vanda by writing:
grant select on STC to vanda;
Consider now another example of view:
Example 5.23.2 The view SNA that contains the student number and the
names of students can be created by:
create view SNA as
select stno, name from STUDENTS
The purpose of this view is to insure privacy to students. Any user who has
access only to this view can retrieve the student number and name of a student,
but not the address of the student.
There is a fundamental difference between the views introduced in Exam-
ples 5.23.1 and 5.23.2, and this refers to the ways in which these two views
behave with respect to updates.
Suppose that the user wishes to insert the pair (7799, ’Jane Jones’) in the
view SNA. The user may ignore entirely the fact that SNA is not a base tabular
variable. On the other hand, the effect on the base tabular variable of this
insertion is unequivocally determined: the system inserts in the tabular variable
STUDENTS the tuple (7799, ’Jane Jones’, null, null, null). On the other hand,
we cannot insert a tuple in a meaningful way in the view STC introduced in
Example 5.23.1. Indeed if we attempt to insert a pair (s, c) in STC, then we have
to define the effect of this insertion on the base tabular variable. This is clearly
156 SQL — The Relational Language
impossible: we do not know what the student number is, what the identification
of the instructor is, etc. SQL forbids users to update views based on more than
one table (as STC is). Even if such updates would have an unambiguous effect
on the base tabular variable, this rule rejects any such update. Only some views
based on exactly one tabular variable can be updated. It is the responsibility
of the database administrator to grant to the user the right to update a view
only if that view can be updated.
If a view can be updated, then its behavior is somewhat different from the
base tabular variable on which the view is built. An update made to a view
may cause one or several tuples to vanish from the view, whenever we retrieve
the tuples of the view.
Example 5.23.3 Consider the view uppergr defined by:
create view UPPERGR as
select * from GRADES where grade > 75;
If we wish to examine the tuples that satisfy the definition of the view we use
the construction:
select * from UPPERGR;
that returns the result:
STNO EMPNO CNO SEM YEAR GRADE
---------- ----------- ----- ------ ---------- ----------
2661 019 cs110 FALL 1999 80
3566 019 cs110 FALL 1999 95
5544 019 cs110 FALL 1999 100
3566 019 cs240 SPRING 2000 100
2415 019 cs240 SPRING 2000 100
5571 234 cs410 SPRING 2000 80
1011 019 cs210 FALL 2000 90
3566 019 cs210 FALL 2000 90
5571 019 cs210 SPRING 2001 85
1011 056 cs240 SPRING 2001 90
4022 056 cs240 SPRING 2001 80
2661 234 cs310 SPRING 2001 100
The update construction:
update UPPERGR
set grade = 70
where stno = ’2661’ and empno = ’019’ and cno = ’cs110’
and sem = ’FALL’ and year = 1999;
makes the first row disappear, since it no longer satisfies the definition of the
view. Indeed, if we use again the same query on UPPERGR, we obtain:
STNO EMPNO CNO SEM YEAR GRADE
5.23 ครั้งใน SQL
ครั้งเป็นตัวแปรตารางเสมือน ซึ่งหมายความว่าใน SQL มุมมองที่มีการอ้างอิง
เพื่อวัตถุประสงค์ในการดึงตรงเช่นเดียวกับตัวแปรตารางมีการอ้างอิง.
ความแตกต่างเพียงอย่างเดียวคือมุมมองที่ไม่ได้มีการดำรงอยู่ทางกายภาพ มันมีอยู่
เพียง แต่เป็นความหมายในแคตตาล็อกฐานข้อมูล เราหมายถึง "ของจริง" ตัวแปรตาราง
(นั่นคือตัวแปรตารางที่มีการดำรงอยู่ทางกายภาพในฐานข้อมูล) เป็น
ตัวแปรตารางฐาน.
ผู้เข้าชมได้รับการสนับสนุนทั้งใน SQLPlus และในการทำธุรกรรมของ SQL แต่ไม่ได้อยู่ใน
รุ่นปัจจุบัน (4.1) ของ MySQL .
เพื่อแสดงให้เห็นความคิดของมุมมองให้เราพิจารณาตัวอย่างต่อไปนี้.
5.23.1 ตัวอย่างสมมติว่าเราเขียน:
สร้างมุมมองที่เป็นเอสทีซี
เลือก STUDENTS.name, GRADES.cno
จากนักเรียน, การเรียน
ที่ STUDENTS.stno = GRADES.stno;
เลือกสร้างที่มีอยู่โดยสร้างมุมมองนี้จะเรียกคู่ของ
ชื่อนักเรียนและตัวเลขดังกล่าวแน่นอนว่านักเรียนที่มีชื่อ S ได้
ลงทะเบียนสำหรับการเรียนการสอนค.
เมื่อสั่งนี้จะถูกดำเนินการโดย SQL, การดึงข้อมูลไม่เกิดขึ้น
ระบบฐานข้อมูลเพียงเก็บความหมายในแคตตาล็อกนี้ ความหมายของ
เอสทีซีวิวจะกลายเป็นวัตถุถาวรที่เป็นวัตถุที่มีอยู่หลังจากที่เรา
มีปฏิสัมพันธ์กับ DBMS ได้หยุด จากจุดแนวความคิดในมุมมองของ
ผู้ใช้เอสทีซีถือว่าเหมือนกับตัวแปรตารางอื่น ๆ สมมติเช่น
ที่เราต้องการที่จะดึงชื่อของนักเรียนที่เข้ามา cs110 ในกรณีนี้มัน
จะเพียงพอที่จะเขียนแบบสอบถาม:
5.23 ครั้งใน SQL 155
เลือกชื่อจากเอสทีซีที่ CNO = 'cs110';
ในความเป็นจริงนี้ SQL รวมวลีเลือกด้วยแบบสอบถามแสดงเพียงและ
ดำเนินการแก้ไขแบบสอบถาม:
เลือก STUDENTS.name จากนักเรียน, การเรียน
ที่ STUDENTS.stno = GRADES.stno
และ GRADES.cno = 'cs110';
ตัวอย่างก่อนหน้านี้แสดงให้เห็นว่ามุมมองใน SQL มีบทบาทคล้ายกับบทบาท
. เล่นโดยแมโครในการเขียนโปรแกรมภาษา
อ่านมีความสำคัญต่อความปลอดภัยของข้อมูล ผู้ใช้ที่ต้องการให้มีการเข้าถึงเท่านั้น
ที่จะมีรายชื่อของนักเรียนและหลักสูตรที่พวกเขามีการตอบสนองความต้องการที่จะทราบ
เพียงอย่างเดียวของการดำรงอยู่ของเอสทีซี หากผู้มีอำนาจที่จะใช้เพียงเลือกทำา
structs แล้วผู้ใช้สามารถละเว้นไม่ว่าจะเป็นเอสทีซีเป็นตารางหรือมุมมอง ความลับของ
ข้อมูล (เช่นเกรดที่ได้รับในหลักสูตรเฉพาะ) สามารถป้องกันได้อย่างสมบูรณ์
ในลักษณะนี้ นอกจากนี้คำสั่งที่ว่านี้ผู้ใช้ จำกัด การเข้าถึงอาจเขียนเป็น
ง่ายและง่ายต่อการเข้าใจ ไม่มีพื้นที่จะสูญเสียกับเอสทีซีมุมมองและ
มุมมองที่ยังคงอยู่ในปัจจุบันมักจะสะท้อนให้เห็นถึงเนื้อหาของตัวแปรตาราง
นักเรียนและการเรียน.
SQL มุมมองถือว่าตรงตามที่ให้การปฏิบัติต่อตัวแปรตารางเท่าที่ดึง
เป็นห่วง นอกจากนี้เรายังสามารถมอบหมายสิทธิ์เลือกไปยังมุมมองในตรง
เช่นเดียวกับที่เราทำสำหรับตัวแปรตาราง ตัวอย่างเช่นหากผู้ใช้ george
สร้างมุมมอง STC แล้วเขาสามารถให้สิทธิเลือกเพื่อแวนด้าโดยการเขียน:
ทุนเลือกใน STC เพื่อแวนด้า;
พิจารณาในขณะนี้ตัวอย่างของมุมมองอื่น:
ตัวอย่าง 5.23.2 SNA มุมมองที่มีจำนวนนักเรียน และ
ชื่อของนักเรียนที่สามารถสร้างขึ้นโดย:
สร้างมุมมอง SNA เป็น
stno เลือกชื่อจากนักเรียน
วัตถุประสงค์ของมุมมองนี้คือการประกันความเป็นส่วนตัวให้กับนักเรียน ผู้ใช้ที่มี
การเข้าถึงเฉพาะมุมมองนี้สามารถดึงข้อมูลจำนวนนักเรียนและชื่อของนักเรียน
แต่ไม่อยู่ของนักเรียน.
มีความแตกต่างพื้นฐานระหว่างมุมมองที่นำมาใช้ใน Exam- เป็น
Ples 5.23.1 และ 5.23.2 และ นี้หมายถึงวิธีการที่ทั้งสองมุมมองที่
มีลักษณะการทำงานที่เกี่ยวกับการปรับปรุง.
สมมติว่าผู้ใช้มีความประสงค์จะใส่คู่ (7799, 'เจนโจนส์) ใน
มุมมอง SNA ผู้ใช้อาจไม่สนใจทั้งหมดความจริงที่ว่า SNA ไม่ตารางฐาน
ตัวแปร ในทางตรงกันข้าม, ผลกระทบต่อตัวแปรตารางฐานนี้
แทรกจะถูกกำหนดอย่างแจ่มแจ้ง: แทรกระบบในตัวแปรตาราง
นักเรียน tuple (7799, 'เจนโจนส์', null, null, null) ในทางตรงกันข้าม,
เราไม่สามารถแทรก tuple ในทางที่มีความหมายในมุมมอง STC นำมาใช้ใน
ตัวอย่าง 5.23.1 อันที่จริงถ้าเราพยายามที่จะแทรกคู่ (s ค) ในเอสทีซีแล้วเรามี
การกำหนดผลของการแทรกนี้บนตัวแปรตารางฐาน นี้จะชัดเจน
156 SQL - ภาษาเชิงสัมพันธ์
เป็นไปไม่ได้เราไม่ทราบว่าจำนวนนักเรียนเป็นสิ่งที่บัตรประจำตัว
ของผู้สอนคือ ฯลฯ SQL ห้ามผู้ใช้สามารถอัปเดตขึ้นอยู่กับมุมมองมากกว่า
หนึ่งตาราง (เช่นเอสทีซีเป็น) แม้ว่าการปรับปรุงดังกล่าวจะมีผลกระทบที่ชัดเจน
เกี่ยวกับตัวแปรตารางฐานกฎนี้ปฏิเสธใด ๆ การปรับปรุงดังกล่าว เพียงมุมมองบางอย่าง
ขึ้นอยู่กับว่าหนึ่งตัวแปรตารางสามารถปรับปรุงได้ มันเป็นความรับผิดชอบ
ของผู้ดูแลระบบฐานข้อมูลเพื่อให้กับผู้ใช้สิทธิในการปรับปรุงมุมมอง
เฉพาะในกรณีที่เห็นว่าสามารถปรับปรุง.
ถ้ามีมุมมองที่สามารถปรับปรุงได้แล้วพฤติกรรมของมันจะค่อนข้างแตกต่างจาก
ตัวแปรตารางฐานที่ดูเป็น สร้าง การปรับปรุงที่เกิดขึ้นกับมุมมองที่
อาจก่อให้เกิดสิ่งอันดับหนึ่งหรือหลายที่จะหายไปจากมุมมองเมื่อใดก็ตามที่เราเรียก
tuples ของมุมมอง.
ตัวอย่าง 5.23.3 พิจารณามุมมอง uppergr กำหนดโดย:
UPPERGR สร้างมุมมองที่เป็น
* เลือกจากการเรียนที่คะแนน> 75 ;
ถ้าเราต้องการที่จะตรวจสอบ tuples ที่ตอบสนองความหมายของมุมมองที่เราใช้ใน
การก่อสร้าง:
* เลือกจาก UPPERGR;
ที่ส่งกลับผล:
STNO EMPNO CNO SEM ชั้นปี
---------- ---- ------- ----- ------ ---------- ----------
2661 019 cs110 FALL 1999 80
3566 019 cs110 FALL 1999 95
5544 019 cs110 FALL 1999 100
3566 019 cs240 SPRING 2,000 100
2,415 019 cs240 SPRING 2000 100
5571 234 cs410 SPRING 2000 80
1011 019 cs210 ฤดูใบไม้ร่วง 2000 90
3566 019 cs210 ฤดูใบไม้ร่วง 2000 90
5571 019 cs210 SPRING 2001 85
1011 056 cs240 SPRING 2001 90
056 4022 cs240 SPRING 2001 80
2661 234 CS310 SPRING 2001 100
ก่อสร้างปรับปรุง:
ปรับปรุง UPPERGR
ชุดชั้นประถมศึกษาปี 70 =
ที่ stno = '2661' และ empno = '019' และ CNO = 'cs110'
และ SEM = 'FALL' และในปี = 1999;
ทำให้ แถวแรกหายไปเพราะมันไม่ตรงกับความหมายของ
มุมมอง อันที่จริงถ้าเราใช้อีกครั้งแบบสอบถามเดียวกันใน UPPERGR เราได้รับ:
STNO EMPNO CNO SEM ปีระดับการศึกษา
การแปล กรุณารอสักครู่..
