CS 3630 Database Design and Implementation

Slides:



Advertisements
Similar presentations
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
Advertisements

CSCI3170 Introduction to Database Systems
Chapter 4 Hotel (hotelno, hotelname, city)
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Information Resources Management February 27, 2001.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Chapter 6 SQL Homework.
Chapter 5 SQL Homework.
Project Phase I Phase II Due Monday, April 15 Groups 1.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Database Systems Lecture 5 Natasha Alechina
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Project – Phase II Derive Database Schema from E-R Model DBDL.
1 CS 3630 Database Design and Implementation. 2 Sets Foundation of relational database. Basic Operations Power set Mapping.
SQL queries ordering and grouping and joins
Relational Databases.  In week 1 we looked at the concept of a key, the primary key is a column/attribute that uniquely identifies the rest of the data.
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
Chapter 5 SQL: Data Manipulation Thomas Connolly, Carolyn Begg, Database System, A Practical Approach to Design Implementation and Management, 4 th Edition,
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL introduction 2013.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
IST 318 – DB Administration Data Retrieval Using SELECT statements.
INF 280 Database Systems SQL:Join
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Query Processing – Implementing Set Operations and Joins Chap. 19.
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
SQL: 1 SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r except select B from s select A as.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Fan Qi Database Lab 1, com1 #01-08 CS3223 Tutorial 5.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
CS 3630 Database Design and Implementation. Joins Retrieve data from two or more tables Join Conditions PK and FK (Natural Join) Other attributes (Theta.
CS 3630 Database Design and Implementation
Assignment 2 Relational Algebra Which tables? What operations?
IST 220 – Intro to Databases
Databases Chapter 9 Asfia Rahman.
The Relational Database Model
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
The Database Exercises Fall, 2009.
Introduction to MS Access: creating tables, keys, and relationships
Database Management  .
Assignment 2.
Theory behind the relational engine
Theory behind the relational engine
CIS 336 Competitive Success/snaptutorial.com
CIS 336 Education for Service-- snaptutorial.com.
CIS 336 Teaching Effectively-- snaptutorial.com
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
Access: SQL Participation Project
Data Types and Field Properties
CS4222 Principles of Database System
The Relational Algebra
M1G Introduction to Database Development
Section 4 - Sorting/Functions
Topic 12 Lesson 2 – Retrieving Data with Queries
Database Programming Using Oracle 11g
Presentation transcript:

CS 3630 Database Design and Implementation

Joins Retrieve data from two or more tables Join Conditions PK and FK (Natural Join) Other attributes (Theta Join) Based on Cartesian Product Implementation Nested loops Optimization

Tables Schemas Assignment 7

Joins List all guests who have at least one booking. (We keep all guests even they don’t have any bookings.) -- Which table(s)? -- Guest (Guest_no, Guest_Name, Address) -- PK: Guest_no -- Booking (HoteL_no, Guest_no, ...) -- PK: Hotel_no, Guest_No, Date_From -- FK: Guest_no references Guest Select Guest.* From Guest, Booking Where Guest.Guest_no = Booking.Guest_no;

Joins: New Style Select Guest.* From Guest, Booking Where Guest.Guest_no = Booking.Guest_no; From Guest Join Booking on Guest.Guest_no = Booking.Guest_no; We use the new style! -- Run in Oracle -- Duplicate records

Joins List all guests who have at least one booking. Select Guest.* From Guest Join Booking on Guest.Guest_no = Booking.Guest_no; Why duplicate records? For every g in Guest For every b in Booking If g.Guest_no = b.Guest_no Then Select g.*

Always remove duplicate records! Joins Always remove duplicate records! Select Distinct Guest.* From Guest Join Booking on Guest.Guest_no = Booking.Guest_no; Select Unique Guest.*

Using Short Table Names List all guests who have at least one booking. Select Distinct G.* From Guest G Join Booking B on G.Guest_no = B.Guest_no; Pause -- Cannot use the original table name any more Select Distinct Guest.* on Guest.Guest_no = B.Guest_no; ERROR at line 1: ORA-00904: "GUEST"."GUEST_NO": invalid identifier

Joins List all guests who have at least one booking with the details of each booking. Set linesize 100 Col Guest_name Format a14 Heading "Guest Name" Col address format a26 -- No need for Distinct -- All columns from both tables Select * From Guest G Join Booking B on G.Guest_no = B.Guest_no;

Joins What if no common attribute is specified? Select G.* From Guest G, Booking B; -- Where G.Guest_no = B.Guest_no; -- Cartesian product! Select Distinct G.* From Guest G Join Booking B; -- on G.Guest_no = B.Guest_no; ERROR at line 3: ORA-00905: missing keyword

Join not on PK/FK List all guests who are in a city where there is also a hotel. Assuming Address is City. Select Distinct G.* From Guest G Join Hotel H on G.Address = H.Address;

Joins: Three Tables List Hotel name, guest name and date_from for all bookings, sorted by hotel_no in ascending order and then by guest_no in descending order. Which tables? Hotel Name: Hotel Guest Name: Guest Date_From : Booking Select H.name, G.Guest_name, Date_from From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Join Guest G on G.Guest_no = B.Guest_no Order By H.Hotel_No, G.Guest_no desc; -- H.Hotel_No, G.Guest_no not selected

Joins: Three Tables -- List Hotel name, guest name and date_from -- for all bookings. -- Sort the result Select H.name, G.Guest_name, Date_from From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Join Guest G on G.Guest_no = B.Guest_no Order By H.name, G.Guest_name, Date_from; -- Sort by name, Guest_name, Date_from; Order By H.Hotel_No, G.Guest_no; -- Sort by Hotel_No, Guest_no;

Joins with Group By -- For each hotel, display Hotel name and number of -- bookings of the hotel. -- Tables: -- Hotel, Booking Select name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No; -- Will it work?

Joins with Group By -- For each hotel, display Hotel name and number of -- bookings of the hotel. -- Tables: -- Hotel, Booking -- Must also Group by name Select name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- What if group by name only?

Joins with Group By -- For each hotel, display Hotel name and -- number of bookings of the hotel. Select name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name; -- Missing hotels without bookings -- How to display a zero for such hotels? Select * From hotel;

Current Year with Assumption -- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- Assume no booking is longer than 1 year. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no and ((to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy')) or (to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy'))) Group by H.Hotel_No, name;

