U2 P3 Intermediate SQL
U2 P3 Intermediate SQL
U2 P3 Intermediate SQL
Unit-2,Part-3
Faculty: A.Sirisha
Outline
Join Expressions
Views
Transactions
Integrity Constraints
SQL Data Types and Schemas
Index Definition in SQL
Authorization
Joined Relations
Natural join matches tuples with the same values for all
common attributes, and retains only one copy of each common
column
select *
from instructor natural join teaches;
Natural Join in SQL
The from clause can have multiple relations combined using natural
join:
select A1, A2, … An
from r1 natural join r2 natural join .. natural join rn
where P ;
Student Relation
Takes Relation
student natural join takes
Dangerous in Natural Join
• Correct version
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
Natural Join with Using Clause
Query example
select *
from student join takes on student_ID = takes_ID
• The on condition above specifies that a tuple from student matches a
tuple from takes if their ID values are equal.
Equivalent to:
select *
from student , takes
where student_ID = takes_ID
Join operations – Example
Relation course
Relation prereq
Observe that
course information is missing CS-347
prereq information is missing CS-315
Outer Join
Computes the join and then adds tuples form one relation that does not
match tuples in the other relation to the result of the join.
Join condition – defines which tuples in the two relations match. and
what attributes are present in the result of the join.
Join type – defines how tuples in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
Joined Relations – Examples
In some cases, it is not desirable for all users to see the entire logical
model (that is, all the actual relations stored in the database.)
Any relation that is not of the conceptual model but is made visible to
a user as a “virtual relation” is called a view.
View Definition
A view is defined using the create view statement which has the
form
create view v as < query expression >
Once a view is defined, the view name can be used to refer to the
virtual relation that the view generates.
As long as the view definitions are not recursive, this loop will terminate
Materialized Views
If relations used in the query are updated, the materialized view result
becomes out of date
• Need to maintain the view, by updating the view whenever the
underlying relations are updated.
Update of a View
Two approaches
• Reject the insert
• Inset the tuple
('30765', 'Green', 'Music', null)
into the instructor relation
Some Updates Cannot be Translated Uniquely
Issues
• Which department, if multiple departments in Taylor?
• What if no department is in Taylor?
And Some Not at All
However, views can be defined with a with check option clause at the
end of the view definition;
Then, if a tuple inserted into the view does not satisfy the view’s where
clause condition, the insertion is rejected by the database system.
View Updates in SQL
Atomic transaction
• either fully executed or rolled back as if it never occurred
not null
primary key
unique
check (P), where P is a predicate
Not Null Constraint
not null
• Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null
Unique Constraint
Ensures that a value that appears in one relation for a given set of
attributes also appears for a certain set of attributes in another
relation.
• Example: If “Biology” is a department name appearing in one of
the tuples in the instructor relation, then there exists a tuple in
the department relation for “Biology”.
Foreign keys can be specified as part of the SQL create table statement.
foreign key (dept_name) references department
Shorter notation:
• create table course (
course_id char(5) primary key,
title varchar(20),
dept_name varchar(20) references department )
Cascading Actions in Referential Integrity
Consider:
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)
For each tuple in the student relation, the value of the attribute tot_cred
must equal the sum of credits of courses that the student has completed
successfully.
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)
When a query returns a large object, a pointer is returned rather than the
large object itself.
User-Defined Types
It is inefficient for the system to read every record to find a record with
particular value
Query: 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
Authorization
Example:
• grant select on department to Amit, Satoshi
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
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
instructor relation:
grant select on instructor to U1, U2, U3
all privileges: used as a short form for all the allowable privileges
Revoking Authorization in SQL
<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.
Authorization in SQL
Roles
Chain of roles
• create role dean;
• grant instructor to dean;
• grant dean to Satoshi;
Authorization on Views
What if
• geo_staff does not have permissions on instructor?
• Creator of view did not have some permissions on instructor?
Other Authorization Features
transfer of privileges
• grant select on department to Amit with grant option;