SQL
SQL
4.2
Joined Relations
Join operations take two relations and return as a result
another relation.
match, and what attributes are present in the result of the join.
4.3
4.4
course.course_id = prereq.course_id
4.5
4.6
Views
In some cases, it is not desirable for all users to see the
and department, but not the salary. This person should see a
relation described, in SQL, by
select ID, name, dept_name
from instructor
4.7
View Definition
A view is defined using the create view statement which has
the form
4.8
Example Views
A view of instructors without their salary
select name
from faculty
where dept_name = Biology
4.9
4.10
View Expansion
Expand a view in a query/another view
4.11
from v1 to v2
4.12
View Expansion
A way to define the meaning of views defined in terms of other
views.
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
Note:As long as the view definitions are not recursive, this
4.13
Update of a View
Add a new tuple to faculty view which we defined earlier
4.14
if no department is in Taylor?
Any attribute not listed in the select clause can be set to null
4.15
Materialized Views
Materializing a view: creates a physical table containing the tuples
of the view
4.17
Transactions
Transaction-Unit of work-- consists of a sequence of query and/or
update statements
Can turn off auto commit for a session (e.g. using API)
4.18
Integrity Constraints
Integrity constraints ensure that changes made by
4.19
Integrity Constraints
In general, an integrity constraint can be an arbitrary
not null
primary key
unique
4.20
4.21
where P is a predicate
4.22
4.23
Referential Integrity
Ensures that a value that appears in one relation for a given
null value
4.24
4.25
4.26
4.27
dept_name varchar(20),
foreign key (dept_name) references department
on delete cascade
on update cascade,
...
)
alternative actions to cascade: set null, set default, restrict
In case of chain of cascade updates system may abort the
transaction
c
4.28
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)
4.29
clause
Deferrable
4.30
must equal the sum of credits of courses that the student has
completed successfully.
create assertion credits earned constraint check
(not exists (select ID
from student
where tot cred <> (select sum(credits)
from takes natural join course
Where grade is not null and grade<> F ) )
4.31
Index Creation
create table student
e.g. select *
from student
where ID = 12345
4.32
User-Defined Types
create type construct in SQL creates user-defined type
Structured types
(budget to numeric(12,2))+20
4.33
Domains
create domain construct in SQL-92 creates user-defined
domain types
vendor.
4.34
Large-Object Types
Large objects (photos, videos, CAD files, etc.) are stored as a
large object:
4.35
Default Values
SQL allows a default value to be specified for an attribute
4.36
of a query
create table t1 as
(select *
from instructor
where dept name= Music)
with data;
Note: create view vs create table as :contents of the table are set
4.37
schema.
catalog5.univ schema.course
4.38
The default catalog and schema are part of an SQL environment that
4.39
Authorization
Authorization control: ensures that only authorized users are
User----Operation------Database object
objects
4.40
Authorization
Note: Also known as privilege
4.41
Privileges in SQL
select: same as read
eg
4.42
a user-id
A role
4.43
Authorization in SQL
Example:
privileges
4.44
Authorization in SQL
4.45
hold.
If <user-list> includes public, all users lose the privilege except the
owners.
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
4.46
4.47
4.48
4.49
Roles
4.50
Roles
4.51
Authorization on Views
create view geo_instructor as
(select *
from instructor
where dept_name = Geology);
select *
from geo_instructor;
4.52
Authorizations on Schema
Only the owner of the schema can carry out any modification to
the schema
4.53
Transfer of Privileges
A user who has been granted some form of authorization may
4.54
Transfer of Privileges
If DBA revokes grant from U1:
U7 Grants authorization to U8
U8 Grants authorization to U7
4.55
Transfer of Privileges
cascading revocation: revocation of a privilege from a user/role may
cascading revocation:
To grant a privilege with the grantor set to the current role associated
4.56