Intermediate Intermediate SQL SQL
Intermediate Intermediate SQL SQL
dept_name varchar(20),
foreign key (dept_name) references department
on delete cascade
on update cascade,
. . .
)
alternative actions to cascade: set null, set default
6
Integrity Constraint Violation During Transactions Integrity Constraint Violation During Transactions
E.g.
create table person (
ID char(10),
name char(40),
mother char(10),
father char(10),
primary key ID,
foreign key father references person,
foreign key mother references person)
How to insert a tuple without causing constraint violation ?
insert father and mother of a person before inserting person
OR, set father and mother to null initially, update after
inserting all persons (not possible if father and mother
attributes declared to be not null)
OR defer constraint checking
Complex Check Clauses Complex Check Clauses
check (time_slot_id in (select time_slot_id from time_slot))
why not use a foreign key here?
Every section has at least one instructor teaching the section.
how to write this?
Unfortunately: subquery in check clause not supported by
pretty much any database
Alternative: triggers (later)
create assertion <assertion-name> check <predicate>;
Also not supported by anyone
Built Built--in Data Types in SQL in Data Types in SQL
date: Dates, containing a (4 digit) year, month and date
Example: date 2005-7-27
time: Time of day, in hours, minutes and seconds.
Example: time 09:00:30 time 09:00:30.75
timestamp: date plus time of day
Example: timestamp 2005-7-27 09:00:30.75
interval: period of time
Example: interval 1 day
Subtracting a date/time/timestamp value from another gives
an interval value
Interval values can be added to date/time/timestamp values
Has functions to extract parts
extract (day from <date-type-field>)
Index Creation Index Creation
create table student
(ID varchar (5),
name varchar (20) not null,
dept_name varchar (20),
tot_cred numeric (3,0) default 0,
primary key (ID))
create index studentID_index on student(ID)
Indices are data structures used to speed up access to records
with specified values for index attributes
e.g. select *
from student
where ID = 12345
can be executed by using the index to find the required record,
without looking at all records of student
More on indices in Chapter 11
User User--Defined Types Defined Types
create type construct in SQL creates user-defined type
create type Dollars as numeric (12,2) final
create table department
(dept_name varchar (20),
building varchar (15),
budget Dollars);
Domains Domains
create domain construct in SQL-92 creates user-defined domain
types
create domain person_name char(20) not null
Types and domains are similar. Domains can have constraints,
such as not null, specified on them.
create domain degree_level varchar(10)
constraint degree_level_test
check (value in (Bachelors, Masters, Doctorate));
7
Large Large--Object Types Object Types
Large objects (photos, videos, CAD files, etc.) are stored as a
large object:
blob: binary large object -- object is a large collection of
uninterpreted binary data (whose interpretation is left to an
application outside of the database system)
clob: character large object -- object is a large collection of
character data
When a query returns a large object, a pointer is returned
rather than the large object itself.
Authorization Authorization
Forms of authorization on parts of the database:
Read - allows reading, but not modification of data.
Insert - allows insertion of new data, but not modification of
existing data.
Update - allows modification, but not deletion of data.
Delete - allows deletion of data.
Forms of authorization to modify the database schema
Index - allows creation and deletion of indices.
Resources - allows creation of new relations.
Alteration - allows addition or deletion of attributes in a
relation.
Drop - allows deletion of relations.
Authorization Specification in SQL Authorization Specification in SQL
The grant statement is used to confer authorization
grant <privilege list>
on <relation name or view name> to <user list>
<user list> is:
a user-id
public, which allows all valid users the privilege
granted
A role (more on this later)
Granting a privilege on a view does not imply granting any
privileges on the underlying relations.
The grantor of the privilege must already hold the privilege
on the specified item (or be the database administrator).
Privileges in SQL Privileges in SQL
select: allows read access to relation,or the ability to
query using the view
Example: grant users U
1
, U
2
, and U
3
select
authorization on the instructor relation:
grant select on instructor to U
1
, U
2
, U
3
insert: the ability to insert tuples
update: the ability to update using the SQL update
statement
delete: the ability to delete tuples.
all privileges: used as a short form for all the allowable
privileges
Revoking Authorization in SQL Revoking Authorization in SQL
The revoke statement is used to revoke authorization.
revoke <privilege list>
on <relation name or view name> from <user list>
Example:
revoke select on branch from U
1
, U
2
, U
3
<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.
Roles
create role instructor;
grant instructor to Amit;
Privileges can be granted to roles:
grant select on takes to instructor;
Roles can be granted to users, as well as to other
roles
create role teaching_assistant
grant teaching_assistant to instructor;
Instructor inherits all privileges of teaching_assistant
Chain of roles
create role dean;
grant instructor to dean;
grant dean to Satoshi;
8
Authorization on Views Authorization on Views
create view geo_instructor as
(select *
from instructor
where dept_name = Geology);
grant select on geo_instructor to geo_staff
Suppose that a geo_staff member issues
select *
from geo_instructor;
What if
geo_staff does not have permissions on instructor?
creator of view did not have some permissions on
instructor?
Other Authorization Features Other Authorization Features
references privilege to create foreign key
grant reference (dept_name) on department to Mariano;
why is this required?
transfer of privileges
grant select on department to Amit with grant option;
revoke select on department from Amit, Satoshi cascade;
revoke select on department from Amit, Satoshi restrict;
Etc. read Section 4.6 for more details we have omitted here.
Questions ? Questions ?