Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Modifications CIS 4301 Lecture Notes Lecture 19 - 3/30/2006.

Similar presentations


Presentation on theme: "Database Modifications CIS 4301 Lecture Notes Lecture 19 - 3/30/2006."— Presentation transcript:

1 Database Modifications CIS 4301 Lecture Notes Lecture 19 - 3/30/2006

2 Lecture 19© CIS 4301 - Spring 20062 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?

3 Lecture 19© CIS 4301 - Spring 20063 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

4 Lecture 19© CIS 4301 - Spring 20064 Example SELECT * FROM MovieStar LEFT OUTER JOIN MovieExec ON 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 MovieExec ON MovieStar.Name = MovieExec.name; Dangling tuples of the right (second) relation are padded with NULLs and included in result

5 Lecture 19© CIS 4301 - Spring 20065 Example (Older Syntax) SELECT * FROM MovieStar, MovieExec WHERE MovieStar.Name(+) = MovieExec.name; SELECT * FROM MovieStar, MovieExec WHERE MovieStar.Name = MovieExec.name(+);

6 Lecture 19© CIS 4301 - Spring 20066 Where to go for details? Oracle 9i SQL Reference http://www.cise.ufl.edu/help/database/oracle -docs/server.920/a96540.pdf http://www.cise.ufl.edu/help/database/oracle -docs/server.920/a96540.pdf

7 Lecture 19© CIS 4301 - Spring 20067 Database Modifications: Insert INSERT INTO R(A 1,A 2, …, A n ) VALUES (v 1,v 2, …, v n ) Example: INSERT INTO Movies(title,year,length,inColor,Studio Name,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

8 Lecture 19© CIS 4301 - Spring 20068 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

9 Lecture 19© CIS 4301 - Spring 20069 Database Modifications: Delete DELETE FROM R WHERE Delete the fact that R. Moore was a star in the Maltese Falcon DELETE FROM StarsIn WHERE movietitle =‘The Maltese Falcon’ AND movieYear = 1942 AND starName = ‘R. Moore’;

10 Lecture 19© CIS 4301 - Spring 200610 Database Modifications: Update UPDATE R SET WHERE ; 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 MovieExec SET name = ‘Pres.’ || name WHERE cert# IN (SELECT presC# FROM Studio);

11 Lecture 19© CIS 4301 - Spring 200611 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 AS SFW query

12 Lecture 19© CIS 4301 - Spring 200612 Example Create a view of the Movie relation that includes only movies made by Paramount CREATE VIEW Paramount_Movies AS SELECT title, year FROM Movie WHERE studioName = ‘Paramount’; Use it in query: SELECT title FROM Paramount_Movies WHERE year = 1970;

13 Lecture 19© CIS 4301 - Spring 200613 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

14 Lecture 19© CIS 4301 - Spring 200614 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, studioName FROM Movie WHERE studioName = ‘Paramount’;

15 Lecture 19© CIS 4301 - Spring 200615 More View Examples CREATE VIEW MovieProd AS SELECT title, name FROM Movie, Movie Exec WHERE 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!

16 Lecture 19© CIS 4301 - Spring 200616 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

17 Lecture 19© CIS 4301 - Spring 200617 Example View: 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 tablevirtual table

18 Lecture 19© CIS 4301 - Spring 200618 Expressing the Query in Terms of Base Tables  title, year  studioName = ‘Paramount’ Movie  title  year = 1979 simplified  title  year = 1979 AND studioName = ‘Paramount’ Movie replace virtual table by its view definition

19 Lecture 19© CIS 4301 - Spring 200619 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

20 Lecture 19© CIS 4301 - Spring 200620 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!

21 Lecture 19© CIS 4301 - Spring 200621 Selection of Indexes Why do we not index ALL attributes and their combinations? Two factors Speed vs. overhead

22 Lecture 19© CIS 4301 - Spring 200622 Intro to Database Tuning Assume we have the schema StarsIn(movieTitle,movieYear,starName) Assume we have following query mix Q1: SELECT starName FROM StarsIn WHERE starName = s; Q2: SELECT starName FROM StarsIn WHERE movieTitle = t AND movieYear = y; Q3: INSERT INTO StarsIn VALUES (t,y,s); Which indexes should we create?


Download ppt "Database Modifications CIS 4301 Lecture Notes Lecture 19 - 3/30/2006."

Similar presentations


Ads by Google