5.22 Access Rights 153
Example 5.21.5 The following delete eliminates all rows of the table ASSIGN:
delete from ASSIGN;
The syntax of delete is: delete from table name [wherecondition]
5.22 Access Rights
The grant operation assigns access rights to users. To delegate access rights to other users, a user must “own” these rights. The set of access rights includes select, insert, update, and delete and refers to the right of executing each of these operations on a table. Further, update can be restricted to specific columns. All these access rights are granted to the creator of a table automatically. The creator, in turn, may grant access rights to other users or to all users (designated in SQL as public). The SQL standard envisions a mechanism that can limit the excessive proliferation of access rights. Namely, a user may receive the select right with or without the right to grant this right to others by his own action.
Example 5.22.1 Suppose that the user alex owns the table COURSES and intends to grant this right to the user whose name is peter. The user alex can accomplish this by
grant select on COURSES to peter
Now, peter has the right to query the table COURSES but he may not propagate this right to the user ellie. In order for this to happen, alex would have to use the directive:
grant select on COURSES to peter with grant option
Example 5.22.2 If peter owns the table STUDENTS, then he may delegate the right to query the table and the right to update the columns addr, city and zip to ellie using the directive:
grant select, update(addr, city, zip) on STUDENTS to ellie
The standard syntax of grant is:
154 SQL — The Relational Language
grant {priv{,priv} | all [privileges]} on [table] tablename{,tablename} to husername{,username}|publici [with grant option] Here priv has the syntax: hselect|insert|delete|update[(attribute{,attribute})]i Privileges can be revoked using the revoke construct, which is a feature of standard SQL. For instance, if peter wishes to revoke ellie’s privileges to update the table STUDENTS, he may write:
revoke update(addr,city,zip) on STUDENTS from ellie
The standard syntax for this directive is revoke {priv{,priv}|all [privileges]} on [table] tablename{,tablename} from husername{,username}|publici
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 constructs, 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 Examples 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 ---------- ----------- ----- ------ ---------- ---------3566 019 cs110 FALL 1999 95 5544 019 cs110 FALL 1999 100
5.23 Views in SQL 157
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
To reestablish the previous content of GRADES, we can use the update:
update UPPERGR set grade = 80 where stno = ’2661’ and empno = ’019’ and cno = ’cs110’ and sem = ’FALL’ and year = 1999;
The standard syntax of create view allows us to use the clause with check option. When this clause is used, every insertion and update done through the view is verified to make sure that a tuple inserted through the view actually appears in the view and an update of a row in the view does not cause the row to vanish from the view. The syntax of create view is:
create view view as subselect [with check option] A view V can be dropped from a database by using the construct
drop view V;
If we drop a tabular variable from the database, then all views based on that table are automatically dropped; if we drop a view, then all other views that use the view that we drop are also dropped. Views are useful instruments in implementing generalizations. Suppose, that we began the construction of the college database from the existing tabular variables UNDERGRADUATES and GRADUATES that modelled sets of entities having the same name, where
heading(UNDERGRADUATES) = stno name addr city state zip major heading(GRADUATES) = stno name addr city state zip qualdate
Then, the tabular variable STUDENTS could have been obtained as a view built from the previous two base tabular variables by
create view STUDENTS as select stno name addr city state zip from U