Advanced SQL Flashcards
Advanced SQL Topics
-
Views
- Using Views
- Views of Views
- View Expansion
- Materialized Views
- Updating a View
- Transactions
-
Constraints
- Check Clause
- Assertions
-
Advanced Operations
- Create Index
- Create Functions
- Triggers
Views:
Overview
Views allow the construction of a custom relation
- It will remain up to date with the underlying data
- Views are not precomputed or stored
- Instead, the query expression is stored and executed whenever the view is needed
- A view can be used as if it were a normal relation
Views:
Common Uses
- Limit what parts of a database a user can interact with
- Present data with simplified schema
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.
- A view v1 is said to depend directly on a view v2
- if v2 is used in the expression defining v1
- A view v1 is said to depend on view v2 if
- v1 depends directly on v2 OR
- there is a path of dependencies from v1 to v2
View Expansion
The meaning of views can be defined in terms of other views
For Example:
- Let view v1 be defined by an expression e1
- e1 may itself contain uses of view relations
- View expansion of an expression repeats:
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
- Some database systems allow this
- MySQL requires a lot of manual work to do this
- Physical copy is created when the view is defined
- If relations used in the query are updated, the materialized view result becomes out of date
- Need to maintain the view by updating it whenever underlying relations are updated
Updating a View
- Can insert new values into a view
- These updates can be cascaded to update the underlying relations(the “real” data
- Can present some problems:
- What if data in view is missing an attribute that the underlying relation requires
- Can insert a null, or reject
- Not always possible
- May not be able to determine which “real” tuple to update
- What if data in view is missing an attribute that the underlying relation requires
Updates in SQL Views
- Most SQL implementations allow updates on simple views,
- where:
- The FROM clause has only one relation
-
SELECT clause contains only attribute names of the relation
- No expressions
- No aggs
- No “distinct”s
- Any attribute not listed in the SELECT clause can be set to null
- Query does not have a “group by” or “having” clause
SQL Transactions
A Transaction consists of a sequence of query and/or update statements.
Can be thought of as a “unit” of work.
- SQL standard specifies that a transaction begins implicitly when a SQL statement is executed
- Transaction must end in either a “commit” or “rollback” option
- They are atomic and isolated
Transactions:
Constraints:
Common Constraints put on relations
- not null
- primary keys
- unique
- check
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
- For example, check that all student GPAs are greater than 0
- Or an instructor cannot teach two different classrooms in a semester on the same time slot
Form:
CREATE ASSERTION <name> CHECK <predicate></predicate></name>
Advanced SQL Operations:
Create an Index
CREATE INDEX <myindex> ON relation(attribute)</myindex>
- Remember, MySQL has InnoDB as the default(most of the time) and it uses B+ trees
- MySQL can use Hash Indexes as well, but it requires special setups