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

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi.
SQL Query Examples Database Management COP4540, SCS, FIU.
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.
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
SQL reviews. Stored Procedures Create Procedure Triggers.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
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.
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
CMSC424: Database Design Instructor: Amol Deshpande
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.
CMSC424: Database Design Instructor: Amol Deshpande
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
CMSC424: Database Design Instructor: Amol Deshpande
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.
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.
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 Query Compiler 16.1 Parsing and Preprocessing Meghna Jain(205) Dr. T. Y. Lin.
CMSC424: Database Design Instructor: Amol Deshpande
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
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.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
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.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/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.
Database Management COP4540, SCS, FIU Constraints and security in SQL (Ch. 8.6, Ch22.2)
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
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.
Index Example From Garcia-Molina, Ullman, and Widom: Database Systems, the Complete Book pp
ICS 321 Fall 2011 The Database Language SQL (iv) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/26/20111Lipyeow.
1 Chapter 6 Constraints uForeign Keys uConstraints.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
SQL Exercises – Part I April
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
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.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
1 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
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.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Chap 5. The DB Language (SQL)
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
The Query Compiler Parsing and Preprocessing. Meghna Jain(205)
2018, Fall Pusan National University Ki-Joune Li
SQL: Concept and Usage.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL This presentation will cover: View in database MySQL installation
SQL – Constraints & Triggers
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

Database Modifications CIS 4301 Lecture Notes Lecture /30/2006

Lecture 19© CIS Spring 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 Spring 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 Spring 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 Spring Example (Older Syntax) SELECT * FROM MovieStar, MovieExec WHERE MovieStar.Name(+) = MovieExec.name; SELECT * FROM MovieStar, MovieExec WHERE MovieStar.Name = MovieExec.name(+);

Lecture 19© CIS Spring Where to go for details? Oracle 9i SQL Reference -docs/server.920/a96540.pdf -docs/server.920/a96540.pdf

Lecture 19© CIS Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring 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 Spring Selection of Indexes Why do we not index ALL attributes and their combinations? Two factors Speed vs. overhead

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