Sei sulla pagina 1di 79

Advanced data management with SQL

Databases '05 1
Review

Databases '05 2
Last week ...

Overview of database management systems
– Characteristics of a DBMS
– Database schema and database state
– Database languages and interfaces

Intro to SQL (Structured query language)
– Overview

Features, Schema, Tables and Data types
– Data definition
● Creating, deleting and altering schemas/tables

Specifying simple constraints
– Basic queries
Databases '05 3
SQL
● Tables in SQL are “multisets” of tuples
– Unlike a set, a multiset can contain duplicate elements (tuples)

Two multisets are equal if they have the same number copies of
duplicate elements
{(a,1), (b,3), (b,3), (c,9)} = {(a,1), (b,3), (c,9), (b,3)}


but not equal to {(a,1), (b,3), (c,9)}
– Just like sets, tuples in a multiset are not ordered
–A multiset is also called a “bag”

Table with primary key or unique column is equivalent to a relation

SQL is a declarative, set-oriented language
– You do not specify how the result is to be computed
– Syntax is case insensitive (keywords, table names, attribute names)

However, case does matter inside strings (attribute values)

Databases '05 4
SELECT statement
start FROM: WHERE:
Defines input Select rows that
tables satisfy condition

GROUP BY: HAVING:


Group rows based on Select groups that
equal column values Satisfy condition

SELECT: ORDER BY:


end
Project columns listed Sort rows based on
in the select list column values result


Minimum number of clauses in SQL statement is 2

SELECT [DISTINCT|ALL] {*|[columnExpression [AS newName]][,...]}


FROM tableName [alias] [, ...]
[WHERE condition]
[GROUPBY columnList] [HAVING condition]
[ORDER BY columnList]
Databases '05 5
Today

Advanced Queries in SQL
– Multi-table queries (joins)
– Handling missing information (NULLs)
– Set Operations in SQL
– Aggregation, grouping and sorting
– Nested queries, universal and existential quantifiers

Data modification in SQL

Defining constraints

Views

Databases '05 6

SQL: Multi-table queries (joins)

Databases '05 7
Multi-table queries

What if we want to refer to data in more than one table?
– We can refer to other tables in subqueries

Results of nested queries can appear in WHERE clause

What if we want columns from different tables to appear
in result

Databases '05 8
Multi-table queries
EMNO FNAME LNAME ADDRESS DNO DNUMBER DNAME MGR
11005 John Smith 123 Main Street 4 2 SALES 56667
11223 Jane Doe 300 High street 4 3 MARKETING 80009
80009 Eric Clark 400 Columbus Av 5 4 PERSONNE 60005
L
5 LEGAL 71122
EMPLOYEE
DEPARTMENT
FNAME LNAME DNAME
Q1: SELECT FNAME, LNAME, DNAME John Smith SALES
John Smith MARKETING
FROM EMPLOYEE, DEPARTMENT; John Smith PERSONNEL
John Smith LEGAL
RESULT
Jane Doe SALES

Result is cross-product of tables in FROM clause Jane Doe MARKETING

Query's WHERE clause applied to cross-product Jane Doe PERSONNEL
– If Q1 had a WHERE Clause, it would take the cross- .. .. ..
product table (shown across) as its input Eric Clark LEGAL

Databases '05 9
Join queries

Join queries useful to combine rows from multiple tables
– Result table contains attributes of input tables

R(A,B) joined with S(C,D,E) produces T(A,B,C,D,E)

Join condition: Specifies which tuples in S match a tuple in R

Two types of joins
– Inner join
● Row is included in result only if it matches one or more rows
in the other table
– Outer join

Rows in one table not matching anything in the other table are
also included in result

Databases '05 10
Join example

EMNO FNAME LNAME ADDRESS DNO DNUMBER DNAME MGR


11005 John Smith 123 Main Street 4 2 SALES 56667
11223 Jane Doe 300 High street 4
3 MARKETING 80009
80009 Eric Clark 400 Columbus Av 5
4 PERSONNEL 60005
5 LEGAL 71122

Q2: SELECT FNAME, LNAME, DNAME


FNAME LNAME DNAME
FROM EMPLOYEE, DEPARTMENT
Jane Doe PERSONNEL
WHERE DNO = DNUMBER AND
Eric Clark LEGAL
LNAME < ‘K’;


Q2 prints out the employee names followed by their department name
• Only for those employees with last names before ‘K’ in the alphabetical order
• DNO = DNUMBER is called the ’join condition’
• LNAME < ‘K’ is called the ’selection condition’

