Running Example Tables name location

Similar documents
The data structures of the relational model Attributes and domains Relation schemas and database schemas

Supplier-Parts-DB SNUM SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens

user specifies what is wanted, not how to find it

Chapter 4. The Relational Model

What is a Database? Peter Wood

Applied Databases. Sebastian Maneth. Lecture 5 ER Model, normal forms. University of Edinburgh - January 25 th, 2016

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Applied Databases. Sebastian Maneth. Lecture 5 ER Model, Normal Forms. University of Edinburgh - January 30 th, 2017

CS275 Intro to Databases

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

8) A top-to-bottom relationship among the items in a database is established by a

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

CS 377 Database Systems

DATABASE TECHNOLOGY - 1DL124

The Relational Model. Chapter 3

The Relational Model

Let s briefly review important EER inheritance concepts

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

The Relational Model 2. Week 3

CSIT5300: Advanced Database Systems

The Relational Model. Week 2

CS530 Database Architecture Models. Database Model. Prof. Ian HORROCKS. Dr. Robert STEVENS. and Design The Relational

ER Model. Objectives (2/2) Electricite Du Laos (EDL) Dr. Kanda Runapongsa Saikaew, Computer Engineering, KKU 1

THE RELATIONAL DATABASE MODEL

A database can be modeled as: + a collection of entities, + a set of relationships among entities.

The Relational Data Model and Relational Database Constraints

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

DATABASE DESIGN I - 1DL300

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Relational Model and Relational Algebra. Rose-Hulman Institute of Technology Curt Clifton

Relational Data Model ( 관계형데이터모델 )

Databases. Jörg Endrullis. VU University Amsterdam

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

Database Management Systems. Chapter 3 Part 2

The Entity Relationship Model

CS403- Database Management Systems Solved MCQS From Midterm Papers. CS403- Database Management Systems MIDTERM EXAMINATION - Spring 2010

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

A database consists of several tables (relations) AccountNum

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

Database Applications (15-415)

Chapter 3. The Relational database design

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

CSC 742 Database Management Systems

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

CSE 132A Database Systems Principles

Chapter 5. Relational Model Concepts 9/4/2012. Chapter Outline. The Relational Data Model and Relational Database Constraints

SQL DATA DEFINITION LANGUAGE

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

SQL DATA DEFINITION LANGUAGE

CS403- Database Management Systems Solved Objective Midterm Papers For Preparation of Midterm Exam

Relational Data Model

Database Systems ( 資料庫系統 )

Database Management

MIDTERM EXAMINATION Spring 2010 CS403- Database Management Systems (Session - 4) Ref No: Time: 60 min Marks: 38

High Level Database Models

Relational Data Model. Christopher Simpkins

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

In This Lecture. The Relational Model. The Relational Model. Relational Data Structure. Unnamed and named tuples. New thing:scheme (and attributes)

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

Chapter 5. Relational Model Concepts 5/2/2008. Chapter Outline. Relational Database Constraints

Relational model. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

BIRKBECK (University of London)

RELATIONAL DATA MODEL

Chapter 2: Intro to Relational Model

Entity Attribute STUDENT TABLE tuples single domain

The Relational Model

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Relational data model

Introduction to Database Systems. The Relational Data Model

The Relational Model

Introduction to Database Systems. The Relational Data Model. Werner Nutt

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Chapter 5. The Relational Data Model and Relational Database Constraints. Slide 5-١. Copyright 2007 Ramez Elmasri and Shamkant B.

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 5-1

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

Systems Analysis and Design in a Changing World, Fourth Edition. Chapter 12: Designing Databases

SQL DATA DEFINITION LANGUAGE

THE RELATIONAL DATA MODEL CHAPTER 3 (6/E) CHAPTER 5 (5/E)

Database Applications (15-415)

Data about data is database Select correct option: True False Partially True None of the Above

Informatics 1: Data & Analysis

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Chapter 2 Introduction to Relational Models

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Lecture4: Guidelines for good relational design Mapping ERD to Relation. Ref. Chapter3

The Relational Data Model. Data Model

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

CS54100: Database Systems

Transcription:

