CSE - 301 - Lecture-4 SQLIntermediateSQL
CSE - 301 - Lecture-4 SQLIntermediateSQL
SQL
CSE 4509
INSTRUCTOR: DR. F. A. FAISAL
OUTLINE
• Join Expressions
• Views
• Transactions
• Integrity Constraints
• SQL Data Types and Schemas
• Index Definition in SQL
• Authorization
JOINED RELATIONS
• Join operations take two relations and return as a result another
relation.
• A join operation is a Cartesian product which requires that tuples
in the two relations match (under some condition). It also
specifies the attributes that are present in the result of the join
• The join operations are typically used as subquery expressions
in the from clause
• Three types of joins:
• Natural join
• Inner join
• Outer join
NATURAL JOIN IN SQL
• Natural join matches tuples with the same values for all common
attributes, and retains only one copy of each common column.
• List the names of students along with the course ID of the courses
that they taught
• select name, course_id
from students, takes
where student.ID = takes.ID;
• Same query in SQL with “natural join” construct
• select name, course_id
from student natural join takes;
NATURAL JOIN IN SQL
(CONT.)
• 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
• Beware of unrelated attributes with same name which get equated
incorrectly
• Example -- List the names of students instructors along with the titles
of courses that they have taken
• Correct version
• select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
• Incorrect version
• select name, title
from student natural join takes natural join course;
• This query omits all (student name, course title) pairs where the
student takes a course in a department other than the student's
own department.
• The correct version (above), correctly outputs such pairs.
NATURAL JOIN WITH USING
CLAUSE
To avoid the danger of equating attributes erroneously, we can use the
“using” construct that allows us to specify exactly which columns
should be equated.
Query example
select name, title
from (student natural join takes) join course using (course_id)
JOIN CONDITION
The on condition allows a general predicate over the relations being
joined
This predicate is written like a where clause predicate except for the use
of the keyword on
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 CONDITION (CONT.)
Relation prereq
Observe that
course information is missing CS-347
prereq information is missing CS-315
A view is defined using the create view statement which has the form
select name
from faculty
where dept_name = 'Biology'
Create a view of department salary totals
not null
• Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null
UNIQUE CONSTRAINTS
unique ( A1, A2, …, Am)
• The unique specification states that the attributes A1, A2, …, Am
form a candidate key.
• Candidate keys are permitted to be null (in contrast to primary
keys).
THE CHECK CLAUSE
• 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”.
• Let A be a set of attributes. Let R and S be two relations that contain
attributes A and where A is the primary key of S. A is said to be a
foreign key of R if for any values of A appearing in R these values
also appear in S.
REFERENTIAL INTEGRITY
(CONT.)
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)
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
CONDITIONS
The predicate in the check clause can be an arbitrary predicate that can
include a subquery.
check (time_slot_id in (select time_slot_id from time_slot))
The check condition states that the time_slot_id in each tuple in
the section relation is actually the identifier of a time slot in the
time_slot relation.
• The condition has to be checked not only when a tuple is inserted or
modified in section , but also when the relation time_slot changes
ASSERTIONS
Example:
create table department
(dept_name varchar (20),
building varchar (15),
budget Dollars);
DOMAINS
create domain construct in SQL-92 creates user-defined domain
types
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
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