Databases '05 11
Natural join

Natural join
– Is an inner join with an implicit join condition

Join condition: All likely named attributes are equal

Natural join example
– Consider two tables R(a, b , c) and S(b, c, d)
SELECT *
FROM (R NATURAL JOIN S);
is equivalent to
SELECT *
FROM R, S
WHERE R.b = S.b AND R.c = S.c;

Databases '05 12
Natural join example
Q3: DNUMBER DNAME MGR
SELECT FNAME, LNAME, DNAME 2 SALES 56667
3 MARKETING 80009
FROM (EMPLOYEE NATURAL JOIN
4 PERSONNEL 60005
(DEPARTMENT AS DEPT(DNO, DNAME, MGR))
5 LEGAL 71122

EMPLOYEE DEPT
EMNO FNAME LNAME ADDRESS DNO DNO DNAME MGR
11005 John Smith 123 Main Street 4 2 SALES 56667
11223 Jane Doe 300 High street 4 3 MARKETING 80009
80009 Eric Clark 400 Columbus Av 5
4 PERSONNEL 60005
54422 Patrick Simpson 456 Pleasant Dr 8
5 LEGAL 71122


Attribute renaming before natural join FNAME LNAME DNAME
– Using “AS” operator Q3 John Smith PERSONNNEL
Jane Doe PERSONNEL
– Forcing an equi-join on Dept number
Eric Clark LEGAL

Databases '05 13
Left-outer join
EMPLOYEE DEPARTMENT
EMNO FNAME LNAME ADDRESS DNO DNUMBER DNAME MGR
11005 John Smith 123 Main Street 4 2 SALES 56667
11223 Jane Doe 300 High street 4 3 MARKETING 80009
80009 Eric Clark 400 Columbus Av 5
4 PERSONNEL 60005
54422 Patrick Simpson 456 Pleasant Dr 8
5 LEGAL 71122

Q4: SELECT FNAME, LNAME, DNAME


FROM EMPLOYEE LEFT OUTER JOIN DEPARTMENT
ON DNO = DNUMBER;
FNAME LNAME DNAME
● S LEFT OUTER JOIN T ON a1 < b2 John Smith PERSONNNEL
Jane Doe PERSONNEL
– Includes results of the inner join, plus: Eric Clark LEGAL
Patrick Simpson NULL

Any unmatched tuples in S
– with NULLs for T’s attributes

Databases '05 14
Right-outer join
EMPLOYEE DEPARTMENT
EMNO FNAME LNAME ADDRESS DNO DNUMBER DNAME MGR
11005 John Smith 123 Main Street 4 2 SALES 56667
11223 Jane Doe 300 High street 4 3 MARKETING 80009
80009 Eric Clark 400 Columbus Av 5
4 PERSONNEL 60005
54422 Patrick Simpson 456 Pleasant Dr 8
5 LEGAL 71122

Q5: SELECT FNAME, LNAME, DNAME


FROM EMPLOYEE RIGHT OUTER JOIN DEPARTMENT
ON DNO = DNUMBER;
FNAME LNAME DNAME
John Smith PERSONNNEL
● S RIGHT OUTER JOIN T ON a1 < b2 Jane Doe PERSONNEL
– Includes results of the inner join, plus: Eric Clark LEGAL
NULL NULL SALES

Any unmatched tuples in T
NULL NULL MARKETING
– with NULLs for S’s attributes

Databases '05 15
Full-outer join
EMPLOYEE DEPARTMENT
EMNO FNAME LNAME ADDRESS DNO DNUMBER DNAME MGR
11005 John Smith 123 Main Street 4 2 SALES 56667
11223 Jane Doe 300 High street 4 3 MARKETING 80009
80009 Eric Clark 400 Columbus Av 5
4 PERSONNEL 60005
54422 Patrick Simpson 456 Pleasant Dr 8
5 LEGAL 71122

Q6: SELECT FNAME, LNAME, DNAME


FROM EMPLOYEE FULL OUTER JOIN DEPARTMENT
ON DNO = DNUMBER; FNAME LNAME DNAME
John Smith PERSONNNEL
Jane Doe PERSONNEL
● S FULL OUTER JOIN T ON a1 < b2 Eric Clark LEGAL
– Includes results of the inner join, plus: Patrick Simpson NULL

Any unmatched tuples in T and S NULL NULL SALES
– with NULLs for undefined attributes NULL NULL MARKETING

Databases '05 16
Join summary
● Two types of joins
– Inner: include only tuples that match condition

This is the default if join type is unspecified
● S NATURAL JOIN T
– Means: S INNER NATURAL JOIN T
● S JOIN T ON a1 < b2
– Means: S INNER JOIN T ON a1 < b2
The join condition is “a1 < b2”

– Outer: add remaining tuples that do not match anything
● S RIGHT OUTER JOIN T ON a1 < b2
– Includes results of above inner join, plus:
– Any unmatched tuples in T (with NULLs for S’s attributes)

Databases '05 17
Renaming

Renaming attributes
– Attributes in result can be renamed FNAME LNAME SALARY
– Using the AS keyword … … …


Print employee last name, first name, salary (in US $)

SELECT FNAME, LNAME, SALARY * 1.88 AS USSAL


FROM EMPLOYEE
FNAME LNAME USSAL
WHERE SALARY BETWEEN 20000 AND 30000;
… … …

Renaming tables often necessary
– When referring to two copies of same table

Print names of employees who share a first name with another employee

SELECT E1.FNAME, E1.LNAME, E2.LNAME AS LNAME2


FROM EMPLOYEE AS E1, EMPLOYEE AS E2
WHERE E1.FNAME = E2.FNAME AND E1.EMNO <> E2.EMNO;

Databases '05 18

Handling NULLs

Databases '05 19
Representing missing information

NULL is not zero!
– Real-world interpretations of NULL:

‘Unknown/withheld value’, ‘Not applicable attribute’

Evaluating conditions over NULL attributes
– What should Q7 return??
● Select employees who work in ‘LEGAL’ department

Q7: SELECT FNAME, LNAME, DNAME


FROM EMPLOYEE LEFT OUTER JOIN DEPARTMENT
WHERE DNO = DNUMBER AND DNAME = ‘LEGAL’;
FNAME LNAME DNAME
Output of Q7 without  John Smith PERSONNNEL
Jane Doe PERSONNEL
DNAME = 'LEGAL' condition Eric Clark LEGAL
Patrick Simpson NULL

Databases '05 20
Three-valued logic (TVL)

TVL defines three different truth values
– True, False, and Unknown

Test involving NULL attribute yields truth value UNKNOWN
– UNKNOWN composes with T, F according to 3VL truth table (below)

AND True False Unknown OR True False Unknown

True True False Unknown True True True True

False False False False False True False Unknown


Unknown True Unknown Unknown
Unknown Unknown False Unknown

NOT(True) = False NOT (False) = True


NOT(Unknown) = Unknown

Databases '05 21
Handing NULLs

Query returns tuples for which selection condition is True
– Tuples for which condition evaluates to UNKOWN are not returned


Testing for NULL attributes using IS/IS NOT NULL
• WHERE LNAME IS NULL
• WHERE LNAME IS NOT NULL

Empty versus NULL strings
– Test if LNAME is empty string
● LNAME LIKE ‘’
– Test if LNAME is NULL
● LNAME IS NULL
● What is matched by (LNAME LIKE ‘%’)?
– Are Empty LNAME, and NULL LNAME values returned??

Databases '05 22

Set Operations in SQL

Databases '05 23
Tables as sets in SQL

A table in SQL is a multiset
– Duplicate rows allowed
– There is no specific order among rows

DISTINCT keyword used for duplicate elimination
SELECT DISTINCT SALARY
FROM EMPLOYEE;
– Duplicate elimination is an expensive operation!

Use wisely (only when needed)
– Default is ALL
SELECT ALL SALARY
FROM EMPLOYEE;

Databases '05 24
Set operations in SQL

Set Operations
– UNION, EXCEPT, INTERSECT
– Tables must be union-compatible

Same attributes in same order
– Tables treated as sets, no duplicates in result

Example
(SELECT * FROM EMPLOYEE WHERE DNUM = 4)
UNION
(SELECT * FROM EMPLOYEE WHERE DNUM = 5);

Databases '05 25
Set example

R1
FNAME
Jane
John
UNION EXCEPT INTERSECT
John FNAME FNAME FNAME
Patrick Jane Patrick Jane
John John
R2 Michael

FNAME Patrick

Jane
John
Michael

Databases '05 26
Multiset operations

Multiset Operations
– UNION ALL, EXCEPT ALL, INTERSECT ALL
– Tables must be union-compatible
– Tables treated as multisets (or bags)

Two rows having same values considered different rows

Some familiar set laws still hold
– Union and Intersection are commutative and associative
– Other laws do not hold (e.g. Distributivity)

R  (S  T) =? (R  S)  (R  T)

Databases '05 27
Multiset example

R1
FNAME
Jane
UNION ALL EXCEPT ALL INTERSECT ALL
John
FNAME FNAME FNAME
John
Jane John Jane
Patrick
Jane Patrick John
John
R2 John
FNAME John
Jane Michael
John Patrick
Michael

Databases '05 28

Aggregation and grouping

Databases '05 29
Aggregation

Built-in aggregate operators
– COUNT: number of tuples
– MAX, MIN, SUM, AVG: functions over numeric attribute

Average salary within the sales department
SELECT AVG(SALARY)
FROM EMPLOYEE JOIN DEPARTMENT ON DNUM=DNUMBER
WHERE DNAME =‘SALES’;

Number of employees in the Marketing dept
SELECT COUNT(*)
FROM EMPLOYEE JOIN DEPARTMENT ON DNUM=DNUMBER
WHERE DNAME = ‘MARKETING’;

Databases '05 30
Sorting results

Result tuples can be sorted using ORDER BY
– According to one or more attributes
– Ascending (default) Or descending order

ORDER BY <attribute-list> [ASC | DESC]

Example
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
ORDER BY LNAME, FNAME;

SELECT FNAME, LNAME, ADDRESS


FROM EMPLOYEE
ORDER BY LNAME DESC, FNAME DESC;

Databases '05 31
Grouping

Grouping the result by one or more attributes
– GROUP BY <grouping-attributes>;
– Result table partitioned into sub-groups

Aggregate functions can be computed for each group

Print employee count & avg salary by department
SELECT DNO, COUNT(*), AVG(SALARY)
FROM EMPLOYEE
GROUP BY DNO;
EMNO FNAME LNAME SALARY DNO (DNO, COUNT, AVG)
11005
11223
John
Jane
Smith
Doe
20000
25000
4
4
} 4 2 22500

