Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

IS698: Database Management Min Song IS NJIT. The Relational Data Model.
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.
Relational Model (CB Chapter 4) CPSC 356 Database Ellen Walker Hiram College.
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Chapter Secondary Storage Rough Speed Differentials –nanoseconds: retrieve data in main memory –microseconds: retrieve from disk cache or under a.
Review Indra Budi Fakultas Ilmu Komputer UI 2 Database Introduction Database vs File Processing Main purpose of database Database Actors.
Review for Final Test Indra Budi
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
The Relational Database Model
Information Resources Management February 27, 2001.
NestedRelations: 1 Nested Relations Flat schemas often have replicated data values in their relations. Nested schemas allow us to collapse some of these.
Chapter Design Objectives Obtain the theoretically “best” design (normalize) –Remove redundancy and update anomalies –Remove nulls –Minimize the.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
XNF: 1 XML and NNF A Standard Form for XML Documents (XNF) Properties –As few hierarchical trees as possible –No redundant data values in any tree Method.
Chapter Relational Calculus Queries are open predicate-calculus formulas General form: { | F(x 1, …, x n ) } –Standard set-builder notation (i.e.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
FileOrg: 1 Secondary Storage Rough Speed Differentials –nanoseconds: retrieve data in main memory –microseconds: retrieve from under a read head –milliseconds:
Chapter Nested Schemes Flat schemes often have replicated data values. Nested schemes allow us to collapse some of these replicated data values.
XNF-1 XML and NNF A Standard Form for XML Documents (XNF) Properties –As few hierarchical trees as possible –No redundant data values in any tree Method.
View Processing & Update. View Processing create view PresSuite as select * from Room where NrBeds = 2 select * from PresSuite p natural join Reservation.
Create, Insert, Delete, Update. Create Create database Create table Create index – Primary – Secondary.
CostAnalysis: 1 Cost Analysis Rule-of-Thumb Guidelines As a guide, consider denormalizing if: –redundancy is minimal and update anomalies are not expected.
QueryRewriting: 1 Query Rewriting (for Query “Optimization”) Main Strategy: Make intermediate results small by applying selection and projection early.
CS405G: Introduction to Database Systems Final Review.
Chapter Implementation Faithful translation of a design into a target environment Design should be free of target-environment dependencies Should.
Design Algorithm Essentials 1.Combine functional edges with the same tail object set(s) into a scheme. (The tail object set(s) and any others in a 1-1.
Midterm 1 Concepts Relational Algebra (DB4) SQL Querying and updating (DB5) Constraints and Triggers (DB11) Unified Modeling Language (DB9) Relational.
Databases Illuminated
Information storage: Introduction of database 10/7/2004 Xiangming Mu.
Chapter 4 The Relational Model.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Concepts and Terminology Introduction to Database.
FEN  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
Logical Database Design (1 of 3) John Ortiz Lecture 6Logical Database Design (1)2 Introduction  The logical design is a process of refining DB schema.
SE305 Database System Technology 23/10/2014 Quiz-2.
Databases Illuminated Chapter 3 The Entity Relationship Model.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
RelAlg: 1 Relational Algebra An Algebra is a pair: (set of values, set of operations) Note that an algebra is the same idea as an ADT Relational Algebra:
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
CSE 412/598 DATABASE MANAGEMENT COURSE NOTES 3. ENTITY-RELATIONSHIP CONCEPTUAL MODELING Department of Computer Science & Engineering Arizona State University.
Extended ER Mappings & Models. General Principle #1 Graphical representations dictate cardinality relationships among attributes. When graphical representations.
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.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
The Relational Data Model & Relational Algebra
CS580 Advanced Database Topics
Entity-Relationship Model
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Chapter 2: Intro to Relational Model
The Relational Model Textbook /7/2018.
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
SQL: Structured Query Language
Presentation transcript:

Chapter Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable) –systematic translation –ad hoc translation –not translatable (incomplete or unclear)

Chapter The 3-Level Architecture view 2view nview 1 Conceptual Database Physical Database Abstract Concrete Implementation Independence

Chapter Abstract Data Type (ADT) Abstract Description of a Data Type (Set of Values, Set of Operations) Examples: –Integer = ({0, -1, 1, -2, 2, …}, {+, -, *, /}) –Stack = (all possible stacks, {push, pop, top, empty}) –Database = (all possible DB states, {insert, remove, retrieve}) Description of all possible DB States: –Description of Conceptual Database –Scheme, Schema, Model

Chapter Entity-Relationship (ER) Model Entity -- object Relationship -- connection among objects Attribute -- properties of objects and connections

Chapter ER Application Model

Chapter ISA – Generalization/Specialization

Chapter Cardinality Relationships 1-1 m-1; 1-m seen the other way m-n

Chapter Entity Key Uniquely Identifies an Individual Entity Keys: A or BC

Chapter Relationship Key Uniquely Identifies an Individual Relationship Key: A or B Key: A Key: AB

Chapter Key Attributes for Relationships Relationship attributes are sometimes needed to uniquely Identify an individual relationship. Depending on the semantics, the key could be: ABC, AC, BC, or C.

Chapter Keys Superkey: A set of attributes that uniquely identifies Minimal Key: A minimal superkey. Candidate Key: Same as minimal key. Primary Key: A chosen candidate key.

Chapter Scheme Generation Generate a scheme for each entity set (except specializations) –The attributes are the attributes of the entity set. –The keys are the candidate keys of the entity set; choose a primary key. For each relationship set, adjust or add a scheme. –1-1: merge schemes; add any relationship attributes. –1-m, m-1: to the scheme on the many-side, add the primary-key attribute(s) of the one-side; add any relationship attributes. –m-m: make a new scheme from the primary-key attributes of the connected entity sets and any relationship attributes; the key is the composite of the primary-key attributes if there are no key relationship attributes—otherwise semantically determine the key. Generate a scheme for each specialization. –The attributes are the attributes of the specialization plus the primary key attribute(s) of the generalization. –The key is the primary key of the generalization.

Chapter Scheme Generation – Example Generated schemes, with candidate keys underlined and primary keys double underlined: Room(RoomNr, Name, NrBeds, Cost) Guest(GuestNr, Name, StreetNr, City) Reservation(GuestNr, RoomNr, ArrivalDate, NrDays) RoomUnderConstruction(RoomNr, CompletionDate)

Chapter SQL DDL create table Room( RoomNr integer primary key, Name char(20) unique, NrBeds integer, Cost float ); create table Guest( GuestNr integer primary key, Name char(20), StreetNr char(20), City char(15), unique (Name, StreetNr, City) ); create table Reservation( GuestNr integer references Guest, RoomNr integer references Room, ArrivalDate char(6), NrDays integer, primary key (RoomNr, ArrivalDate) );

Chapter SQL DDL create table Room( RoomNr integer primary key, Name char(20) unique, NrBeds integer, Cost float ); create table Guest( GuestNr integer unique, Name char(20), StreetNr char(20), City char(15), primary key (Name, StreetNr, City) ); create table Reservation( Name char(20), StreetNr char(20), City char(15), RoomNr integer references Room, ArrivalDate char(6), NrDays integer, primary key (RoomNr, ArrivalDate), foreign key (Name, StreetNr, City) references Guest );

Chapter Sample Database Instance r = Room(RoomNr Name NrBeds Cost) Kennedy Nixon Carter Blue Green 1 50 g = Guest(GuestNr Name StreetNr City) Smith 12 Maple Boston 102 Carter 10 Main Hartford 103 Jones 6 Elm Hartford 104 Smith 4 Oak Providence 105 Green 10 Main Boston 106 Johnson 15 Main Boston s = Reservation(GuestNr RoomNr ArrivalDate NrDays) May May May May May May May May May May 2

Chapter Relational Algebra An Algebra is a Pair: (set of values, set of operations) Note that an Algebra is the same idea as an ADT Relational Algebra: (relations, relational operators) –set of values = relations –set of operations = relational operators Relational Operators –update operators: insert a tuple delete one or more tuples modify one or more tuples –retrieval operators: { , , , , , , ,  }

Chapter  – Selection General Form:  Examples:  Cost > 75 r  ArrivalDate = 10 May  NrDays > 2 s RoomNr Name NrBeds Cost Kennedy Nixon Carter 2 80 GuestNr RoomNr ArrivalDate NrDays May 5

Chapter  – Projection General Form:  Examples:  City g  GuestNr,RoomNr s City Boston Hartford Providence GuestNr RoomNr

Chapter Closed Set of Operators Results are relations Closed implies we can nest operations in expressions Example:  GuestNr,RoomNr  ArrivalDate = 10 May s GuestNr RoomNr

Chapter Set Operators: , ,  Name Carter Green RoomNr RoomNr  Name r   Name g  RoomNr r   RoomNr s  RoomNr  Cost < 75 r   RoomNr  ArrivalDate = 10 May s Note: schemes must be compatible.

Chapter  – Renaming General Form:   Examples:  RoomName  Name  RoomName  Cost < 75 r  Nr, Name  RoomNr  Nr r   Nr, Name  GuestNr  Nr g Note: The old attribute must be in the scheme and the new attribute must not be in the scheme. RoomName Blue Green Nr Name

Chapter  – Cross Product  NrBeds r   RoomNr s  NrBeds RoomNr NrBeds RoomNr = Note: The intersection of the schemes must be empty.

Chapter  – Natural Join r  g =  RoomNr,Name,NrBeds,Cost,GuestNr,StreetNr,City  Name = Name  (r   Name  Name  g) RoomNr Name NrBeds Cost GuestNr StreetNr City Carter Main Hartford 5 Green Main Boston

Chapter Natural Join – Examples A B B C A B C A B C D A B C D A B C B C D A B C D = = = 

Chapter Query Examples List names and cities of guests arriving on 15 May.  Name,City  ArrivalDate = 15 May (g  s) List names of each guest who has a reservation for a room that has the same name as the guest’s name.  Name (g  s  r) List names of guests who have a reservation for rooms with two beds.  Name (g  s   RoomNr  NrBeds = 2 r)

Chapter More Query Examples List the names of guests from Hartford who are arriving after 10 May.  Name (  GuestNr,Name  City = Hartford g   GuestNr  ArrivalDate > 10 May s) List names of rooms for which no guest is arriving on 10 May.  Name (r  (  RoomNr r -  RoomNr  ArrivalDate = 10 May s))

Chapter SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r minus select B from s select A as “D” from r select A, r.B, C from r, s where r.B = s.B  A  B = 1 r  B r -  B s  D  A  D r  A, r.B, C  r.B = s.B (r  s)

Chapter SQL: Basic Retrieval Get full details of rooms. select RoomNr, Name, NrBeds, Cost from Room select * from Room Get costs. select Cost from Room select distinct Cost from Room COST COST

Chapter SQL: Aggregate Operations Get average cost. select avg(Cost) from Room select avg(Cost) as “AVE$” from Room AVE$ AVG(COST) Also: min, max, sum, count.

Chapter SQL: Sorting Get distinct costs in descending order. select distinct Cost from Room order by Cost desc Get cost and number of beds, sorted with beds in ascending order and cost in descending order. select distinct NrBeds, Cost from Room order by NrBeds asc, Cost desc COST NRBEDS COST

Chapter SQL: Grouping Get the average cost of rooms with the same number of beds. select NrBeds, avg(Cost) from Room group by NrBeds Get guest numbers of guests who have reservations for a total of 5 or more days. select GuestNr from Reservation group by GuestNr having sum(NrDays) >= 5 NRBEDS AVG(COST) GUESTNR

Chapter SQL: Nested Queries Get Names and Addresses of guests who have reservations for a total of 5 or more days. select Name, StreetNr, City from Guest where GuestNr in ( select GuestNr from Reservation group by GuestNr having sum(NrDays) >= 5) NAME STREETNR CITY Smith 12 Maple Boston Carter 10 Main Hartford Smith 4 Oak Providence Green 10 Main Boston

Chapter SQL: Multiple-Relation Retrieval Get names of guests whose names match room names. select Guest.Name from Guest, Room where Guest.Name = Room.Name Get names of each guest who has a reservation for a room that has the same name as the guest’s name. select g.Name from Guest g, Room r, Reservation s where g.Name = r.Name and g.GuestNr = s.GuestNr and s.RoomNr = r.RoomNr NAME Carter Green NAME Carter

Chapter SQL: Select/Project/Join Queries Get names and addresses of guests arriving on 15 May. select distinct Name, StreetNr, City from Guest g, Reservation s where g.GuestNr = s.GuestNr and ArrivalDate != “15 May” … arriving on a date other than 15 May. select distinct Name, StreetNr, City from Guest g, Reservation s where g.GuestNr = s.GuestNr and ArrivalDate = “15 May” NAME STREETNR CITY Smith 12 Maple Boston Green 10 Main Boston NAME STREETNR CITY Smith 12 Maple Boston Carter 10 Main Hartford Jones 6 Elm Hartford Smith 4 Oak Providence Johnson 15 Main Boston

Chapter SQL: Negation Get names and addresses of guests not arriving on 15 May. select Name, StreetNr, City from Guest minus select Name, StreetNr, City from Guest g, Reservation s where g.GuestNr = s.GuestNr and ArrivalDate = “15 May” select Name, StreetNr, City from Guest where GuestNr not in ( select GuestNr from Reservation where ArrivalDate = “15 May”) NAME STREETNR CITY Carter 10 Main Hartford Jones 6 Elm Hartford Smith 4 Oak Providence Johnson 15 Main Boston

Chapter SQL: Joining a Relation with Itself Get pairs of guest numbers of guests arriving on the same date. (The pairs should be unique.) select s.GuestNr, t.GuestNr from Reservation s, Reservation t where s.ArrivalDate = t.ArrivalDate and s.GuestNr < t.GuestNr GUESTNR