Lecture 5 β Intermediate SQL
Join expressions and outer joins, views (including materialized views), integrity constraints with cascading actions, SQL data types & domains, index creation, and authorization with GRANT/REVOKE and roles.
Sections
π
Join Expressions
Inner/theta join vs natural join, and left, right & full outer joins with every result table worked out.
ποΈ
Views
create view, how the DBMS rewrites a view query, views on views, the limits of updating a view, and materialized views.
π‘οΈ
Integrity Constraints
not null, unique, check(P), referential integrity and the five cascading actions.
ποΈ
Data Types, Domains & Indexes
date/time/timestamp/interval, create type & create domain, blob/clob, and CREATE INDEX.
π
Authorization
Data & schema privileges, GRANT/REVOKE with every variant, roles, and authorization on views.
Quick summary
JOINS: join = Cartesian product + match condition.
inner join β¦ on β keeps BOTH copies of the common column, any predicate (theta).
natural join β matches same-named columns automatically, keeps ONE copy.
OUTER JOINS avoid loss of information, pad the missing side with NULL:
β left (all left rows) Β· β right (all right rows) Β· β full (both).
"Which rows have no partner?" = outer join + IS NULL.
VIEWS: create view v as <query>. NOT a stored table β a STORED QUERY (virtual table).
Used to HIDE data (security), simplify joins, and give logical data independence.
Updatable only on simple views (one relation, no aggregate/distinct/group by).
MATERIALIZED view = a real physical table β fast but can go stale, must be maintained.
INTEGRITY: single relation β not null Β· primary key Β· unique (may be NULL!) Β· check(P).
Referential integrity β foreign key β¦ references; subquery in CHECK is unsupported (use a trigger).
CASCADING: on delete/update cascade Β· set null Β· set default Β· restrict/no action.
TYPES: date Β· time Β· timestamp Β· interval (date β date = interval).
create type / create domain (domains can carry constraints) Β· blob & clob (pointer returned).
INDEX: create index i on T(col) β index stores the key + a row pointer.
AUTHORIZATION: grant <priv> on <rel> to <user|public|role>; revoke β¦ from β¦.
Column-level grants Β· WITH GRANT OPTION Β· REFERENCES Β· roles = named privilege sets.
Granting on a VIEW grants nothing on the base tables.