80009 Eric Clark 30000 5 } 5


8
1
3
30000
18000

}
54422 Patrick Simpson 15000 8
.. .. .. .. 8
.. .. .. .. 8

Databases '05 32
Having
● Print employee count & avg salary by department
– Only for large departments (> 2 employees)
SELECT DNO, COUNT(*), AVG(SALARY)
FROM EMPLOYEE
GROUP BY DNO
HAVING COUNT(*) > 2;

EMPN FNAME LNAME SALARY DNO


O
11005
11223
John
Jane
Smith
Doe
20000
25000
4
4
}
80009 Eric Clark 30000 5 } 8 3 18000

}
54422 Patrick Simpson 15000 8
.. .. .. .. 8
.. .. .. .. 8

Databases '05 33

Nested queries and univ./exist.
quantifiers

Databases '05 34
Nested queries

Allows queries within selection condition
– Result of subquery computed, used in WHERE clause

Select all co-workers of Jane (EMNO 445566)
– Co-worker: Works on a common project
– Suppose we have another table WORKS_ON (EMNO, PNUM)

SELECT EMNO
FROM WORKS_ON
WHERE PNUM IN (SELECT PNUM
FROM WORKS_ON
WHERE EMNO = 445566);

Databases '05 35
ANY and ALL
● Used to compare a value to a set or a multiset
• In the following, v a value
v is a value
S is a set or a multiset
S can be, for example, (the result of) a nested query

