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

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.