We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published byTeagan Lacer
Modified over 2 years ago
Database Modifications CIS 4301 Lecture Notes Lecture 19 - 3/30/2006
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?
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
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
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(+);
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
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
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
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’;
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);
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
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;
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
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’;
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!
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
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
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
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
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!
Lecture 19© CIS 4301 - Spring 200621 Selection of Indexes Why do we not index ALL attributes and their combinations? Two factors Speed vs. overhead
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?
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
© D. Wong Normalization Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies. Decompose relation.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
SQL reviews. Stored Procedures Create Procedure Triggers.
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
1 SQL Exercises – Part I February 1, Some Simple SQL Commands CREATE TABLE test ( i int, s char(10) ); DESCRIBE test;
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
Subqueries Example Find the name of the producer of ‘Star Wars’.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Index Example From Garcia-Molina, Ullman, and Widom: Database Systems, the Complete Book pp
The Query Compiler 16.1 Parsing and Preprocessing Meghna Jain(205) Dr. T. Y. Lin.
CMSC424: Database Design Instructor: Amol Deshpande
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
1 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
SQL Query Examples Database Management COP4540, SCS, FIU.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
© D. Wong Indexes JDBC JDBC in J2EE (Java 2 Enterprise Edition)
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
Chapter 5 : Integrity And Security Domain Constraints Referential Integrity Security Triggers Authorization Authorization in SQL Views
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Oracle Labs ECS 242, 342, 360 –You can connect from home to the machines in the lab. –E.g.: ssh u-knoppix.csc.uvic.ca Execute “sh” to use the proper shell.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
© Abdou Illia MIS Spring 2014
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
© 2017 SlidePlayer.com Inc. All rights reserved.