• v > ANY (S)
● True if and only if
There exists at least one x in S such that v > x

• v > ALL (S)



True if and only if :
– For all x in S: v > x

Databases '05 36
ANY and ALL over empty sets

What happens if the argument set/multiset is empty?
– What does the outer query return if the subquery returns 0 rows?
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE
WHERE DNO = 5);

What do the following conditions evaluate to?
• v > ANY (S) when S is empty
• v > ALL (S) when S is empty

Databases '05 37
Correlated nested queries
● Correlated nested queries
– When the nested query references some attribute of a relation
declared in the outer query
– A nested query is evaluated once for each tuple (or combination of
tuples) in the outer query

Example
– Consider additional table storing dependents of employees

Dependent (Dep_Fname, Dep_Lname, EMNO)
– Find all employees who have a like-named dependent
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.EMNO IN (SELECT EMNO
FROM DEPENDENT
WHERE E.EMNO = D.EMNO AND
E.FNAME = DEP_FNAME);

Databases '05 38
EXISTS

Tests whether set/multiset is not empty
– Set/multiset can be the result of a nested subquery
– EXISTS (S) returns TRUE if and only if S is not empty

Example
– Consider additional table storing dependents of employees

Dependent (Dep_Fname, Dep_Lname, EMNO)
– Find all employees with a like-named dependent

