Database Modifications
CIS 4301
Lecture Notes
Lecture 19 - 3/30/2006
Lecture 19© CIS 4301 - Spring 2006 2
Recall Join Expressions
Cartesian Product: Multiple relations in the FROM clause
Join: Specify join condition in WHERE clause
Remember, when specifying join condition, only tuples which satisfy join condition will be selected
Example R NATURAL JOIN S If a tuple t from R does not match any tuple s from S, all trace of t will disappear from result
Could pose problems – when?
Lecture 19© CIS 4301 - Spring 2006 3
Outer Join Differs from “inner” join Adding to result any tuple of either relation that does not join with at least one tuple of the other relation Recall, those tuples are called dangling tuples
Pad out dangling tuples with NULL’s in those attributes belonging to the other relation before adding to result
Left and Right Outer Join
Lecture 19© CIS 4301 - Spring 2006 4
Example
SELECT *FROM MovieStar LEFT OUTER JOIN MovieExecON MovieStar.Name = MovieExec.name; Dangling tuples of the left (first) relation are padded with NULLs and included in result
SELECT *FROM MovieStar RIGHT OUTER JOIN MovieExecON MovieStar.Name = MovieExec.name; Dangling tuples of the right (second) relation are padded with NULLs and included in result
Lecture 19© CIS 4301 - Spring 2006 5
Example (Older Syntax)
SELECT *
FROM MovieStar, MovieExec
WHERE MovieStar.Name(+) = MovieExec.name;
SELECT *
FROM MovieStar, MovieExec
WHERE MovieStar.Name = MovieExec.name(+);
Lecture 19© CIS 4301 - Spring 2006 6
Where to go for details?
Oracle 9i SQL Reference http://www.cise.ufl.edu/help/database/oracle-docs/server.920/a96540.pdf
Lecture 19© CIS 4301 - Spring 2006 7
Database Modifications: Insert
INSERT INTO R(A1,A2, …, An)
VALUES (v1,v2, …, vn) Example:INSERT INTO Movies(title,year,length,inColor,StudioName,producerC#)
VALUES (‘Star Wars’,1997,191,‘y’,‘Fox’,12345);
May omit list of attributes if we provide all values
Values may be provided explicitly through SFW subquery
Lecture 19© CIS 4301 - Spring 2006 8
Timing of Insertions Add to Studio all movie studios mentioned in Movie who do not appear in Studio.
1 INSERT INTO Studio(name)
2 SELECT studioName
3 FROM Movie
4 WHERE studioName NOT IN
5 (SELECT Name
6 FROM Studio);
Result can be affected by timing of insertion
Lecture 19© CIS 4301 - Spring 2006 9
Database Modifications: Delete
DELETE FROM R WHERE <condition> Delete the fact that R. Moore was a star in the Maltese Falcon
DELETE FROM StarsIn WHERE movietitle =‘The Maltese Falcon’
AND movieYear = 1942AND starName = ‘R. Moore’;
Lecture 19© CIS 4301 - Spring 2006 10
Database Modifications: Update
UPDATE R SET <new value assignment>WHERE <condition>; Find all tuples in R that satisfy WHERE-clause Each tuple will be updated by evaluating the formula(s) in the SET-clause and making assignments
Prepend the tittle ‘Pres.’ in front of every movie executive who is a president of a studio
UPDATE MovieExecSET name = ‘Pres.’ || nameWHERE cert# IN
(SELECT presC# FROM Studio);
Lecture 19© CIS 4301 - Spring 2006 11
SQL Views
Two kinds of relations Physically present in database: tables or base relations
Virtual relations, do not exist physically Virtual relation = view
Can be queried (as if they existed physically) In some cases, can even be modified!
Be clear about which type of relation you mean
View defined by an expression (like a query)
CREATE VIEW <view-name> AS <view-definition>
SFW query
Lecture 19© CIS 4301 - Spring 2006 12
Example
Create a view of the Movie relation that includes only movies made by ParamountCREATE VIEW Paramount_Movies AS
SELECT title, yearFROM MovieWHERE studioName = ‘Paramount’;
Use it in query:SELECT titleFROM Paramount_MoviesWHERE year = 1970;
Lecture 19© CIS 4301 - Spring 2006 13
Facts About Views Can be queried just like any other table Allow data to been seen differently by different users
Often used as a security mechanism Cannot always be updated
Updateable views: translate the modification of the view into equivalent modifications on the corresponding base table(s)
Complex rules about which views are updateable
Lecture 19© CIS 4301 - Spring 2006 14
Modifying Views
Suppose we try to insert (through view Paramount_Movies) the movie (‘Star Trek’, 1979) into the base relation Movie
Problem, studioName is not part of the view not updateable
Would leave studioName in Movie without value (NULL)
New view definition:CREATE VIEW Paramount_Movies AS
SELECT title, year, studioNameFROM MovieWHERE studioName = ‘Paramount’;
Lecture 19© CIS 4301 - Spring 2006 15
More View Examples
CREATE VIEW MovieProd ASSELECT title, nameFROM Movie, Movie ExecWHERE producerC#= cert#;
What happens when we try:INSERT INTO MovieProd VALUES
(‘Greatest …’, ‘Cecile B. DeMille’); Cannot update the two relations Movie and MovieExec correctly
MovieProd is not updateable!
Lecture 19© CIS 4301 - Spring 2006 16
Interpreting Queries Involving Views
In order to process query, it must first be represented by its expression tree in relational algebra
To evaluate, leaves must be base tables In case query involves views, some of the leaves are virtual relations
Represent virtual relations by their definitions, which must also be in terms of expression trees
To form the query over base tables, substitute for each leaf in tree that is a view, the root of a copy of the tree that defines that view
Lecture 19© CIS 4301 - Spring 2006 17
ExampleView:CREATE VIEW ParamountMovie AS
SELECT title, year
FROM Movie
WHERE studioName = ‘Paramount’;
Query:SELECT title
FROM ParamountMovie
WHERE year = 1979;
title, year
studioName = ‘Paramount’
Movie
title
year = 1979
ParamountMovie
base table virtual table
Lecture 19© CIS 4301 - Spring 2006 18
Expressing the Query in Terms of Base Tables
title, year
studioName = ‘Paramount’
Movie
title
year = 1979 simplifiedtitle
year = 1979 AND studioName
= ‘Paramount’
Movie
replace virtual table byits view definition
Lecture 19© CIS 4301 - Spring 2006 19
Indexes
Index I on attribute A of relation R Data structure that makes it efficient to find those tuples that have a fixed value for attribute A
E.g., “Find all MovieStars where gender = ‘M’”
Prevents database from having to scan all tuples of a relation
Useful when relation is large Different types of indexes for different types of lookup
Can include multiple attributes in index Determining which attributes to index on is not always easy and requires experience
Trade-off between speed and overhead for space and maintenance
Lecture 19© CIS 4301 - Spring 2006 20
Sample Indexes
CREATE INDEX Movie_index ON Movie(producerC#) TABLESPACE cis4301_sp06_ind;
DROP INDEX Movie_index;
CREATE TABLE Test (a INT PRIMARY KEY USING INDEX TABLESPACE cis4301_sp06_ind, b VARCHAR(20) UNIQUE USING INDEX TABLESPACE cis4301_sp06_ind); Drawback: using the implicit declaration, we cannot name index!
Lecture 19© CIS 4301 - Spring 2006 21
Selection of Indexes Why do we not index ALL attributes and their combinations?
Two factors Speed vs. overhead
Lecture 19© CIS 4301 - Spring 2006 22
Intro to Database Tuning
Assume we have the schemaStarsIn(movieTitle,movieYear,starName)
Assume we have following query mixQ1:
SELECT starNameFROM StarsInWHERE starName = s;
Q2:SELECT starNameFROM StarsInWHERE movieTitle = t AND movieYear = y;
Q3:INSERT INTO StarsIn VALUES (t,y,s);
Which indexes should we create?