Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL4.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Structured Query Language (SQL)
UNITED NATIONS Shipment Details Report – January 2006.
Using the Set Operators
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Relational data objects 1 Lecture 6. Relational data objects 2 Answer to last lectures activity.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL2.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL1.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
SQL: The Query Language Part 2
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Chapter 7 Working with Databases and MySQL
© Abdou Illia MIS Spring 2014
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Introduction to Structured Query Language (SQL)
Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
MySQL Access Privilege System
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Microsoft Office Illustrated Fundamentals Unit K: Working with Data.
Yong Choi School of Business CSU, Bakersfield
Displaying Data from Multiple Tables
Chapter Information Systems Database Management.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Benchmark Series Microsoft Excel 2013 Level 2
Relational Algebra Chapter 4, Part A
1 Database Systems 10.8, 2008 Lecture #4. 2 Course Administration Assignment #1 is due next Wed (outside 336/338). Course slides will now have black background.
Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23, 03-46,
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Relational Algebra and Relational Calculus
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Energy Generation in Mitochondria and Chlorplasts
Management Information Systems, 10/e
CMU SCS Carnegie Mellon Univ. School of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture #4: Relational Algebra.
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Database Systems: Design, Implementation, and Management Tenth Edition
The Relational Algebra and Relational Calculus
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL4

CS252 Fundamentals of Relational Databases 2 Interim Summary Topics covered: Mathematical functions, for single values and groups of values. GROUP BY and HAVING for producing summaries. Storing dates and times in the database. Creating views with CREATE VIEW … AS ". Creating tables from tables with CREATE TABLE … AS ".

CS252 Fundamentals of Relational Databases 3 This lecture This session : Indexes, Sequences, Relational Algebra and Advanced Joins

CS252 Fundamentals of Relational Databases 4 Indexes redundant but can improve the performance of a DBMS (look up rows) Updates can take longer not required, do not affect query results automatically added to any column declared as a Primary Key. DB implementations: own specific schemes; in some you can specify what kind to use.

CS252 Fundamentals of Relational Databases 5 Creating an index not part of SQL standard but nearly all DBMS support a common syntax. An index can be created on a single column : CREATE INDEX year_index ON CD_Year (year); This will improve performance on queries that look up 'year'. e.g. : SELECT barcode FROM CD_Year WHERE year = 1984;

CS252 Fundamentals of Relational Databases 6 Creating a multiple-key index An index can also be created on a multiple columns : CREATE INDEX index ON Pop_albums (artist,album); This will improve performance on queries that look up both artist and album, e.g. : SELECT barcode FROM Pop_albums WHERE artist='U2' AND album='Rattle and Hum';

CS252 Fundamentals of Relational Databases 7 Sequences Some DBMS support an 'Identity' column type, automatically populated with a unique reference number ( URN ). useful when creating surrogate keys (a key not derived data & acts as primary key). MS Access, MS SQL Server: identity + GUID (Globally Unique IDentifier) column type. MySQL: AUTO_INCREMENT column type. Oracle has Sequences.

CS252 Fundamentals of Relational Databases 8 Creating a Sequence To create a sequence that starts at 1 and increments by 1 use : CREATE SEQUENCE seq1; To create a sequence that starts at 10 and increments by 2 use : CREATE SEQUENCE seq2 INCREMENT BY 2 START WITH 10; To delete a sequence use : DROP SEQUENCE seq2;

CS252 Fundamentals of Relational Databases 9 Using a Sequence CREATE TABLE test ( urn NUMBER, name VARCHAR(10) ); To insert a row where one of the columns should be an URN : INSERT INTO test VALUES (seq1.nextval,'Tim');

CS252 Fundamentals of Relational Databases 10 Relational Algebra and SQL SQL manipulates tables. Relational operators are closed over relations, so, if translated into SQL, should be closed over these tables. 6 primitive operators: union, difference, product, projection, selection and renaming. NB The SQL keyword SELECT is associated with projection, not selection! Also: derived operators (cf operators in arithmetic, such as square(x) = x * x). Examples include intersection and join.