SELECT E.FNAME, E.LNAME


FROM EMPLOYEE AS E
WHERE EXISTS (SELECT *
FROM DEPENDENT AS D
WHERE E.EMNO = D.EMNO AND
E.FNAME = D.DEP_FNAME);
Databases '05 39
NOT EXISTS

Tests whether set/multiset is empty
– NOT EXISTS (S) returns TRUE if and only if S is empty

Example
– Find all employees with no dependents

SELECT E.FNAME, E.LNAME


FROM EMPLOYEE AS E
WHERE NOT EXISTS (SELECT *
FROM DEPENDENT AS D
WHERE E.EMNO = D.EMNO);

Databases '05 40
UNIQUE

Tests if result is a set or Multiset
– That is, does S contain duplicates?

UNIQUE(S)
– Returns False if and only if S has duplicates
– Returns True if and only if S has no duplicates

Databases '05 41

Database modification in SQL

Databases '05 42
Database modifications in SQL

Three DML statements
– INSERT: inserts new tuples
– DELETE: deletes existing tuples
– UPDATE: Change attribute values of existing tuples

Databases '05 43
INSERT

Inserts one or more tuples in a single table

Syntax:
INSERT INTO <table_name> [(<attribute list>)]
VALUES (<attribute value list for first tuple>),

(<attribute value list for nth tuple>);

Databases '05 44
INSERT examples
● Default assignment assumed if attribute list is empty
– Associate values to attributes in declaration order
– Example

Table to maintain all movies for rent in a video store

RENTAL_MOVIES (Title, Type, MNO, Producer, Year, Num_Rentals, Price)
INSERT INTO RENTAL_MOVIES
VALUES (‘The Godfather’, ‘Action’, 5664, ‘Warner Bros’, 1992,0,4.0),
(‘The Lion King’, ‘Kids’, 7775, ‘Sony’, 1998,0,3.0);


Forgot declaration order?
– Specify attributes explicitly after the table name
– Also, need not specify values for all attributes
INSERT INTO RENTAL_MOVIES (MNO, Title, Type, Producer)
VALUES (3331, ‘Rocky’, ‘Action’, ‘Fox’);

Databases '05 45
Bulk INSERT

Inserting the result of a query
– Syntax: INSERT INTO <table> (<subquery>);
– Example:

Insert movies rented more than 100 times in POP_MOVIES
CREATE TABLE POP_MOVIES (MNO INTEGER NOT NULL,
TITLE CHAR(15),
TYPE CHAR(15),
PRODUCER CHAR(15);
PRIMARY KEY (MNO));
INSERT INTO POP_MOVIES (MNO, TITLE, TYPE, Producer)
(SELECT MNO, TITLE, TYPE, PRODUCER
FROM RENTAL_MOVIES
WHERE Num_Rentals >= 100);

Databases '05 46
DELETE

Deletes one or more tuples from single table
– Set of tuples identified by selection condition:
DELETE FROM <table>
WHERE <condition>;

Example
– Delete all Horror movies from table
DELETE FROM RENTAL_MOVIES
WHERE TYPE LIKE ‘Horror’;

Databases '05 47
Delete Examples

Example II
– Delete all movies by the Producer of ‘Green Sun’
DELETE FROM RENTAL_MOVIES
WHERE PRODUCER = (SELECT PRODUCER
FROM RENTAL_MOVIES
WHERE TITLE LIKE ‘Green Sun’);
Subqueries are allowed in WHERE clause of DELETE


Example III
– Delete all movies in table
● DELETE FROM POP_MOVIES;


Makes relation POP_MOVIES empty

Databases '05 48
Delete problem

When to evaluate a selection condition during DELETE?
– Delete all movies that share a common Producer with another movie
DELETE FROM RENTAL_MOVIES m
WHERE EXISTS (SELECT *
FROM RENTAL_MOVIES
WHERE PRODUCER = m.PRODUCER AND MNO <> m.MNO);
– What is left after above stmt has finished execution?

TITLE Type MNO Producer Year Num_Rentals Price


The Godfather Action 5664 Warner Bros 1992 10 4.0
The Lion King Kids 7775 Disney 1998 5 2.0
Green Sun SciFi 3331 Warner Bros 1986 6 3.0