Running Example Pubs-Drinkers-DB: The data structures of the relational model Attributes and domains Relation schemas and database schemas databases Pubs (name, location) Drinkers (name, location) Sells (pub, beer, price) Visits (drinker, pub) First normal form (1NF) each pub has a name and location each drinker has a name and location where they live pubs sell beers at various prices drinkers visit various pubs Running Example Tables Pubs: Sells: name location Horse and Hound Bloomsbury Hound and Hare Islington March Hare Bloomsbury Black Horse Islington White Horse Bloomsbury pub beer price Horse and Hound 1.50 Horse and Hound 2.00 Hound and Hare 2.75 Hound and Hare 2.50 March Hare 1.75 March Hare 2.50 Black Horse 2.50 Black Horse 2.25 Black Horse 2.50 White Horse 2.75 Running Example Tables name location Islington Drinkers: Bloomsbury Islington Dave Bloomsbury Eve Stratford drinker pub Black Horse Hound and Hare Visits: Horse and Hound White Horse March Hare Dave Hound and Hare Eve March Hare

Attributes and Domains Relation Names such as beer and price are known as attributes Each attribute A has values drawn from a domain, denoted DOM(A) In practice, this domain will be specified by a type, such as em string or integer A relation schema (or table header) R has the following components: A relation symbol R, which is the name of the schema. A set of attributes (or column headers), denoted by schema(r). E.g. Visits is a relation symbol, schema(visits) = { drinker, pub }. First Normal Form Relation Schema A database schema R is a collection {R 1,..., R n } of relation schemas (table headers). E.g. The database schema of the Pubs-Drinkers-DB is { Drinkers, Pubs, Sells, Visits }. Notation. schema(r) is the union of all schema(r i ) such that R i is in R. A relation schema R is in First Normal Form (1NF) if all the domains of attributes A i in schema(r) are atomic. (I.e. non-decomposable by the DBMS.) A database schema R is in 1NF if all the relation schemas R i in R are in 1NF. E.g. schema(pubs-drinkers-db) = { name, location, pub, beer, price, drinker }

A non-1nf version of the Visits table drinker pub Black Horse Hound and Hare Visits: Horse and Hound White Horse March Hare Dave Hound and Hare Eve March Hare From now on we will assume that database schemas are in 1NF. The justification for this assumption is: 1. The semantics of 1NF are easy to understand (e.g. ADDRESS vs. ST_NO, ST_NAME and CITY). 2. 1NF makes it easier to formalise the relational model; flat relations (Visits table) vs. nested relations (Visits non-1nf table). 3. 1NF makes querying simpler too. Cartesian Product Example All possible Drinker and Beer values: A tuple (or row) over R, with schema(r) = {A 1,..., A m } is a member of DOM(A 1 )... DOM(A m ), where is the Cartesian product operator. A relation (or table) over R is a finite set of tuples over R. A database d over R is a collection {r 1,..., r n } of relations r i over R i. Drinker Drinker Beer Beer Drinker Beer

First Normal Form (1NF) Drinker Beer represents all possible combinations of Drinker and Beer values, i.e., all possible tuples (rows). A relation called Likes, representing drinkers and the beers they like, will be a subset of Drinker Beer, e.g.: Relations over 1NF relation schemas are called 1NF relations (or flat relations or simply relations). Drinker Beer 1NF relations are advantageous since they have 1. A simple tabular representation. 2. Simple query languages. 3. A simple set of fundamental integrity constraints. The contents of the relation could change over time. Synonymous terminology Summary of the properties of relations relation schema = table header database schema = database headers relation = table database = database tables attribute = column header attribute value = table cell tuple = row Relation names in a database are distinct. Attribute names in a relation are distinct. The order of attributes and tuples in a relation is not important. No two tuples in a relation are the same, i.e. a relation does not contain duplicate rows. Attribute values are atomic.

