Join Expressions

Natural Join

--List the names of instructors 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
Student
Student Relation
Takes
Takes Relation
Result
student natural join takes
select A1, A2, … An
from r1 natural join r2 natural join .. natural join rn
where  P ;

Dangerous in Natural Join

--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;

Natural Join with Using Clause

select name, title
from (student natural join takes) join course using (course_id);

Join Condition

select *
from student join takes on student.ID = takes.ID;

--equivalent query
select *
from student, takes
where student.ID = takes.ID;

--display the ID value only once
select student.ID as ID, name, dept_name, tot_cred, course_id, sec_id, semester, year, grade
from student join takes on student.ID = takes.ID;

Outer Join

select *
from student natural join takes;
Course
Relation course
Prereq
Relation prereq

Left Outer Join

Result
course natural left outer join prereq

Right Outer Join

Result
course natural right outer join prereq

Full Outer Join

Result
course natural full outer join prereq

Views

select ID, name, dept_name
from instructor;
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
    and course.dept_name = 'Physics'
    and section.semester = 'Fall'
    and section.year = 2017;

View Definition

create view v as query_expression

View Definition and Use

--A view of instructors without their salary
create view faculty as
    select ID, name, dept_name
    from instructor;
--A view that lists all course sections offered by the Physics department in the Fall 2017 semester with the building and room number of each section
create view physics_fall_2017 as
    select course.course_id, sec_id, building, room_number
    from course, section
    where course.course_id = section.course_id
        and course.dept_name = 'Physics'
        and section.semester = 'Fall'
        and section.year = 2017;
--Create a view of department salary totals
create view departments_total_salary(dept_name, total_salary) as
    select dept_name, sum (salary)
    from instructor
    group by dept_name;
--physics_fall_2017 is a view
create view physics_fall_2017_watson as
    select course_id, room_number
    from physics_fall_2017
    where building= 'Watson';

--equivalent query
create view physics_fall_2017_watson as
    select course_id, room_number
    from (select course.course_id, building, room_number
          from course, section
          where course.course_id = section.course_id
               and course.dept_name = 'Physics'
               and section.semester = 'Fall'
               and section.year = '2017')
     where building= 'Watson';

Materialized Views

Update of a View

insert into faculty values ('30765', 'Green', 'Music');
create view instructor_info as 
    select ID, name, building
    from instructor, department
    where instructor.dept_name = department.dept_name;

insert into instructor_info values ('69987', 'White', 'Taylor');

View Updates in SQL

create view history_instructors as
    select *
    from instructor
    where dept_name= 'History';

Transaction

Integrity Constraints

Constraints on a Single Relation

Not Null Constraints

--Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null;

Unique Constraints

unique(A, B, C, ...)

The Check Clause

--Ensure that semester is one of fall, winter, spring or summer  
create table section 
    (course_id varchar (8),
    sec_id varchar (8),
    semester varchar (6),
    year numeric (4,0),
    building varchar (15),
    room_number varchar (7),
    time slot id varchar (4), 
    primary key (course_id, sec_id, semester, year),
    check (semester in ('Fall', 'Winter', 'Spring', 'Summer')));

Referential Integrity

create table instructor
    (ID varchar(5),
    name varchar(20) not null,
    dept_name varchar(20),
    salary numeric(8, 2) check (salary > 29000),
    primary key(ID),
    foreign key(dept_name) references department);
foreign key (dept_name) references department (dept_name)

create table section
    (course_id varchar(8),
    sec_id varchar(8),
    semester varchar(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
    year numeric(4, 0) check (year > 1759 and year < 2100),
    building varchar(15),
    room_number varchar(7),
    time_slot_id varchar(4),
    primary key (course_id, sec_id, semester, year),
    foreign key(course_id) references course,
    foreign keyI(building, room_number) references classroom);
create table course 
    (...
    dept_name varchar(20),
    foreign key (dept_name) references department
        on delete cascade
        on update cascade
    ...) 

Integrity Constraint Violation During Transactions

Complex Check Conditions

check(time_slot_id in(select time_slot_id from time_slot))

Assertions

create assertion credits_earned_constraint check
(not exists (select ID
            from student
            where tot_cred <> (select coalesce(sum(credits), 0)
                                from takes natural join course
                                where student.ID = takes.ID and grade is not null and grade <> 'F')));

SQL Data Types and Schemas

Built-in Data Types in SQL

Default Values

create table student
    (ID varchar(5),
    name varchar not null,
    dept_name varchar(20),
    tot_cred numeric(3, 0) default 0,
    primary key (ID));

Large-Object Types

book_review clob(10KB)
image blob(10MB)
movie blob(2GB)

User-Defined Types

create type Dollars as numeric (12,2) final;

create table department
    (dept_name varchar(20),
    building varchar (15),
    budget Dollars);

Domains

create domain person_name char(20) not null;

create domain degree_level varchar(10)
    constraint degree_level_test
        check (value in ('Bachelors', 'Masters', 'Doctorate'));

Index Definition in SQL

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);
select *
from  student
where  ID = '12345'

Authorization

Authorization Specification in SQL

grant select on department to Amit, Satoshi;

Privileges in SQL

Revoking Authorization in SQL

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
--Instructor inherits all privileges of teaching_assistant
create role teaching_assistant
grant teaching_assistant to instructor;

--Chain of roles
create role dean;
grant instructor to dean;
grant dean to Satoshi;

Authorization on Views

create view  geo_instructor as
(select *
from instructor
where dept_name = 'Geology');

grant select on geo_instructor to geo_staff;
select *
from geo_instructor;

Other Authorization Features

grant reference (dept_name) on department to Mariano;
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;