Unit 2
Unit 2
Relational Model
A relational model is one of the database models that use tables to store the data in a simple way. Its
simplicity is due to the usage of tables in which an entity is represented as a table and its instance by
the rows of the table (tuples).
Example: A student’s entity is represented as table whereas an individual student corresponds to the
rows in the table. A relation in a relational model consists of
While describing a relation, a relation schema is defined first followed by the relation instance.
Relation Schema
A relation schema contains the basic information of a table or relation. This information includes the
name of the entire table, the names of the column and the data types associated with each column.
For Example: A relation schema for the relation called students could be expressed using the following
representation.
Students (sid : string, name: string, login : string, age : integer, gpa : real)
The name of the entire table or relation is “students”. There are five column names: sid, name, login,
age, gpa with respective data types: string, string, string, integer, real associated with them.
Relation Instance
A relation instance is a set of rows that when combined together forms the schema of the relation. A
relation instance can be thought of as a table in which each tuple is a row, and all rows have the same
number of fields.
Relational Database Schema
A relational database schema is a collection of relation schemas, describing one or more relations.
Domain
Domain is synonymous with data type. Attributes can be thought of as columns in a table. Therefore,
an attribute domain refers to the database associated with a column.
Relation Cardinality
The relation cardinality is the number of tuples in the relation.
Relation Degree
The relation degree is the number of fields in the relation.
Tuples / Records
The rows of the table are also known as records or tuples.
Field/Attributes
The columns of the table is also known as fields or attributes.
The CREATE TABLE statement is used to define a new table. To create the student relation, we can
use the following statement.
Syntax:
create table <table name> (column definition 1, column definition 2, …);
(CID INTEGER,
ACCNO INTEGER,
AMT REAL)
Thus, the above statement creates the students relation shown in Figure below by accepting the
values from the user.
Alter A Table
Alter Table statement is used to:-
o When a user wants to add a new column
o To change the width of a datatype or the datatype itself
o To include or drop integrity constraints.
Syntax
alter table <table name> modify (column definition 1, column definition 2, …);
alter table <table name> add (column definition 1, column definition 2, …);
Drop a column
If there is no further use of a particular column in table, Oracle provides the facility to drop it using
the Drop Column command.
o Syntax:
alter table <table name> drop column (column definition 1, column definition 2, …;
Truncate Table
If there is no further use of records stored in a table and the structure has to be retained then the
records alone can be deleted.
o Syntax:
truncate table <table name>;
By adding reuse storage clause to the same command the space that is used for the storage can be
reclaimed.
o Syntax:
truncate table <table name> reuse storage;
CID INTEGER,
ACCNO INTEGER,
AMT REAL
(CID, CNAME, ACCNO, BNAME, AMT) VALUES (6001, ‘Kirpal Singh’, 41, ‘UTI’,
15000.00);
The key words WHERE and SET are used to determine the modifying rows and the modifying
procedure (i.e., how the rows can be changed)
When a customer deposits some money in his account its instance can be updated as
Integrity constraint is a condition that ensures the correct insertion of the data and prevents
unauthorized data access thereby pressuring the consistency of data. It means that, DBMS specifies
some conditions that must be satisfied while inserting and storing the data in database which
prevents the entry of incorrect information.
Example: Fro example the roll number of a student cannot be a decimal value. The database enforces
the constraint that the instance of roll number. Can have only integer values.
Key Constraint
• A key constraint is a statement that a certain minimal fields of a relation has a unique identified
for all tuples.
• Actually key constraint is the general term, the term Candidate Key is used for satisfying the
constraints according to a key constraint.
Candidate Key
• A set of fields that uniquely identifies a tuple according to a key constraint is called a candidate
key for the relation.
Super Key
• A super Key is a superset of a candidate key. A super key is a set of fields, that each contains a
candidate key.
• For Example: The set of attributes fields {sid, login} is a super key. Here there is a constraint for
sid and there is a constraint for login separately. When we collect such types of fields with key
constraint i.e. candidate keys, then they form the super key.
Specifying Key constraint in SQL
In SQL, we can declare that column/fields of a table form a candidate key using two statements.
o Unique Key: The purpose of a unique key is to ensure that information in the column is Unique.
• The data held across the column MUST be UNIQUE; it must not be repeated across the
column.
• Any column can be left blank with NULL value.
• In the following example, no two students or more can have the same login. Bust atleast one
student can have NULL login.
o Primary Key: The purpose of Primary key is to ensure that information in the column is unique
and MUST be compulsory entered
• The data held across the column MUST be UNIQUE; it must not be repeated across the
column.
• Even one column also cannot be left blank; it must be compulsory entered i.e. the NOT NULL
attribute is active.
• In the above example, no two students or more can have the same sid and EVERY student
should have sid compulsorily.
Let us revisit out example of table definition and specify key information:
SQL > CREATE TABLE students
AGE INTEGER,
GPA REAL);
In the above example, no two students or more can have the same login. Bust atleast one student
can have NULL login.
Domain Constraints
A relational schema specified the domain of each field or column in the relation.
For Example: consider students relation
Students (sid: string, name: stirng, login: stirng, age: integer, gpa: real)
Here sid, name, login, age, gpa are column names with domains: stirng, string, string, integer, real
respectively.
Column Constraints
The value in any column of any table should be controlled by column constraints, which are
defined for that particular column, and recorded once and only once in central data dictionary.
Example:
SQL > CREATE TABLE PERSONAL
(EMPNO SMALLINT
This type of facility allows business rules to be applied centrally to the database, so the when a
certain action is performed on a set of data, other actions are automatically triggered, conforming
to some user-defined and centrally recorded set of rules.
In general terms such facilities are implemented as triggers and take the form of a sequence of
three types of definitions:
o Define the condition which actions the trigger
o Specify the test to be made
o Specify the action to be taken if interest fails.
General Constraints
A query language is a language in which user requests to retrieve some information from the
database. The query languages are considered as higher-level languages than programming
languages also.
Strong entity set. The primary key of the entity set becomes the primary key of the relation.
Weak entity set. The primary key of the relation consists of the union of the primary key of the strong
entity set and the discriminator of the weak entity set.
Relationship set. The union of the primary keys of the related entity sets becomes a super key of
the relation.
o For binary many-to-one relationship sets, the primary key of the “many” entity set becomes
the relation’s primary key.
o For one-to-one relationship sets, the relation’s primary key can be that of either entity set.
o For many-to-many relationship sets, the union of the primary keys becomes the relation’s
primary key
Views
In some cases, it is not desirable for all users to see the contents of the entire model
A relation that is not in the model but is made visible to a user as a “virtual relation” is called a view.
Views not available in MySQL 3.23
Prevent access to tables or columns using the access privilege system
View Definition
A view is defined using the create view statement which has the form
create view v as <query expression>
where <query expression> is any legal relational algebra query expression. The view name
is represented by v.
Once a view is defined, the view name can be used to refer to the virtual relation that the view
generates.
View definition is not the same as creating a new relation by evaluating the query expression
Rather, a view definition causes the saving of an expression; the expression is substituted into
queries using the view.
Tuples Inserted Into loan and borrower
View Expansion
As long as the view definitions are not recursive, this loop will terminate
Unit-2
Relational Algebra
Where p is a formula in propositional calculus consisting of terms connected by: (and), (or),
(not)
Each term is one of:
Example of selection:
branch-name=“Perryridge”(account)
Project Operation
The result is defined as the relation of k columns obtained by erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
E.g. To eliminate the branch-name attribute of account
account-number, balance (account)
Union Operation
Notation: r s
Defined as: r s = {t | t r or t s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column of r deals with the same type of values
as does the 2nd column of s)
Notation: r – s
Defined as: r – s = {t | t r and t s}
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Cartesian-Product Operation
Notation r x s
Defined as: r x s = {t q | t r and q s}
Assume that attributes of r(R) and s(S) are disjoint. (That is, R S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
Composition of Operations
A=C(r x s)
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Allows us to refer to a relation by more than one name.
Example: x (E)
returns the result of expression E under the name X, and with the
Banking Example
Example Queries
Find all loans of over $1200
amount > 1200 (loan)
Find the loan number for each loan of an amount greater than $1200
loan-number (amount > 1200 (loan))
Find the names of all customers who have a loan, an account, or both, from the bank
customer-name (borrower) customer-name (depositor)
Find the names of all customers who have a loan and an account at bank.
customer-name (borrower) customer-name (depositor)
Find the loan number for each loan of an amount greater than $1200
loan-number (amount > 1200 (loan))
Find the names of all customers who have a loan, an account, or both, from the bank
customer-name
Find the names of all customers who have a loan and account at the bank
customer-name
Find the names of all customers who have a loan at the Perryridge branch.
customer-name (branch-name=“Perryridge”
Find the names of all customers who have a loan at the Perryridge branch but do not have an
account at any branch of the bank.
customer-name (branch-name = “Perryridge”
Find the names of all customers who have a loan at the Perryridge branch.
Query 1
customer-name(branch-name = “Perryridge” (
borrower.loan-number = loan.loan-number(borrower x loan)))
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = “Perryridge”(loan)) x borrower))
balance(account) - account.balance
Formal Definition
A basic expression in the relational algebra consists of either one of the following:
o A relation in the database
o A constant relation
Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra
expressions:
o E1 E2
o E1 - E2
o E1 x E2
o p (E1), P is a predicate on attributes in E1
o s(E1), S is a list consisting of some of the attributes in E1
o x (E1), x is the new name for the result of E1
Additional Operations
We define additional operations that do not add any power to the relational algebra, but that
simplify common queries.
Set intersection
Natural join
Division
Assignment
Set-Intersection Operation
Notation: r s
Defined as:
r s ={ t | t r and t s }
Assume:
o r, s have the same arity
o attributes of r and s are compatible
Note: r s = r - (r - s)
Natural-Join Operation
Notation: r s
S = (E, B, D)
R – S = (A1, …, Am)
r s = { t | t R-S(r) u s ( tu r ) }
Property
Let q – r s
Then q is the largest relation satisfying q x s r
To see why
The assignment operation () provides a convenient way to express complex queries.
Write query as a sequential program consisting of
• a series of assignments
• followed by an expression whose value is displayed as a result of the query.
Assignment must always be made to a temporary relation variable.
Example: Write r s as
temp1 R-S (r)
temp2 R-S ((temp1 x s) – R-S,S (r))
result = temp1 – temp2
The result to the right of the is assigned to the relation variable on the left of the.
May use variable in subsequent expressions.
Example Queries
Find all customers who have an account from at least the “Downtwon” and the Uptwon” branches.
Query 1
CN(BN=“Downtwon”(depositor account))
CN(BN=“Uptwon”(depositor account))
Query 2
Find all customers who have an account at all branches located in Brooklyn city.
The content of the database may be modified using the following operations:
Deletion
Insertion
Updating
All these operations are expressed using the assignment operator.
Deletion
A delete request is a query where the selected tuples are removed from the database.
Can only delete whole tuples; cannot delete values on only particular attributes
A deletion is expressed in relational algebra by:
rr–E
Deletion Examples
D
loan
account account – r2
depositor depositor – r3
Insertion
The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.
Insertion Examples
Insert information in the database specifying that Smith has $1200 in account A-973 at the
Perryridge branch.
account account {(“Perryridge”, A-973, 1200)}
depositor
Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan
number serve as the account number for the new savings account.
r1 (branch-name = “Perryridge” (borrower loan))
Updating
A mechanism to change a value in a tuple without charging all values in the tuple
Use the generalized projection operator to do this task
r F1, F2, …, FI, (r)
Each Fi is either
the ith attribute of r, if the ith attribute is not updated, or,
if the attribute is to be updated Fi is an expression, involving only constants and the
attributes of r, which gives the new value for the attribute
Update Examples
where AN, BN and BAL stands for account number, branch name
Pay all accounts with balances over $10,000 6 % interest and pay all others 5%
account AN, BN, BAL * 1.06 ( BAL 10000 (account))
AN, BN, BAL * 1.05 (BAL 10000 (account))
x y x v y
5. Set of quantifiers:
Banking Example
Example Queries
Find the loan-number, branch-name, and amount for loans of over $1200
t | t loan t [amount] 1200}
Find the loan number for each loan of an amount greater than $1200
{t | s loan (t[loan-number] = s[loan-number] s [amount] 1200)}
Find the names of all customers having a loan, an account, or both at the bank
{t | s borrower( t[customer-name] = s[customer-name])
Find the names of all customers who have a loan and an account at the bank
{t | s borrower( t[customer-name] = s[customer-name])
u depositor( t[customer-name] = u[customer-name])
Find the names of all customers having a loan at the Perryridge branch
{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))}
Find the names of all customers who have a loan at the Perryridge branch, but no account at any
branch of the bank
Find the names of all customers having a loan from the Perryridge branch, and the cities they live
in
{t | s loan(s[branch-name] = “Perryridge”
u borrower (u[loan-number] = s[loan-number]
t [customer-name] = u[customer-name])
v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-city])))}
Find the names of all customers who have an account at all branches located in Brooklyn:
{t | c customer (t[customer.name] = c[customer-name])
s branch(s[branch-city] = “Brooklyn”
u account ( s[branch-name] = u[branch-name]
s depositor ( t[customer-name] = s[customer-name]
s[account-number] = u[account-number] )) )}
Safety of Expressions
Find the names of all customers who have a loan of over $1200
{ c | l, b, a ( c, l borrower l, b, a loan a > 1200)}
Find the names of all customers who have a loan from the Perryridge branch and the loan amount:
{ c, a | l ( c, l borrower b( l, b, a loan
b = “Perryridge”))}
Find the names of all customers having a loan, an account, or both at the Perryridge branch:
{ c | l ({ c, l borrower
b,a( l, b, a loan b = “Perryridge”))
a( c, a depositor
b,n( a, b, n account b = “Perryridge”))}
Find the names of all customers who have an account at all branches located in Brooklyn:
{ c | s, n ( c, s, n customer)
Safety of Expressions
{ x1, x2, …, xn | P(x1, x2, …, xn)} is safe if all of the following hold:
1. All values that appear in tuples of the expression are values from dom(P) (that is, the values
appear either in P or in a tuple of a relation mentioned in P).
2. For every “there exists” subformula of the form x (P1(x)), the subformula is true if an only
if P1(x) is true for all values x from dom(P1).
3. For every “for all” subformula of the form x (P1 (x)), the subformula is true if and only if P1(x)
is true for all values x from dom(P1).
SQL
Basic Structure
Set Operations
Aggregate Functions
Null Values
Nested Subqueries
Derived Relations
Views
Modification of the Database
Joined Relations
Data Definition Language
Schema Used in Examples
SQL is based on set and relational operations with certain modifications and
enhancements
A typical SQL query has the form:
The select clause can contain arithmetic expressions involving the operation, +, –, ,
and /, and operating on constants or attributes of tuples.
The query:
select loan-number, branch-name, amount 100
from loan
Would return a relation which is the same as the loan relations, except that the
attribute amount is multiplied by 100.
select loan-number
from loan
where amount between 90000 and 100000
Tuple Variables
Tuple variables are defined in the from clause via the use of the as clause.
Find the customer names and their loan numbers for all customers having a loan at
some branch.
select customer-name, T.loan-number, S.amount
from borrower as T, loan as S
where T.loan-number = S.loan-number
Find the names of all branches that have greater assets than some branch located in
Brooklyn.
String Operations
SQL includes a string-matching operator for comparisons on character strings.
Patterns are described using two special characters:
percent (%). The % character matches any substring.
underscore (_). The _ character matches any character.
Find the names of all customers whose street includes the substring “Main”.
select customer-name
from customer
where customer-street like ‘%Main%’
List in alphabetic order the names of all customers having a loan in Perryridge
branch
select distinct customer-name
from borrower, loan
where borrower loan-number - loan.loan-number and
branch-name = ‘Perryridge’
order by customer-name
We may specify desc for descending order or asc for ascending order, for each
attribute; ascending order is the default.
E.g. order by customer-name desc
Set Operations
union, intersect, and except
operate on relations
correspond to the relational algebra operations
Each of the above operations eliminates duplicates
to retain all duplicates use: union all, intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it occurs:
m + n times in r union all s
min(m,n) times in r intersect all s
max(0, m – n) times in r except all s
Find all customers who have a loan, an account, or both:
(select customer-name from depositor)
union
(select customer-name from borrower)
Aggregate Functions
Find the names of all branches where the average account balance is more than
$1,200.
select branch-name, avg (balance)
from account
group by branch-name
having avg (balance) > 1200
Note: Predicates in the having clause are applied after the formation of groups
whereas predicates in the where clause are applied before forming groups.
Null Values
Attributes may have the null value
The predicate is null can be used to check for null values.
E.g. Find all loan number which appear in the loan relation with null values
for amount.
select loan-number
from loan
where amount is null
Find all customers who have a loan at the bank but do not have
an account at the bank
select distinct customer-name
from borrower
where customer-name not in (select customer-name
from depositor)
Set Comparison
Find all branches that have greater assets than some branch located in Brooklyn.
select distinct T.branch-name
from branch as T, branch as S
where T.assets > S.assets and
S.branch-city = ‘Brooklyn’
F <comp> some r t r s.t. (F <comp> t) Where <comp> can be:
(= some) in However, ( some) not in
Example Query
Find the names of all branches that have greater assets than all branches located in
Brooklyn.
select branch-name
from branch
where assets > all
(select assets
from branch
where branch-city = ‘Brooklyn’)
Example Query
Find all customers who have an account at all branches located in Brooklyn.
select distinct S.customer-name
from depositor as S
where not exists (
(select branch-name
from branch
where branch-city = ‘Brooklyn’)
except
(select R.branch-name
from depositor as T, account as R
where T.account-number = R.account-number and
S.customer-name = T.customer-name))
Note that X – Y = Ø X Y
Note: Cannot write this query using = all and its variants
Views
Provide a mechanism to hide certain data from the view of certain users. To create a
view we use the command:
where:
Example Queries
Find the average account balance of those branches where the average account
balance is greater than $1200.
select branch-name, avg-balance
from (select branch-name, avg (balance)
from account
group by branch-name)
as result (branch-name, avg-balance)
where avg-balance > 1200
Example Query
Delete the record of all accounts with balances below the average at the bank.
2. Next, delete all tuples found above (without recomputing avg or retesting
the tuples)
Provide as a gift for all loan customers of the Perryridge branch, a $200 savings
account. Let the loan number serve as the account number for the new savings
account
insert into account
select loan-number, branch-name, 200
from loan
where branch-name = ‘Perryridge’
insert into depositor
select customer-name, loan-number
from loan, borrower
where branch-name = ‘Perryridge’
and loan.account-number = borrower.account-number
The select from where statement is fully evaluated before any of its results are
inserted into the relation (otherwise queries like
insert into table1 select * from table1
would cause problems
Modification of the Database – Updates
Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%.
Write two update statements:
update account
set balance = balance 1.06
where balance > 10000
update account
set balance = balance 1.05
where balance 10000
Update of a View
Create a view of all loan data in loan relation, hiding the amount attribute
create view branch-loan as
select branch-name, loan-number
from loan
Updates on more complex views are difficult or impossible to translate, and hence
are disallowed.
Most SQL implementations allow updates only on simple views (without aggregates)
defined on a single relation
Allows the specification of not only a set of relations but also information about each
relation, including:
where A is the name of the attribute to be added to relation r and D is the domain of
A.
All tuples in the relation are assigned null as the value for the new
attribute.
The alter table command can also be used to drop attributes of a relation
alter table r drop A
where A is the name of an attribute of relation r
Dropping of attributes not supported by many databases
Joins
Join types:
Inner join
Left outer join
Right outer join
Full outer join
Join conditions:
Natural
On <predicate>
Using (A1, …, An)
Join Conditions
a INNER JOIN b USING (c1, ..., cn)
{ n | c, a ( n, c, a branch c = “Brooklyn”)}
Join tricks
Restrict a to just those rows compatible with b
SELECT DISTINCT a.* FROM a NATURAL JOIN b;
E.g. list tuples from the borrower table only for those borrowers who are depositors:
SELECT DISTINCT borrower.*
FROM borrower NATURAL JOIN depositor;
A ⋈ C B ≝ C (A ⋈ B)
Find all customers who have an account at all branches located in Brooklyn.
SELECT branch-name
FROM branch
WHERE branch-city = ‘Brooklyn’
r2 = {t | d depositor, a account
(t[branch-name] = a[branch-name]
t[customer-name] = d[customer-name]
d[account-number] = a[account-number]) }
Find all customers who have an account at all branches located in Brooklyn
r2 = {t | d depositor, a account
(t[branch-name] = a[branch-name]
t[customer-name] = d[customer-name])
d[account-number] = a[account-number]) }
Exercises:
write this as a single expression
convert it into an expression in the domain relational calculus
Let r1(R1) and r2(R2) be relations with primary keys K1 and K2 respectively.
The subset of R2 is a foreign key referencing K1 in relation r1
if for every t2 in r2
there must be a tuple t1 in r1
such that t1[K1] = t2[].
E.g.
r1 = loan(loan-number, branch-name, amount)
r2 = borrower(customer-name, loan-number)
K1 = loan-number, K2 = customer-name
The loan-number attribute of the borrower relation is a foreign key referencing loan-
number in the loan relation because
for every tuple b in borrower
there is a tuple l in loan
such that loan[loan-number] = borrower[loan-number].
Primary and candidate keys and foreign keys can be specified as part of the SQL create table
statement:
The primary key clause:
• attributes that comprise the primary key
The unique key clause:
• attributes that comprise a candidate key
The foreign key clause:
• attributes that comprise the foreign key, and
• the name of the relation referenced by the foreign key
By default, a foreign key references the primary key attributes of the referenced table
foreign key (account-number) references account
(...
foreign key(branch-name) references branch
on delete cascade
on update cascade
...)
Alternative to cascading:
on delete set null
on delete set default
Null values in foreign key attributes complicate SQL referential integrity semantics, and are
best prevented using not null
if any attribute of a foreign key is null, the tuple is defined to satisfy the foreign key
constraint!
Assertions
When an assertion is made, the system tests it for validity, and tests it again on every update
that may violate the assertion
This testing may introduce a significant amount of overhead; hence assertions
should be used with great care.
Asserting
for all X, P(X)
is achieved indirectly using
not exists X such that not P(X)
Assertions Examples
The sum of all loan amounts for each branch must be less than the sum of all account
balances at the branch.
create assertion sum-constraint check
(not exists (select * from branch
where (select sum(amount) from loan
where loan.branch-name =
branch.branch-name)
>= (select sum(amount) from account
where loan.branch-name =
branch.branch-name)))
Every loan has at least one borrower who maintains an account with a minimum balance of
$1000.00
Triggers
Suppose that instead of allowing negative account balances, the bank deals with overdrafts
by
setting the account balance to zero
creating a loan in the amount of the overdraft
giving this loan a loan number identical to the account number of the overdrawn
account
The condition for executing the trigger is an update to the account relation that results in a
negative balance value.
Triggers can be activated before an event, which can serve as extra constraints. E.g. convert
blanks to null.
Instead of executing a separate action for each affected row, a single action can be executed
for all rows affected by a transaction
Use for each statement instead of for each row
Use referencing old table or referencing new table to refer to temporary tables
(called transition tables) containing the affected rows
Can be more efficient when dealing with SQL statements that update a large number
of rows
External World Actions
from minlevel
from minlevel
begin
from reorder
end
When Not To Use Triggers
Authorization
Users can be given authorization on views, without being given any authorization on the
relations used in the view definition
Ability of views to hide data serves both to simplify usage of the system and to enhance
security by allowing users access only to data they need for their job
A combination or relational-level security and view-level security can be used to limit a user’s
access to precisely the data that user needs.
View Example
Suppose a bank clerk needs to know the names of the customers of each branch, but is not
authorized to see specific loan information.
Approach: Deny direct access to the loan relation, but grant access to the view cust-
loan, which consists only of the names of customers and the branches at which they
have a loan.
The cust-loan view is defined in SQL as follows:
create view cust-loan as
select branchname, customer-name
from borrower, loan
where borrower.loan-number = loan.loan-number
When the query processor translates the result into a query on the actual relations in the
database, we obtain a query on borrower and loan.
Authorization must be checked on the clerk’s query before query processing replaces a view
by the definition of the view.
Authorization on Views
Creation of view does not require resources authorization since no real relation is being
created
The creator of a view gets only those privileges that provide no additional authorization
beyond that he already had.
E.g. if creator of view cust-loan had only read authorization on borrower and loan, he gets
only read authorization on cust-loan
Granting of Privileges
Authorization Graph
Privileges in SQL
select: allows read access to relation,or the ability to query using the view
Example: grant users U1, U2, and U3 select authorization on the branch relation:
grant select on branch to U1, U2, and U3
with grant option: allows a user who is granted a privilege to pass the privilege on to other
users.
Example:
grant select on branch to U1 with grant option
privilege to others
Roles
Roles permit common privileges for a class of users can be specified just once by creating a
corresponding “role”
Privileges can be granted to or revoked from roles, just like user
Roles can be assigned to users, and even to other roles
SQL:1999 supports roles
create role teller
create role manager
Example:
revoke select on branch from U1, U2, U3 cascade
Revocation of a privilege from a user may cause other users also to lose that privilege;
referred to as cascading of the revoke.
We can prevent cascading by specifying restrict:
revoke select on branch from U1, U2, U3 restrict
With restrict, the revoke command fails if cascading revokes are required.
<privilege-list> may be all to revoke all privileges the revokee may hold.
If <revokee-list> includes public all users lose the privilege except those granted it explicitly.
If the same privilege was granted twice to the same user by different grantees, the user may
retain the privilege after the revocation.
All privileges that depend on the privilege being revoked are also revoked.
Limitations of SQL Authorization