CS252 Fundamentals of Relational Databases 11 Primitive Relational Operators The 5 primitive relational operators: union A B Combines all tuples from A and B. difference A \ B All tuples from A with those common to B (A B) removed. product A B Cartesian product of A and B. project A[a; b …] Select only attributes a; b … from relation A. select A : C Select only those tuples satisfying the specified Boolean condition C, where C is constructed from arithmetic comparisons involving attributes by using propositional connectives.

CS252 Fundamentals of Relational Databases 12 Derived Relational Operators intersection A B : tuples common to A and B. join : A(A.a = B.b) JOIN B Join tables A and B together - for each row match attribute A.a with B.b, discarding duplicate columns. Another example of a derived operator is divideby - this attempts to invert product, in so far as this is possible.

CS252 Fundamentals of Relational Databases 13 Codd's completeness criterion: a query language is complete if it can express each of the five primitive relational operators.

CS252 Fundamentals of Relational Databases 14 Union Union A B Use SQL keyword UNION. Tables must be compatible... have the same attributes (column headings). (SELECT artist FROM Pop_albums WHERE artist LIKE 'U%') UNION (SELECT artist FROM Band_members WHERE member = 'Grohl'); Result is a one column table containing three entries: Foo Fighters, U2 and Underworld.

CS252 Fundamentals of Relational Databases 15 Intersection Intersection A B Use SQL keyword INTERSECT. Tables must be compatible. Query selects U2 and Foo Fighters: (SELECT artist FROM Pop_albums) INTERSECT (SELECT artist FROM Band_members);

CS252 Fundamentals of Relational Databases 16 Difference Use SQL keyword MINUS. (SELECT artist FROM Pop_albums) MINUS (SELECT artist FROM Band_members); Selects everything but U2 and Foo Fighters - The Verve and Underworld.

CS252 Fundamentals of Relational Databases 17 Product Product A B Part of the SELECT statement - list more than one table after keyword FROM. SELECT f1, f2 FROM Forward, Reverse; Forward f Reverse f Forward Reverse f1f

CS252 Fundamentals of Relational Databases 18 Project Project A[a, b …] Using keyword SELECT as from day 1. SELECT DISTINCT a, b FROM A; SELECT DISTINCT artist, album FROM Pop_albums;

CS252 Fundamentals of Relational Databases 19 Join Join A(A.a = B.b) JOIN B Using SELECT as in last seminar: SELECT DISTINCT artist, album, year FROM Pop_albums, CD_year WHERE Pop_albums.barcode = CD_year.barcode; SELECT DISTINCT * FROM A, B WHERE A.a = B.b; Notice how join is a combination of the product of tables and a predicate selection.

CS252 Fundamentals of Relational Databases 20 Advanced Joins We covered a simple (equi-)join between two tables earlier in the module. Joins combine rows from two or more tables to create a single result. Columns are compared with a Join Condition. Pairs of rows each containing one row from each table are combined when the join condition evaluates to TRUE.

CS252 Fundamentals of Relational Databases 21 Join Types (SQL) Joins can be classified into the following categories : Cartesian Products Inner Joins (Equijoins) Self Joins Outer Joins (Left, Right and Full)

CS252 Fundamentals of Relational Databases 22 Cartesian Products (Cross Join) join without a Join Condition generate many rows of data, e.g., test data. the base of all the other types of join.

CS252 Fundamentals of Relational Databases 23 Inner Joins (Equijoins) An Inner Join (or Equijoin) is a join with a condition (e.g., that compares columns for equality =). Rows are combined that have equal values in the specified columns. The order of the tables listed in the FROM clause should have no significance.

CS252 Fundamentals of Relational Databases 24 Inner Join Example The example from before : SELECT DISTINCT artist, album, year FROM Pop_albums, CD_year WHERE Pop_albums.barcode = CD_year.barcode ; ARTIST ALBUM YEAR U2 The Unforgettable Fire 1984 U2 Rattle and Hum 1988 U2 Achtung Baby 1991 Underworld Second Toughest in the Infants 1996 The Verve Urban Hymns 1997 Foo Fighters The Colour and the Shape 1997