Databases '05 49
Delete semantics
● Delete all movies that share a common Producer with another movie
– Does deletion of first row makes third not qualify?

‘Green Sun’ will become only movie by ‘Warner Bros’


SQL 2 formal Semantics
– All conditions should be evaluated before any modifications due to
DELETE statement are performed.

Important when subqueries are used in DELETE stmt
– Since they can refer to table being modified

Databases '05 50
UPDATE

Modifies the attributes of one or more tuples
– Set of tuples identified by selection condition
UPDATE <table>
SET attr1=v1, attr2=v2, …, attrk = vk
WHERE <condition>;

Example
– Increase rental fee by 10% for all New releases
UPDATE RENTAL_MOVIES
SET PRICE = PRICE * 1.1
WHERE TYPE = ‘New Release’;

Databases '05 51

Defining Constraints

Databases '05 52
Why use constraints?

To catch data-entry errors

To enforce consistency across data in the database

To tell the system about the data
• System can store data and process queries accordingly

SELECT DISTINCT A, B, C
FROM T
WHERE …

– If B is declared a Unique attribute in T, then no expensive


duplicate elimination process is required. That is, DISTINCT
can be dropped by DBMS (optimiser) before execution.

Databases '05 53
Types of constraints

Primary key
– Ensure value is not NULL and is unique across table

Attribute-based
– Condition (e.g. valid range) over single attribute

Tuple-based
– Condition can range over relation

Referential integrity
– Ensures references to keys of other tables are valid

General assertions
– General assertions that hold across entire database

Databases '05 54
Primary key & Uniqueness
CREATE TABLE PROJECT
(PNAME VARCHAR(15) NOT NULL,
PNUMBER INT NOT NULL,
DNUM INT NOT NULL,
PRIMARY KEY(PNUMBER),
CONSTRAINT UNQ_PNAME UNIQUE (PNAME) );


Only one primary key can be declared per table

Many attributes can be declared UNIQUE
– What does UNIQUE(PNAME) mean?

No two rows of PROJECT agree in their PNAME values

We also say: PNAME is a candidate key of PROJECT

Databases '05 55
Attribute-based constraints
CREATE TABLE PROJECT
(PNAME VARCHAR(15) NOT NULL,
PNUMBER INT NOT NULL,
DNUM INT NOT NULL,
PRIMARY KEY(PNUMBER),
CONSTRAINT UNQ_PNAME UNIQUE (PNAME),
CONSTRAINT POS_DNUM CHECK (DNUM > 0) );


Follow attribute with condition which must hold for that
attribute in every row of the table

Checked on Update and Insert only
– Not on Delete

Databases '05 56
Dynamic attribute constraints

Constraints discussed so far are static
– Specify static constraints on attribute

E.g. The bounds on the valid range are fixed


Constraints can be dynamic
– By using subqueries in constraint definition

ALTER TABLE EMPLOYEE


ADD CONSTRAINT SAL_CAP
CHECK (SALARY <= 1.5 * (SELECT AVG(SALARY)
FROM EMPLOYEE) );

No employee to be paid 50% more than the average

Databases '05 57
Tuple-based constraints

Constraints within relations
– Applied to each tuple individually

CREATE TABLE PROJECT


(PNAME VARCHAR(15) NOT NULL,
PNUMBER INT NOT NULL,
DNUM INT NOT NULL,
STARTD DATE,
ENDD DATE,
PRIMARY KEY(PNUMBER),
CONSTRAINT VALID_DATES CHECK (ENDD > STARTD) );

Databases '05 58
Referential integrity

References to data must be valid

Foreign key constraint
– References to the primary key of another table
– Checked during database modifications

CREATE TABLE PROJECT


(PNAME VARCHAR(15) NOT NULL,
PNUMBER INT NOT NULL,
DNUM INT NOT NULL,
PRIMARY KEY(PNUMBER),
CONSTRAINT UNQ_PNAME UNIQUE (PNAME),
CONSTRAINT POS_DNUM CHECK (DNUM > 0)
FOREIGN KEY(DNUM) REFERENCES DEPARTMENT(DNUMBER) );

Databases '05 59
RI example

Constraint checked on Update, Delete, Insert

DEPARTMENT
PROJECT
DNAME DNUMBER MGR
PNAME PNUM DNUM
HR 3 7455
LEAP 230 7
Delete HEALTH 5 5466
COUNSEL 235 5
EMPDEV 7 3588
BALANCE 335 3
Referencing row

