Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture 16 - 21/03/2006.

Slides:



Advertisements
Similar presentations
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
SQL Query Examples Database Management COP4540, SCS, FIU.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
SQL CSET 3300.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
SQL and Relational Algebra Zaki Malik September 02, 2008.
E-R Diagram for a Banking Enterprise
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
Relational Algebra.
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
Query Compiler. The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical.
Subqueries Example Find the name of the producer of ‘Star Wars’.
The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical query plan into.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
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
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
CMSC424: Database Design Instructor: Amol Deshpande
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
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.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Chapter 6 The database Language SQL Spring 2011 Instructor: Hassan Khosravi.
Dr. T. Y. Lin | SJSU | CS 157A | Fall 2011 Chapter 6 THE DATABASE LANGUAGE SQL 1.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/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.
Relational Algebra (Chapter 7)
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
THE DATABASE LANGUAGE SQL
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Advanced Relational Algebra & SQL (Part1 )
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.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
© D. Wong Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.
1 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Chapter 3 Introduction to SQL(2)
Chap 5. The DB Language (SQL)
THE DATABASE LANGUAGE SQL
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
SQL: Concept and Usage.
Algebraic Laws.
CS4433 Database Systems SQL - Basics.
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Instructor: Zhe He Department of Computer Science
Presentation transcript:

Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006

Lecture 16© CIS Spring 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#)

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

Lecture 16© CIS Spring 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!

Lecture 16© CIS Spring 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’;

Lecture 16© CIS Spring Duplicates Retained unless use keyword DISTINCT Duplicate removal is an expensive operation This is a significant difference between SQL and “pure” relational algebra

Lecture 16© CIS Spring 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%’;

Lecture 16© CIS Spring 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;

Lecture 16© CIS Spring 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* AS Length, ‘hrs.’ AS inHours FROM Movie WHERE studioName = ‘Disney’ and year = ‘1990’;

Lecture 16© CIS Spring 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’

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

Lecture 16© CIS Spring 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!

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

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

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

Lecture 16© CIS Spring 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.

Lecture 16© CIS Spring Sample Queries Find the names and addresses of all female movie stars who are also movie executives with a net worth over $10M.

Lecture 16© CIS Spring Sample Queries Which movies are longer than “Star Wars?”

Lecture 16© CIS Spring Sample Queries What is the result of the following query if T is empty? SELECT * FROM Movie WHERE length 120;