CS252 Fundamentals of Relational Databases 25 Self Join A Self Join is a join of a table to itself. Put the table in the FROM clause twice. Self joins are very useful. Use aliases to distinguish columns in the WHERE clause.

CS252 Fundamentals of Relational Databases 26 Self Join Example Determine artists that have released more than one album : SELECT DISTINCT a.artist FROM Pop_albums a, Pop_albums b WHERE a.artist = b.artist AND a.album <> b.album; Can you think of alternative way of expressing this in SQL ?

CS252 Fundamentals of Relational Databases 27 Hierarchical Data Self-joins can be useful when working with hierarchical data. Consider the following table (EMPLOYEES) : EMPLOYEEID SUPERVISORID NAME NULL John Balfour Susan Saronnen Eric La Sold Martin Murphy Erica Strange Noah Tamil

CS252 Fundamentals of Relational Databases 28 How do we write a query to find the name of each employee's supervisor ? We can use a self-join : SELECT staff.name,supervisor.name FROM EMPLOYEES staff, EMPLOYEES supervisor WHERE staff.supervisorid = supervisor.employeeid; STAFF.NAME SUPERVISOR.NAME Susan Saronnen John Balfour Eric La Sold John Balfour Martin Murphy John Balfour Erica Strange Martin Murphy Noah Tamil Martin Murphy

CS252 Fundamentals of Relational Databases 29 Outer Join An Inner Join excludes rows from either table that don't have a matching row in the other table. An Outer Join allows us to return unmatched rows. Outer Joins come in three varieties : LEFT - only unmatched rows from the left table are kept RIGHT - only unmatched rows from the right table are kept FULL - unmatched rows from both tables are retained

CS252 Fundamentals of Relational Databases 30 Example data Imagine we have two tables defined as : CD_company barcode company Island Island Island Junior Virgin Capital EMI Naxos

CS252 Fundamentals of Relational Databases 31 Pop_albums barcode artist album U2 The Unforgettable Fire U2 Rattle and Hum U2 Achtung Baby Underworld Second Toughest in the Infants The Verve Urban Hymns Foo Fighters The Colour and the Shape Leftfield Leftism Notice not all companies match up to an album and not all albums match to a company.

CS252 Fundamentals of Relational Databases 32 Outer Left Join Example SELECT DISTINCT company,artist,album FROM CD_Company LEFT JOIN Pop_albums ON Pop_albums.barcode = CD_Company.barcode; COMPANY ARTIST ALBUM Capital Foo Fighters The Colour and the Shape EMI Island U2 The Unforgettable Fire Island U2 Rattle and Hum Island U2 Achtung Baby Junior Underworld Second Toughest in the Infants Virgin The Verve Urban Hymns Naxos

CS252 Fundamentals of Relational Databases 33 Produces a summary of record labels and the artists they publish. Notice that the record labels EMI and Naxos are displayed even though there are no albums with these companies in the Pop albums table.

CS252 Fundamentals of Relational Databases 34 Outer Right Join Example SELECT DISTINCT company,artist,album FROM CD_company RIGHT JOIN Pop_albums ON Pop_albums.barcode = CD_Company.barcode; COMPANY ARTIST ALBUM Island U2 The Unforgettable Fire Island U2 Rattle and Hum Island U2 Achtung Baby Junior Underworld Second Toughest in the Infants Virgin The Verve Urban Hymns Capital Foo Fighters The Colour and the Shape Leftfield Leftism

CS252 Fundamentals of Relational Databases 35 Outer Full Join Example SELECT DISTINCT company,artist,album FROM CD_Company FULL JOIN Pop_albums ON Pop_albums.barcode = CD_Company.barcode;

CS252 Fundamentals of Relational Databases 36 COMPANY ARTIST ALBUM Island U2 The Unforgettable Fire Island U2 Rattle and Hum Island U2 Achtung Baby Junior Underworld Second Toughest in the Infants Virgin The Verve Urban Hymns Capital Foo Fighters The Colour and the Shape Naxos EMI Leftfield Leftism

CS252 Fundamentals of Relational Databases 37 Interim Summary Indexes, Sequences, Relational Algebra and Advanced Joins