Chapter 8 Database Redesign We only discuss complex sub-queries.

Slides:



Advertisements
Similar presentations
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
Advertisements

Chapter Eight: Database Redesign Database Processing: Fundamentals, Design, and Implementation.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
Fundamentals, Design, and Implementation, 9/e Chapter 8 Database Redesign.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 8 Foundations of Relational Implementation.
Fundamentals, Design, and Implementation, 9/e Chapter 4 The Relational Model and Normalization.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 David M. Kroenke’s Chapter Five: Data Modeling with the ER Model.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 1.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 David M. Kroenke’s Chapter One: Introduction Part One Database Processing:
Prentice Hall © COS 346 Day Agenda Questions? Assignment 7 Corrected –4 A’s and 4 B’s Assignment 8 posted –Due April 6 Quiz 2 next class.
Chapter 14 Getting to First Base: Introduction to Database Concepts.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 COS 346 Day 17.
1 Views. 2 What are views good for? (1) Simplifying complex queries: We saw one example. Here is another that allows the user to "pretend" that there.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 David M. Kroenke’s Chapter Five: Data Modeling with the Entity-Relationship.
David M. Kroenke and David J
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 David M. Kroenke’s Database Processing: Fundamentals, Design, and.
David M. Kroenke and David J. Auer Database Processing—12 th Edition Fundamentals, Design, and Implementation Chapter One: Introduction KROENKE AND AUER.
© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2003 Dragomir R. Radev.
Even Numbers – Any number that can be divided by 2
Chapter 3 The Relational Model and Normalization
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
David M. Kroenke’s Chapter One: Introduction Part Two Database Processing: Fundamentals, Design, and Implementation.
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, Animation of SQL Queries To illustrate three SQL queries: –Q1: simple select (one.
Chapter 4 The Relational Model and Normalization.
9.9 The Fundamental Theorem of Algebra
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
Chapter 8 Foundations of Relational Implementation David M. Kroenke Database Processing © 2000 Prentice Hall.
Relational Algebra CS420. Relation A relation is a two dimensional table that Rows contain data about an entity Columns contains data about attributes.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Chapter 9 Structured Query Language David M. Kroenke Database Processing © 2000 Prentice Hall.
(C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 David M. Kroenke’s Chapter Five: Data Modeling with the Entity-Relationship.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 5-1 David M. Kroenke’s Chapter Five: Data Modeling with the Entity-Relationship.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Eight: Database Redesign.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter One: Introduction.
Spring 2011 ITCS3160: Database Design and Implementation Hands-on Learning.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Appendix A: Getting Started with Microsoft Access 2013.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Appendix G: Data Structures for Database Processing.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Ten: Managing Databases with SQL Server 2012,
Factors, Primes & Composite Numbers Chapter 4.1. Definition  Product – An answer to a multiplication problem. 7 x 8 = 56 Product.
David M. Kroenke and David J
David M. Kroenke and David J
Database Processing: David M. Kroenke’s Chapter Five:
Referential Integrity
Database Processing: David M. Kroenke’s Chapter One: Introduction
Relational Algebra.
The Relational Model and Normalization
Database Processing: David M. Kroenke’s Chapter Five:
Database Processing: David M. Kroenke’s Chapter One: Introduction
Database Processing: David M. Kroenke’s Chapter One: Introduction
Relational Databases Relational Algebra (1) Select, project, join.
Referential Integrity
Database Processing: David M. Kroenke’s Chapter Seven:
Introduction to Databases
Database Processing: David M. Kroenke’s Chapter Six:
SQL: Structured Query Language
Database Processing: David M. Kroenke’s Chapter Five:
Getting to First Base: Introduction to Database Concepts
Database Processing: David M. Kroenke’s Chapter Five:
Getting to First Base: Introduction to Database Concepts
Database Quizz I Write down your answers in word document with file name highlighting your name, student number, and class. E.g “95002”+”_”+ “03 class”+”_”+”name”,
Getting to First Base: Introduction to Database Concepts
Database Assignment Write down your answers in word document with file name highlighting your name, student number, and class. E.g “95002”+”_”+ “03 class”+”_”+”name”,
Presentation transcript:

Chapter 8 Database Redesign We only discuss complex sub-queries

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 2 Referring to itself  Find the student names used by more than one student First try Select name From S Group By Name Having count(*) > 1 Second Try Select s.name Form S, S as S2 Where s.name = s2.name and s.sid <> s2.SID

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 3 Referring to itself (2)  Find the pair of SIDs using the same name Select S.Name, S.SID, S2.SID Form S, S as S2 Where s.name = s2.name and s.sid < s2.SID Note: you cannot use subquery or “Group By” to do this one

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 4 EXISTS and NOT EXISTS  EXISTS and NOT EXISTS are specialized forms of subqueries An EXISTS condition is true if any row in the subquery meets the specified conditions A NOT EXISTS condition is true only if all rows in the subquery do not meet the specified condition  Double use of NOT EXISTS can be used to find rows that have some specified condition to every row of a table

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 5 Example: EXISTS  List the names of students who take at least one class SELECT Name FROM S WHERE EXISTS (SELECT * FROM E WHERE SID =StudentNumber );  What is the difference? SELECT Name FROM S WHERE SID in (SELECT StudentNumber FROM E );

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 6 Example: NOT EXISTS  List the names of students who take no class SELECT Name FROM S WHERE NOT EXISTS (SELECT * FROM E WHERE SID =StudentNumber );  What is the difference? SELECT Name FROM S WHERE SID NOT in (SELECT StudentNumber FROM E );

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 7 Example: NOT EXISTS -- ALL  List the names of students who take all classes SELECT Name FROM S WHERE SID in (SELECT StudentNumber FROM E group by student number Having count(*) = (select count(*) from C) );

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 8 Example: NOT EXISTS – ALL (2)  List the names of students who take all classes  List the names of students for whom there does not exist a class the students are not taking SELECT Name FROM S WHERE Not Exists (SELECT * FROM C where not exists (select * from E where c.name = className and Sid = studentnumber );

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. Kroenke 9 Example: NOT EXISTS – ALL (3)  List the names of students who take all classes taking by PAKERs SELECT Name FROM S WHERE Not Exists (SELECT * FROM E as E2 where studentnumber in (select SID from S where S.name = ‘Paker’) and not exists (select * from E where E2.Classname = E.className and Sid = studentnumber );