What to do if ‘HEALTH’ department is deleted?
– Reject statement – this is the default
– Accept, take a proper action – must specify action
Databases '05 60
RI constraints: actions

Syntax
● FOREIGN KEY(DNUM) REFERENCES DEPARTMENT(DNUMBER) ON
DELETE <DEL-ACTION> ON UPDATE <UP-ACTION>;

Action on delete (for referencing row)
● SET NULL: set to NULL
● SET DEFAULT: set to default value for attribute
● CASCADE: Delete row

Action on update (for referencing row)
● SET NULL, SET DEFAULT: like above
● CASCADE: Propagate change to referencing row

Databases '05 61
RI example: cascade
FOREIGN KEY(DNUM) REFERENCES DEPARTMENT(DNUMBER) ON
DELETE CASCADE;

DEPARTMENT
PROJECT
DNAME DNUMBER MGR
PNAME PNUM DNUM
HR 3 7455
LEAP 230 7
Delete HEALTH 5 5466
COUNSEL 235 5
EMPDEV 7 3588
BALANCE 335 3
Referencing row

PNAME PNUM DNUM


LEAP 230 7
BALANCE 335 3

Databases '05 62
RI example: set NULL
FOREIGN KEY(DNUM) REFERENCES DEPARTMENT(DNUMBER) ON
DELETE SET NULL;
DEPARTMENT
PROJECT
DNAME DNUMBER MGR
PNAME PNUM DNUM
HR 3 7455
LEAP 230 7
Delete HEALTH 5 5466
COUNSEL 235 5
EMPDEV 7 3588
BALANCE 335 3
Referencing row

PNAME PNUM DNUM


LEAP 230 7
COUNSEL 235 NULL
BALANCE 335 3

Databases '05 63
General assertions

Sometimes, we need general assertions
– Not attached to a single attribute or table

Referring to multiple tables in database

SQL provides a general assertion mechanism
CREATE ASSERTION <constraint-name>
CHECK <search-condition> [attributes]

Employee not paid more than the manager of the department
CREATE ASSERTION SALARY_CONSTRAINT
CHECK (NOT EXISTS
(SELECT *
FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D,
WHERE E.SALARY > M.SALARY AND
E.DNO = D.DNUMBER AND
D.MGREMPNO = M.EMPNO) );

Databases '05 64
Question

Suppose R is declared as follows:
CREATE TABLE R (a INT REFERENCES S(b)
ON UPDATE CASCADE);

Suppose R contains a tuple (2) among possibly others

Which stmts, if any, guarantee that (2) will no longer exist in R?

I II III
UPDATE S DELETE UPDATE R
SET b = 5; FROM S; SET a = 3
WHERE a = 2;

Databases '05 65

Views (Virtual tables)

Databases '05 66
Views

View

R S


What is a View?
– A query describing a “virtual” table

The table is not created physically

DBMS only stores view definition
– Referenced like a regular table

Databases '05 67
Creating Views in SQL

Syntax
CREATE VIEW <view-name> AS
<select statement>;

DROP VIEW <view-name>;


Example
– Encapsulate movies on sale into a view
CREATE VIEW MOVIES_ON_SALE AS
(SELECT TITLE, TYPE, MNO, PRICE
FROM RENTAL_MOVIES
WHERE (PRICE < 3.0 AND TYPE <> ‘NEW RELEASE’) OR
(PRICE < 4.0 AND TYPE = ‘NEW RELEASE’) );

Databases '05 68
Using Views

Views are referenced in queries like base tables
– Find all Matrix movies that are on sale
SELECT TITLE, TYPE, MNO, PRICE
FROM MOVIES_ON_SALE
WHERE TITLE LIKE ‘Matrix%’;


