Download presentation
Presentation is loading. Please wait.
Published byMerry Stephens Modified over 9 years ago
1
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture 16 - 21/03/2006
2
Lecture 16© CIS 4301 - Spring 20062 Simple Movie Database Schema Movie(Title,Year,length,inColor,studioName,producerC#) StarsIn(MovieTitle,MovieYear,StarName) MovieStar(Name,address,gender,birthdate) MovieExec(name,address,Cert#,netWorth) Studio(Name,address,presC#)
3
Lecture 16© CIS 4301 - Spring 20063 Simple Queries in SQL SELECT FROM WHERE ORDER BY Asks for those tuples of one or more relation that satisfy a condition, order the output (default ascending) Note, most SQL implementations allow duplicate tuples, i.e., operate on multisets (bags) rather than sets
4
Lecture 16© CIS 4301 - Spring 20064 SQL vs. Rel. Algebra SELECT A1, A2,..., A n FROM R1, R2,..., R m WHERE condition is equivalent to relational algebra query: A1, A2,..., An ( condition (R1 X R2 X... X R m )) Returns unnamed relation whose schema is (A1, A2,..., A n ) SQL is case insensitive Case makes a difference inside quotes!
5
Lecture 16© CIS 4301 - Spring 20065 Selection and Projection Selection: Find all movies produced by Disney in 1990 SELECT * FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’; Projection: Find all movies produced by Disney in 1990 and return their title and length SELECT title, length FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’;
6
Lecture 16© CIS 4301 - Spring 20066 Duplicates Retained unless use keyword DISTINCT Duplicate removal is an expensive operation This is a significant difference between SQL and “pure” relational algebra
7
Lecture 16© CIS 4301 - Spring 20067 String Comparison Use any of the “usual” string comparison operators such as ‘ ’, ‘= ’, … More useful, pattern matching using “ like ” Two wildcards: ‘_’ and ‘%’ Find all movies whose title starts with ‘Star’ SELECT title FROM Movie WHERE title LIKE ‘Star%’; Find all movies with possessive (‘s) in their title SELECT title FROM Movie WHERE title LIKE ‘%’’s%’;
8
Lecture 16© CIS 4301 - Spring 20068 Ordering Output Return tuples in sorted order (by default in ascending order) Use desc if you want descending order Find all movies produced by Universal in 1973 and return their title and length; sort movies in ascend. order by length then by title SELECT title, length FROM Movie WHERE studioName = ‘Universal’ AND year = ‘1973’ ORDER BY length, title;
9
Lecture 16© CIS 4301 - Spring 20069 Renaming Attributes Use keyword AS in SELECT clause SELECT title AS Movie_Title, length AS Movie_Length FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’; SELECT title AS Title, length*0.016667 AS Length, ‘hrs.’ AS inHours FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’;
10
Lecture 16© CIS 4301 - Spring 200610 Cartesian Product Much of the power of rel. query language comes for the ability to combine two or more relations through cartesian product Form cartesian product by listing each relation in the FROM clause Consider pairs of tuples satisfying conditions in WHERE clause Find the name of the producer of ‘Star Wars’
11
Lecture 16© CIS 4301 - Spring 200611 Disambiguating Attributes Method 1: Prefixing the relation name Find stars and movie executives with the same address SELECT MovieStar.name, MovieExec.name FROM MovieStar, MovieExec WHERE MovieStar.address = MovieExec.address; Output: MovieStar.nameMovieExec.name Jane FondaTed Turner
12
Lecture 16© CIS 4301 - Spring 200612 Disambiguating Attributes Method 2: Tuple variable Find two stars with the same address SELECT Star1.name, Star2.name FROM MovieStar Star1, MovieStar Star2 WHERE Star1.address = Star2.address AND Star1.name< Star2.name; Output: Star1.nameStar2.name Alec BaldwinKim Basinger tuple variable Can use tuple variables anytime for convenience and readability!
13
Lecture 16© CIS 4301 - Spring 200613 Disambiguating Attributes What would be different if we re-wrote the query as: SELECT Star1.name, Star2.name FROM MovieStar Star1, MovieStar Star2 WHERE Star1.address = Star2.address AND Star1.name <> Star2.name; Output: Star1.nameStar2.name Alec BaldwinKim Basinger Alec Baldwin
14
Lecture 16© CIS 4301 - Spring 200614 Tuple Variables Tuples variables are always used (implicitly) in SELECT and WHERE clause If relation appears only once in the FROM clause, we use the relation name as its own tuple variable Relation name R in FROM clause is shorthand for R R
15
Lecture 16© CIS 4301 - Spring 200615 Set Operators Union: union Intersection: intersect Set Difference: minus Eliminate duplicates Use ALL to retain duplicates: e.g., UNION ALL Find the names and addresses of movie stars who are not also movie execs
16
Lecture 16© CIS 4301 - Spring 200616 Sample Queries Find the address of MGM studios. Find all stars that appeared either in a movie made in 1988 or a movie with “Dollar” in the title.
17
Lecture 16© CIS 4301 - Spring 200617 Sample Queries Find the names and addresses of all female movie stars who are also movie executives with a net worth over $10M.
18
Lecture 16© CIS 4301 - Spring 200618 Sample Queries Which movies are longer than “Star Wars?”
19
Lecture 16© CIS 4301 - Spring 200619 Sample Queries What is the result of the following query if T is empty? SELECT * FROM Movie WHERE length 120;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.