“Those who cannot remember the past are doomed to repeat it”

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Introduction to Databases
6.830/6.814 Lecture 3 Sam Madden Relational Algebra and Normalization Sept 10, 2014.
Hierarchies & Trees in SQL by Joe Celko copyright 2008.
CS 540 Database Management Systems
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
6.830 / Lecture 2 Data Models Sam Madden “Those who cannot remember the past are doomed to repeat it”
Introduction to Databases CS162 Guest Lecture Sam Madden
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
EECS 339 Lecture 2 Schema Design, Relational Algebra Jennie Duggan January 13, 2014.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Database Systems Lecture 5 Natasha Alechina
IST Databases and DBMSs Todd S. Bacastow January 2005.
INTRODUCTION TO DATABASE USING MS ACCESS 2013 PART 2 NOVEMBER 4, 2014.
Database Design Concepts
Database Management 9. course. Execution of queries.
1 Relational Databases and SQL. Learning Objectives Understand techniques to model complex accounting phenomena in an E-R diagram Develop E-R diagrams.
Fanny Widadie, S.P, M.Agr 1 Database Management Systems.
WXGE 6101 DATABASE CONCEPTS & IMPLEMENTATIONS. Lesson Overview The Relational Model Terminology of relational model. Properties of database relations.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Chapter 3: Relational Databases
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Databases and DBMSs Todd S. Bacastow January
XML: Extensible Markup Language
Relational Model.
Module 2: Intro to Relational Model
Chapter 2: Relational Model
Constraints and Triggers
Query-by-Example (QBE)
Relational Algebra Chapter 4 1.
Database Systems: Design, Implementation, and Management Tenth Edition
Lecture 2 The Relational Model
CSCI-100 Introduction to Computing
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
Introduction to Relational Databases
Translation of ER-diagram into Relational Schema
Databases and Information Management
Lecture#7: Fun with SQL (Part 2)
Normalization Referential Integrity
Relational Databases Relational Algebra (2)
The Relational Model Relational Data Model
Databases: An Introduction
The Entity-Relationship Model
Relational Algebra Chapter 4 1.
The Relational Model Textbook /7/2018.
Creating and Maintaining
Relational Algebra and Normalization 9/13/2017
CMPT 354: Database System I
Databases and Information Management
Database Systems (資料庫系統)
Chapter 2: Intro to Relational Model
Chengyu Sun California State University, Los Angeles
Chapter 2: Intro to Relational Model
XML Constraints Constraints are a fundamental part of the semantics of the data; XML may not come with a DTD/type – thus constraints are often the only.
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
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”,
A – Pre Join Indexes.
Database Systems: Design, Implementation, and Management
Conceptual Data Modeling
Tutorial 9 Using Action Queries and Advanced Table Relationships
Legacy Databases.
CS 405G: Introduction to Database Systems
Presentation transcript:

“Those who cannot remember the past are doomed to repeat it” 6.830 / 6.814 Lecture 2 Data Models Sam Madden 9/11/2017 PS1 Out

Zoo Data Model Entity Relationship Diagram 1 Animal n contains 1 relationship Cage 1 Age Name Species 1 name age species entity entity Building 1 bldg Time 1 feedTime n keeps n Keeper 1 Name 1 name entity Animals have names, ages, species Keepers have names Cages have cleaning times, buildings Animals are in 1 cage; cages have multiple animals Keepers keep multiple cages, cages kept by multiple keepers

Zoo Tables Foreign Key Primary Key Primary Key Primary Foreign Key Key Animals id name age species cageno 1 Sam 3 Salamander 2 Aaron 12 Giraffe Sally Student Foreign Key Primary Key Cages no feedtime building 1 12:30 2 1:30 Primary Key Keepers Keeps id name 1 12:30 2 1:30 kid cageno 1 2 Primary Key Foreign Key Foreign Key

Cages in Building 32 NESTED LOOPS JOIN Imperative Declarative for each row a in animals for each row c in cages if a.cageno = c.no and c.bldg = 32 output a Declarative SELECT a.name FROM animals AS a, cages AS c WHERE a.cageno = c.no AND c.bldg = 32 JOIN

Average Age of Bears Declarative SELECT AVG(age) FROM animals WHERE species = ‘bear’

Complex Queries Find pairs of animals of the same species and different genders older than 1 year: SELECT a1.name,a2.name FROM animals as a1, animals as a2 WHERE a1.gender = M and a2.gender = F AND a1.species = a2.species AND a1.age > 1 and a2.age > 1 “self join” Find cages with salamanders fed later than the average feedtime of any cage: SELECT cages.cageid FROM cages, animals WHERE animals.species = ’salamander' AND animals.cageid = cages.cageid AND cages.feedtime > (SELECT AVG(feedtime) FROM cages ) “nested queries”

Modified Zoo Data Model Slightly different than last time: Each animal in 1 cage, multiple animals share a cage Each animal cared for by 1 keeper, keepers care for multiple animals Animals have feed times, not cages

Example IMS Hierarchy A1 Segment A2 Segment A3 Segment Keepers segment A1 Segment A2 Segment A3 Segment C1 Segment C2 Segment C3 Segment Jane (keeper) (HSK 1) Sam, salamander, … (2) 1, 100sq ft, … (3) Mike, giraffe, … (4) 2, 1000sq ft, … (5) Sally, student, … (6) 1, 100sq ft, … (7) Joe (keeper) (8)

IMS Commands GU (segment type, predicate) - get unique Optional predicate can be used when root is indexed Finds the first segment satisfying a predicate, and set cursor there GN (seg, pred) - get next key in hierarchical order moving on to other parents optional predicate search begins from last GU/GN call GNP (seg) - get next record of the specified segment type, stopping when leaving current parent D - delete this record I - add a new record

Example IMS Programs Find the cages that Jane keeps GetUnique(Keepers, name = "Jane") Until done: cageid = GetNextParent (cages).no print cageid

Example IMS Programs Find the keepers that keep cage 6 GetUnique(keepers) GetNextParent(cages, id = 6) Until done: GetNext(keepers)

Study break #1 Consider a course schema with students, classes, rooms (each has a number of attributes) Classes in exactly one room Students in zero or more classes Classes taken by zero or more students Rooms host zero or more classes Classes Students Rooms isin takenby

Questions Describe one possible hierarchical schema for this data Is there a hierarchical representation that is free of redundancy? Classes Students Rooms isin takenby

Example CODASYL Hierarchy

Example CODASYL Program Find the cages that Joe keeps Find keepers (name = 'Joe') Until done: Find next animal in caredforby Find cage in livesin

Simplified Zoo Relations animals name age species cageno keptby feedtime mike 13 giraffe 1 10:00am sam 3 salam 2 11:00am sally student 1:00pm keepers Primary Key Foreign Key keeper name 1 jenny 2 joe cages cageno bldg 1 2 3

Study Break # 2 SELECT s_name FROM student,takes,classes Schema: classes: (cid, c_name, c_rid, …) rooms: (rid, bldg, …) students: (sid, s_name, …) takes: (t_sid, t_cid) SELECT s_name FROM student,takes,classes WHERE t_sid=sid AND t_cid=cid AND c_name=‘6.830’

Questions Write an equivalent relational algebra expression for this query Are there other possible expressions? Do you think one would be more “efficient” to execute? Why? SELECT s_name FROM student,takes,classes WHERE t_sid=sid AND t_cid=cid AND c_name=‘6.830’