Null Values Projection We must allow for missing or incomplete information by allowing null values as place holders for non-null constants. E.g. An Employee s address is unknown. E.g. An Employee s spouse does not exist. The special place holder null will be used as a null value. e.g. if we want to represent in the Drinkers table but do not know where she lives, we can store the row (, null). Let R be a relation schema, X be a subset of schema(r) (i.e., a set of attributes) and t be a tuple over R. The projection of t onto X, denoted by t[x], is the collection of values of t for the attributes in X, i.e. the restriction of t to X. E.g. if t = (March Hare,, 1.75) is a tuple over Sells, then t[pub] = (March Hare) and t[beer, price] = (, 1.75). Superkeys Keys Definition of a superkey for R. A subset S of schema(r) is a superkey for R if for all relation instances r of R, the projection t[s] of any tuple t over R uniquely identifies a single tuple in r. This must hold for all time. Definition of a key for R. A (candidate) key for a relation schema R is a superkey for R having a minimal number of attributes. Definition of primary key of R. A primary key for R is one of the candidate keys, which is designated by the database designer as being primary. Question. What are the candidate keys for the Pubs-Drinkers DB tables? Using the value of a primary key for a table ensures that only one row (entity) will be retrieved from the table, i.e., a single row is identified.

First Fundamental Constraint (of the relational model) Foreign Keys Let K be the primary key of some relation schema R. Definition of entity integrity. Primary key values t[k ] of tuples t in relations over R should not contain null values. This is because, since the value of null is unknown, it could be equal to some other key value which would mean the key was no longer unique. Let R be a database schema, R 1, R 2 be relation schemas in R and assume that K is the primary key of R 2. Definition of a foreign key. F, a subset of R 1, is a foreign key for R 1 referencing the primary key K of R 2 if the following condition holds: for all database instances d = {r 1, r 2,..., r n } of R and for all tuples t 1 in r 1, if t 1 [F] does not contain any null values, then there exists a tuple t 2 in r 2 such that t 1 [F] = t 2 [K ]. Foreign keys are declared by the database designer or DBA. Second Fundamental Constraint (of the relational model) Translating from ER Diagrams to Let F be a foreign key for R 2 referencing K in R 1. Definition of referential integrity. If the foreign key values t[f] of a tuple t in a relation over R 2 are all non-null, then t[f] are primary key values for K in the referenced relation over R 1. Essentially each entity type becomes a relation schema but each relationship type becomes a relation schema Referential integrity ensures that references to values do not become dangling. weak entity types need special treatment ISA relationship types need special treatment Question. What are the foreign keys for the Pubs-Drinkers DB tables? sometimes it makes sense to combine relations

Modelling Entity Types in the Relational Model Modelling Relationship Types in the Relational Model An entity type E, having attributes A 1,..., A m, is modelled by a relation schema R, with schema(r) = {A 1,..., A m }. The primary key of R is the primary key of E. A many-to-many relationship type M involving entity types E 1,..., E m is modelled by a relation schema R, with schema(r) = (K 1,..., K m ), where K 1,..., K m are the primary keys of the entity types E 1,..., E m. If M has attributes, these are added to schema(r). The primary key of R is (K 1,..., K m ). Question. What happens if a relationship type is many-to-one or one-to-one? Answer. No new relation schemas need to defined but foreign keys need be present in the appropriate relation schemas. Assume that we have a many-to-one relationship type from E 2 to E 1 the primary key of E 1 is K 1 relation schemas R 1 modelling E 1 and R 2 modelling E 2 Then K 1 must be included in schema(r 2 ). K 1 is a foreign key in R 2 referencing K 1 in R 1.

Modelling Weak Entities in the Relational Model Modelling ISA Relationship Types in the Relational Model Assume that, in an ERD, we have a weak entity type E E has discriminator D the other attributes of E are A 1,..., A m the owner entity type of E has primary key K Then E is modelled by a relation schema R, with schema(r) = {K, D, A 1,..., A m }. The primary key of R is (K, D). Assume that, in an ERD, we have entity type E 1 with attributes A 1,..., A m entity type E 2 with attributes B 1,..., B n an ISA relationship type from E 1 to E 2 Then E 1 can be modelled by a relation schema R, with schema(r) = {A 1,..., A m, B 1,..., B n }. The primary key of R is the same as that of E 2. For more information See, e.g., Chapter 4 of [Connolly and Begg]. Chapters 2 and 7 of [Silberschatz et al.]. Chapters 2 and 4 of [Ullman and Widom].