Views simplify queries
(SELECT TITLE, TYPE, MNO, PRICE
FROM RENTAL_MOVIES
WHERE ((PRICE < 3.0 AND TYPE <> ‘NEW RELEASE’) OR
(PRICE < 4.0 AND TYPE = ‘NEW RELEASE’) ) AND
(TITLE LIKE ‘Matrix%’);

Databases '05 69
Benefits of Views
● Simplify queries
– Hide complexity from users

Security
– Hide data from Users
● Logical data independence
– Underlying schema can change without affecting applications

If schema changes, we only need to change view definition

Recall related concept of Physical data independence:
– Changes in underlying physical storage representation do not affect
applications
– This is guaranteed by relational data abstraction

Real applications use Views extensively

Databases '05 70
Querying views
Client: query

View

Data path
(at run-time)
R S


Processing queries using views
– View definition expanded into outer query
– Full query is optimized

Databases '05 71
Updating Views
● Does not seem to make sense since view is “virtual”

But makes sense if that is how users view the database
– Users think view is a “real” table, and treat it as such

Problem
– How to modify the base tables so that the update appears to have
been done on the view ?

Databases '05 72
Updating Views
Base Table: RENTAL_MOVIES
TITLE Type MNO Producer Year Num_Rentals Price
The Godfather Action 5664 Warner 1992 10 4.0
Bros
The Lion King Kids 7775 Disney 1998 5 2.0
Green Sun SciFi 3331 Warner 1986 6 2.5
Bros
Lord of the Rings New Re.. 4545 Sony 2002 15 4.5

(PRICE < 3.0 AND TYPE <> ‘NEW RELEASE’) OR


Cond. in view defn. : (PRICE < 4.0 AND TYPE = ‘NEW RELEASE’)

TITLE Type MNO Price


View: MOVIES_ON_SALE
The Lion King Kids 7775 2.0
Green Sun SciFi 3331 2.5

Databases '05 73
Example I
Base Table: RENTAL_MOVIES
TITLE Type MNO Producer Year Num_Rentals Price
… … … …. … … …

View: MOVIES_ON_SALE
TITLE Type MNO Price UPDATE MOVIES_ON_SALE
The Lion King Kids 7775 2.0 SET PRICE = 2.75
WHERE TITLE = ‘Green Sun’;
Green Sun SciFi 3331 2.5


Mapping the update on the view to the base table
– Can row in view be uniquely associated with one in base table?

Yes (primary key is in the attributes of the view)

Can Insert be mapped correctly onto base table?
– Do we have all required values (Not NULL attributes)

Databases '05 74
Example II
Base Table: RENTAL_MOVIES
TITLE Type MNO Producer Year Num_Rentals Price
… … … …. … … …

View: MOVIES_ON_SALE
TITLE Type MNO Price INSERT INTO MOVIES_ON_SALE
The Lion King Kids 7775 2.0 VALUES (‘Amelie’,
‘Comedy’,
Green Sun SciFi 3331 2.5 3332, 5.0);


Mapping the Insert on the view to the base table
– No matter what we do, this new row will not appear in view!
CREATE VIEW MOVIES_ON_SALE AS
(SELECT *
FROM RENTAL_MOVIES
WHERE (PRICE < 3.0 AND TYPE <> ‘NEW RELEASE’) OR
(PRICE < 4.0 AND TYPE = ‘NEW RELEASE’) );

Databases '05 75
Example III

View for movie counts by type
– MOVIE_COUNTS(TYPE, NUM)
CREATE VIEW MOVIE_COUNTS(TYPE, NUM) AS
SELECT TYPE, COUNT (*)
FROM RENTAL_MOVIE MOVIE_COUNTS
GROUP BY TYPE; TYPE NUM
Action 32
Comedy 44

Updating movie counts view SciFi 10
UPDATE MOVIES_COUNTS
SET NUM = 5
WHERE TYPE = ‘Comedy’;

– Does this update make sense?

Databases '05 76
View Updates: Summary

Not all views are Updatable
– Views defined using grouping, aggregations are not
– Views over multiple tables are generally not updatable
– SQL updateable views

Single-table SFW, no grouping, no aggregation

Satisfying additional requirement below

Updateable single table views
– A View over a single table can be updated if:

It contains all primary key attributes

It contains all NOT NULL attributes of base relation
– Except for those that have DEFAULT values specified

Databases '05 77
View materialisation
Client: query
V View
Data path
(at run-time)

Data path
R S (at create-time)


View Materialisation
– View (query result) is stored in a physical table
– Physical table is accessed when view is referenced

Materialisation can save computations
– If base data does not change frequently
– View definition has complex aggregations, groupings
Databases '05 78
Things to keep in mind

Data Definition Language
– Creating/deleting/altering tables and schemas

Specifying constraints
– Simple, attribute-based, tuple-based and general assertions

Queries
– Simple queries, joins, aggregations, grouping and sorting
– Subqueries and universal quantifiers
– Handling NULLs (3-valued logic)

Views
– Defining, using updating and materialising views

Next
– Database programming
– Transaction processing

Databases '05 79

Potrebbero piacerti anche