Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.

Slides:



Advertisements
Similar presentations
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Advertisements

Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Book Chapter 3 (part 2 ) From ER to Relational Model.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Modeling Your Data Chapter 2. Overview of Database Design Conceptual design: –What are the entities and relationships in the enterprise? – What information.
Conceptual Design Using the Entity-Relationship (ER) Model
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Rewriting Intersect Queries Using In SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ INTERSECT.
ER continued, and ER to Relational Mappings R&G Chapters 2, 3 Lecture 22.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
ICS 321 Fall 2009 The Relational Model (ii) Asst. Prof. Lipyeow Lim Information and Computer Science Department University of Hawaii at Manoa 9/10/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
CMPT 258 Database Systems Final Exam Review.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
ER-to-Relational Mapping
From ER to Relational Model
CS 405G: Introduction to Database Systems
Presentation transcript:

Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division SQL (Division, Outer-Joins, Constraints) Your questions

Manages (did INTEGER, ssn CHAR(11), since DATE) -FOREIGN KEY (ssn) REFERENCES Employees -FOREIGN KEY (did) REFERENCES Employees Departments (did INTEGER, dname CHAR (20), budget INTEGER ) lot name dname budgetdid since name dname budgetdid since Manages Departments Employees ssn Each employee can manage zero, one or more departments. Employees (ssn CHAR (11), name CHAR (20), lot INTEGER ) Each department has zero, one or more managers. ER & Relational Review

Dept_Mgr (did INTEGER, ssn CHAR(11), dname CHAR (20), budget INTEGER, since DATE) -FOREIGN KEY (ssn) REFERENCES Employees lot name dname budgetdid since name dname budgetdid since Manages Departments Employees ssn Each employee can manage zero, one or more departments. Employees (ssn CHAR (11), name CHAR (20),lot INTEGER ) Each department has at most one manager. Key Constraint

Dept_Mgr (did INTEGER, ssn CHAR(11), dname CHAR (20), budget INTEGER, since DATE) - FOREIGN KEY (ssn) REFERENCES Employees - ssn NOT NULL lot name dname budgetdid since name dname budgetdid since Manages Departments Employees ssn Each employee can manage zero, one or more departments. Employees (ssn CHAR (11), name CHAR (20),lot INTEGER ) Each department has exactly one manager. Key + Participation Constraint

Participation Constraint lot name dname budgetdid since name dname budgetdid since Manages Departments Employees ssn Employees (ssn CHAR (11), name CHAR (20),lot INTEGER ) Departments (did INTEGER, dname CHAR (20), budget INTEGER ) Manages (did INTEGER, ssn CHAR(11), since DATE) -FOREIGN KEY (ssn) REFERENCES Employees -FOREIGN KEY (did) REFERENCES Employees Each department has at least one manager. Each employee manages at least one department. Insuffcient! Additional checks required.

lot name age pname Dependents Employees ssn Policy cost Weak entities have only a “partial key” (dashed underline) Weak Entities Dep_Policy (pname CHAR(20), ssn CHAR(11), age INTEGER, cost REAL ) - FOREIGN KEY (ssn) REFERENCES Employees -NOT NULL -ON DELETE CASCADE

Aggregation Used to model a relationship involving a relationship set. Allows us to treat a relationship set as an entity set for purposes of participation in (other) relationships. until Employees Monitors lot name ssn budget did pid started_on pbudget dname Departments Projects Sponsors since Employees(ssn, name, lot) Projects(pid, pbudget, started_on) Departments (did, dname, budget) Monitors (pid_FK, ssn_FK, did_FK, until) Sponsors (pid_FK, did_FK, since)

ISA (`is a’) Hierarchies Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked Employees (ssn, name, lot) Hourly_Emps(ssn_FK, hourly_Wages, hours_worked) Contract_Emps(ssn_FK, contractid)

Relational Algebra: 5 Basic Operations Selection (  ) Selects a subset of rows from relation (horizontal). Projection (  ) Retains only wanted columns from relation (vertical). Cross-product (  ) Allows us to combine two relations. Set-difference ( — ) Tuples in r1, but not in r2. Union (  ) Tuples in r1 and/or in r2. Renaming (  ) E.g.  (C(1 -> sid1, 5 -> sid2), S1 x R1)

Intersect (  ) R  S = R  (R  S) Join Condition Join Equijoin: Special case where c contains only equalities Natural join - Compute R  S - Select rows where attributes that appear in both relations have equal values - Project all unique attributes and one copy of each of the common ones. Compound Operations RS sidR S 

Division A/B A B1 B2 B3 A/B1 A/B2A/B3

Query has the form: {T | p(T)} –p(T) denotes a formula in which tuple variable T appears. Tuple Relational Calculus {S | S  Sailors  S.rating > 7   R(R  Reserves  R.sid = S.sid  R.bid = 103)} Only one free variable Bounded variable

Algebra  Calculus Find the names of sailors who’ve reserved a red boat  sname colorred BoatsservesSailors(( '' )Re)  {S1 |  S (S  Sailors  S1.name = S.name   R(R  Reserves  R.sid = S.sid   B(B  Boats  B.bid = R.bid  B.color = ‘red’)))} Join Projection Selection

Division example Find the names of sailors who’ve reserved ALL red boats {S1 |  S (S  Sailors  S.sname = S1.sname   B  Boats ( B.color = ‘red’   R(R  Reserves  S.sid = R.sid  B.bid = R.bid)))}  sname ( (  sid, bid Reserves) / (  bid (  color=red Boats)) Sailors)

SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification The target-list contains (i) list of column names & (ii) terms with aggregate operations (e.g., MIN (S.age)). – column name list (i) can contain only attributes from the grouping-list. SQL Query

Conceptual Evaluation Compute Cartesian product  of all tables in FROM clause Discard rows not satisfying WHERE clause (Selection  Group the remaining rows according to Grouping- List Apply HAVING clause Apply SELECT list (Projection  ) If there is DISTINCT, eliminate duplicates Order remaining tuples according to ORDER BY

Division in SQL SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( SELECT B.bid FROM Boats B WHERE NOT EXISTS ( SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) SELECT S.name FROM Sailors S, reserves R WHERE S.sid = R.sid GROUP BY S.name, S.sid HAVING COUNT(DISTINCT R.bid) = ( Select COUNT (*) FROM Boats) Find sailors who’ve reserved ALL boats. Simpler

SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid

CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( ( SELECT COUNT (S.sid) FROM Sailors S) + ( SELECT COUNT (B.bid) FROM Boats B) < 100 ) Awkward and wrong! Only checks sailors! Only required to hold if the associated table is non-empty. ASSERTION is the right solution; not associated with either table. CREATE ASSERTION smallClub CHECK ( ( SELECT COUNT (S.sid) FROM Sailors S) + ( SELECT COUNT (B.bid) FROM Boats B) < 100 ) Number of boats plus number of sailors is < 100 Constraints Over Multiple Relations