Advanced SQL Topics
Views:
Overview
Views allow the construction of a custom relation
Views:
Common Uses
Creating a View
Code Example
CREATE VIEW myView AS <query></query>
or
CREATE VIEW myView AS
SELECT titles, name, etc
FROM some_table
Views of Views
One view may be used in the expression defining another view.
Views may also be recursive and depend on themselves.
View Expansion
The meaning of views can be defined in terms of other views
For Example:
While there are no more view relations in e1:
Find any view relation vi in e1
Replace the view relation vi with expression defining vi
Materialized Views:
Definition and overview
Views that are physically stored
Updating a View
Updates in SQL Views
SQL Transactions
A Transaction consists of a sequence of query and/or update statements.
Can be thought of as a “unit” of work.
Transactions:
Constraints:
Common Constraints put on relations
Transactions:
Constraints:
What do we have to ensure on queries?
Because each query might be multiple steps,
we have to ensure that
all of the constraints are satisfied
before we commit.
If all constraints are NOT satisfied, must rollback.
Constraints:
Check Clause
Basic idea and example
The check(P) clause specifies
a predicate P that must be satisfied
by every tuple in a relation.
Added on table creation.
Example:
create table characters(
charid int,
status varchar(30),
primary key (charid),
check (status in (‘Alive’, ‘Dead’) )
)
Constraints:
Assertions
An Assertion is a predicate that expresses a condition
which the database must always satisfy
Form:
CREATE ASSERTION <name> CHECK <predicate></predicate></name>
Advanced SQL Operations:
Create an Index
CREATE INDEX <myindex> ON relation(attribute)</myindex>
Advanced SQL Operations:
Creating Functions
Triggers
A Trigger is a statement that is executed automatically
by the system as a side effect of a modification to the database
Executing Triggers
Other Interesting Topics in SQL
Ranking in SQL
Windowing
Permissions