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

Similar documents
Running Example Tables name location

user specifies what is wanted, not how to find it

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

What is a Database? Peter Wood

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Chapter 4. The Relational Model

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

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

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

CS275 Intro to Databases

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

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

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

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

The Relational Model. Chapter 3

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

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

The Relational Model 2. Week 3

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

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

The Relational Model

Let s briefly review important EER inheritance concepts

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

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

CSIT5300: Advanced Database Systems

Database Management Systems. Chapter 3 Part 2

Databases. Jörg Endrullis. VU University Amsterdam

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

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

THE RELATIONAL DATABASE MODEL

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

Database Applications (15-415)

The Relational Data Model and Relational Database Constraints

DATABASE DESIGN I - 1DL300

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

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

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

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

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

Chapter 2: Intro to Relational Model

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

Chapter 3. The Relational database design

CSC 742 Database Management Systems

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

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

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

Relational Data Model

SQL DATA DEFINITION LANGUAGE

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

Database Management

High Level Database Models

Database Management

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

Database Systems. Course Administration

L12: ER modeling 5. CS3200 Database design (sp18 s2) 2/22/2018

Chapter 2 Introduction to Relational Models

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

Lecture Notes for 3 rd August Lecture topic : Introduction to Relational Model. Rishi Barua Shubham Tripathi

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

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

BIRKBECK (University of London)

CS54100: Database Systems

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

RELATIONAL DATA MODEL

Entity Attribute STUDENT TABLE tuples single domain

The Relational Model

Introduction to Database Systems. The Relational Data Model

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

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

Relational data model

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

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

The Entity-Relationship Model. Overview of Database Design

The Relational Model

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

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

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

SQL DATA DEFINITION LANGUAGE

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

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

Database Applications (15-415)

Transcription:

The data structures of the relational model Attributes and domains Relation schemas and database schemas databases First normal form (1NF)

Running Example Pubs-Drinkers-DB: Pubs (name, location) Drinkers (name, location) Sells (pub, beer, price) Visits (drinker, pub) 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 Bad Habit 1.50 Horse and Hound Rampant Ram 2.00 Hound and Hare Shining Wit 2.75 Hound and Hare Rampant Ram 2.50 March Hare Bad Habit 1.75 March Hare Rampant Ram 2.50 Black Horse Bad Habit 2.50 Black Horse Shining Wit 2.25 Black Horse Rampant Ram 2.50 White Horse Rampant Ram 2.75

Running Example Tables name location Alice Islington Drinkers: Bob Bloomsbury Carol Islington Dave Bloomsbury Eve Stratford Visits: drinker Alice Alice Bob Bob Carol Dave Eve pub Black Horse Hound and Hare Horse and Hound White Horse March Hare Hound and Hare March Hare

Attributes and Domains 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

Relation 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).

Relation 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 }.

A database schema R is a collection {R 1,..., R n } of relation schemas (table headers).

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 }.

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 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. E.g. schema(pubs-drinkers-db) = { name, location, pub, beer, price, drinker }

First Normal Form Relation Schema 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.

A non-1nf version of the Visits table drinker pub Alice Black Horse Hound and Hare Visits: Bob Horse and Hound White Horse Carol 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.

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.

Cartesian Product Example All possible Drinker and Beer values: Drinker Alice Bob Carol Beer Bad Habit Rampant Ram Shining Wit Drinker Alice Alice Alice Bob Bob Bob Carol Carol Carol Beer Bad Habit Rampant Ram Shining Wit Bad Habit Rampant Ram Shining Wit Bad Habit Rampant Ram Shining Wit Drinker Beer

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.: Drinker Alice Bob Bob Carol Beer Shining Wit Bad Habit Shining Wit Rampant Ram The contents of the relation could change over time.

First Normal Form (1NF) Relations over 1NF relation schemas are called 1NF relations (or flat relations or simply relations). 1NF relations are advantageous since they have 1. A simple tabular representation. 2. Simple query languages. 3. A simple set of fundamental integrity constraints.

Synonymous terminology relation schema = table header database schema = database headers relation = table database = database tables attribute = column header attribute value = table cell tuple = row

Summary of the properties of relations 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 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 Alice in the Drinkers table but do not know where she lives, we can store the row (Alice, null).

Projection 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, Bad Habit, 1.75) is a tuple over Sells, then t[pub] = (March Hare) and t[beer, price] = (Bad Habit, 1.75).

Superkeys 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.

Keys 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) 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.

Foreign Keys 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) 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. Referential integrity ensures that references to values do not become dangling. Question. What are the foreign keys for the Pubs-Drinkers DB tables?

Translating from ER Diagrams to Essentially each entity type becomes a relation schema each relationship type becomes a relation schema but weak entity types need special treatment ISA relationship types need special treatment sometimes it makes sense to combine relations

Modelling Entity 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.

Modelling Relationship Types in the Relational Model 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 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).

Modelling ISA Relationship Types in the Relational Model 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].