Advanced SQL Flashcards

1
Q

Advanced SQL Topics

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Views:

Overview

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Views:

Common Uses

A
  • Limit what parts of a database a user can interact with
  • Present data with simplified schema
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Creating a View

Code Example

A

CREATE VIEW myView AS <query></query>

or

CREATE VIEW myView AS

SELECT titles, name, etc

FROM some_table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Views of Views

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

View Expansion

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Materialized Views:

Definition and overview

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Updating a View

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Updates in SQL Views

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

SQL Transactions

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Transactions:

Constraints:

Common Constraints put on relations

A
  • not null
  • primary keys
  • unique
  • check
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Transactions:

Constraints:

What do we have to ensure on queries?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Constraints:

Check Clause

Basic idea and example

A

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’) )

)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Constraints:

Assertions

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Advanced SQL Operations:

Create an Index

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advanced SQL Operations:

Creating Functions

A
  • Different databases use different syntaxes
  • But it is a SQL standard to have functions
  • Another option is to use an external language, such as python, to handle the functions for you.
17
Q

Triggers

A

A Trigger is a statement that is executed automatically

by the system as a side effect of a modification to the database

  • Specify the conditions that cause the trigger to be executed
    • Specify the actions to be taken when executed
18
Q

Executing Triggers

A
  • Can execute triggers for each row
    • ​Alternatively, can execute triggers for all affected rows
  • Can be more efficient when dealing with SQL statements that update many tuples
  • Consider when it is appropriate to use triggers:
    • Maintaining data summaries
    • Replicating databases by recording changes
  • Using views might be a better option in some cases
  • Must be careful not to trigger unintentionally.
    • Example: Loading backup data
19
Q

Other Interesting Topics in SQL

A

Ranking in SQL

  • Can rank using some aggregate functions
  • Allows us to add a ranked value to some aggregate result

Windowing

  • Can specify a “window” to apply different aggregate functions over, such as average

Permissions

  • Many DBMSs have a permissions systems
  • Allows us to restrict access to various tables and views