Be Careful about And/Or -- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- Assume no booking is longer than 1 year. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no and to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy') or to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy') Group by H.Hotel_No, name; -- and ((to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy')) -- or -- (to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy')))

Join and Where -- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- Assume no booking is longer than 1 year. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy') or to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy') Group by H.Hotel_No, name; -- could use where after join condition -- Same result for the query -- But one is Join condition -- the other is selection condition -- We should do it this way!

Current Year without Assumption -- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- No assumptions: booking could be longer than one year. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where to_char(Date_From, 'yyyy') = to_char(SysDate, 'yyyy') or to_char(Date_To, 'yyyy') = to_char(SysDate, 'yyyy') (to_char(Date_From, 'yyyy') < to_char(SysDate, 'yyyy') and to_char(Date_To, 'yyyy') > to_char(SysDate, 'yyyy')) Group by H.Hotel_No, name;

Current Year without Assumption -- For each hotel, display Hotel number with hotel name -- and number of bookings of the hotel for this year. -- No assumptions. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where to_char(Date_From, 'yyyy') <= to_char(SysDate, 'yyyy') and to_char(Date_To, 'yyyy') >= to_char(SysDate, 'yyyy') Group by H.Hotel_No, name; -- How to make it work for the current month? -- Use ‘yyyy mm’ -- Not ‘yyyy’ -- Not ‘mm yyyy’

Group By and Having -- For each hotel with at least 3 bookings this -- year, display Hotel number with the name and the number -- of bookings of the hotel this year. -- With assumption Select H.Hotel_no, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Where to_char(Date_From, 'yyyy') <= to_char(SysDate, 'yyyy') and to_char(Date_To, 'yyyy') >= to_char(SysDate, 'yyyy') Group by H.Hotel_No, name Having Count(*) >= 3;

Group By and Having -- For each hotel with more than 5 bookings, -- display Hotel number with name and the number -- of bookings of the hotel. Select H.Hotel_No, name, count(*) From Hotel H Join Booking B on H.Hotel_no = B.Hotel_no Group by H.Hotel_No, name Having Count(*) > 5;

Assignment 9 Joins Due Friday, April 20

Friday, April 13 Based on Assignment 8 Quiz 3 Friday, April 13 Based on Assignment 8