35
Database Management Peter Wood The Relational Data Model Schemas Relations and Databases Integrity Constraints ERD to Relations The Relational Data Model The data structures of the relational model I Attributes and domains I Relation schemas and database schemas I Relations and databases I First normal form (1NF)

The Relational Data Model Management Databaseptw/teaching/DBM/rel-model.pdf · Definition of a key for R. A (candidate) ... Foreign keys are declared by the database designer or

Embed Size (px)

Citation preview

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

The Relational Data Model

The data structures of the relational model

I Attributes and domainsI Relation schemas and database schemasI Relations and databasesI First normal form (1NF)

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Running Example Database

Pubs-Drinkers-DB:

Pubs (name, location)Drinkers (name, location)Sells (pub, beer, price)Visits (drinker, pub)

I each pub has a name and locationI each drinker has a name and location where they liveI pubs sell beers at various pricesI drinkers visit various pubs

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Running Example Tables

Pubs:

name locationHorse and Hound BloomsburyHound and Hare IslingtonMarch Hare BloomsburyBlack Horse IslingtonWhite Horse Bloomsbury

Sells:

pub beer priceHorse and Hound Bad Habit 1.50Horse and Hound Rampant Ram 2.00Hound and Hare Shining Wit 2.75Hound and Hare Rampant Ram 2.50March Hare Bad Habit 1.75March Hare Rampant Ram 2.50Black Horse Bad Habit 2.50Black Horse Shining Wit 2.25Black Horse Rampant Ram 2.50White Horse Rampant Ram 2.75

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Running Example Tables

Drinkers:

name locationAlice IslingtonBob BloomsburyCarol IslingtonDave BloomsburyEve Stratford

Visits:

drinker pubAlice Black HorseAlice Hound and HareBob Horse and HoundBob White HorseCarol March HareDave Hound and HareEve March Hare

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Attributes and Domains

I Names such as “beer” and “price” are known asattributes

I Each attribute A has values drawn from a domain,denoted DOM(A)

I In practice, this domain will be specified by a type,such as em string or integer

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Relation Schemas

A relation schema (or table header) R has the followingcomponents:

I A relation symbol R, which is the name of theschema.

I A set of attributes (or column headers), denoted byschema(R).

E.g. Visits is a relation symbol,schema(Visits) = { drinker, pub }.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Relation Schemas

A relation schema (or table header) R has the followingcomponents:

I A relation symbol R, which is the name of theschema.

I A set of attributes (or column headers), denoted byschema(R).

E.g. Visits is a relation symbol,schema(Visits) = { drinker, pub }.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Database Schemas

A database schema R is a collection {R1, . . . ,Rn} ofrelation 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(Ri) such that Ri isin R.

E.g. schema(Pubs-Drinkers-DB) = { name, location,pub, beer, price, drinker }

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Database Schemas

A database schema R is a collection {R1, . . . ,Rn} ofrelation 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(Ri) such that Ri isin R.

E.g. schema(Pubs-Drinkers-DB) = { name, location,pub, beer, price, drinker }

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Database Schemas

A database schema R is a collection {R1, . . . ,Rn} ofrelation 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(Ri) such that Ri isin R.

E.g. schema(Pubs-Drinkers-DB) = { name, location,pub, beer, price, drinker }

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Database Schemas

A database schema R is a collection {R1, . . . ,Rn} ofrelation 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(Ri) such that Ri isin R.

E.g. schema(Pubs-Drinkers-DB) = { name, location,pub, beer, price, drinker }

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

First Normal Form Relation Schema

I A relation schema R is in First Normal Form (1NF)if all the domains of attributes Ai in schema(R) areatomic.(I.e. non-decomposable by the DBMS.)

I A database schema R is in 1NF if all the relationschemas Ri in R are in 1NF.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

A non-1NF version of the Visits table

Visits:

drinker pubAlice Black Horse

Hound and HareBob Horse and Hound

White HorseCarol March HareDave Hound and HareEve March Hare

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

From now on we will assume that database schemasare 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.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Relations and Databases

A tuple (or row) over R, with schema(R) = {A1, . . . ,Am} isa member of

DOM(A1)× . . .× DOM(Am),

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 {r1, . . . , rn} ofrelations ri over Ri .

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Cartesian Product ExampleAll possible Drinker and Beer values:

Drinker BeerAlice Bad HabitBob Rampant Ram

Carol Shining Wit

Drinker BeerAlice Bad HabitAlice Rampant RamAlice Shining WitBob Bad HabitBob Rampant RamBob Shining Wit

Carol Bad HabitCarol Rampant RamCarol Shining Wit

Drinker × Beer

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Drinker × Beer represents all possible combinations ofDrinker and Beer values, i.e., all possible tuples (rows).

A relation called Likes, representing drinkers and thebeers they like, will be a subset of Drinker × Beer, e.g.:

Drinker BeerAlice Shining WitBob Bad HabitBob Shining Wit

Carol Rampant Ram

The contents of the relation could change over time.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

First Normal Form (1NF)

Relations over 1NF relation schemas are called 1NFrelations (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.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Synonymous terminology

I relation schema = table headerI database schema = database headersI relation = tableI database = database tablesI attribute = column headerI attribute value = table cellI tuple = row

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Summary of the properties of relations

I Relation names in a database are distinct.I Attribute names in a relation are distinct.I The order of attributes and tuples in a relation is not

important.I No two tuples in a relation are the same, i.e. a

relation does not contain duplicate rows.I Attribute values are atomic.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Null Values

We must allow for missing or incomplete information byallowing null values as place holders for non-nullconstants.

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 tablebut do not know where she lives, we can store the row(Alice, null).

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

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. ift = (March Hare, Bad Habit, 1.75)

is a tuple over Sells, thent [pub] = (March Hare)

andt [beer, price] = (Bad Habit, 1.75).

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Superkeys

Definition of a superkey for R.

A subset S of schema(R) is a superkey for R iffor all relation instances r of R, the projection t [S] of anytuple t over R uniquely identifies a single tuple in r .

This must hold for all time.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Keys

Definition of a key for R. A (candidate) key for a relationschema R is a superkey for R having a minimal numberof attributes.

Definition of primary key of R. A primary key for R isone of the candidate keys, which is designated by thedatabase designer as being primary.

Question. What are the candidate keys for thePubs-Drinkers DB tables?

Using the value of a primary key for a table ensures thatonly one row (entity) will be retrieved from the table, i.e., asingle row is identified.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

First Fundamental Integrity 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 ] oftuples t in relations over R should not contain null values.

This is because, since the value of null is unknown, itcould be equal to some other key value which wouldmean the key was no longer unique.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Foreign Keys

Let R be a database schema, R1,R2 be relation schemasin R and assume that K is the primary key of R2.

Definition of a foreign key.

F , a subset of R1, is a foreign key for R1 referencing theprimary key K of R2 if the following condition holds:

for all database instances d = {r1, r2, . . . , rn} of R and forall tuples t1 in r1, ift1[F ] does not contain any null values, thenthere exists a tuple t2 in r2 such that t1[F ] = t2[K ].

Foreign keys are declared by the database designer orDBA.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Second Fundamental Integrity Constraint(of the relational model)

Let F be a foreign key for R2 referencing K in R1.

Definition of referential integrity. If the foreign keyvalues t [F ] of a tuple t in a relation over R2 are allnon-null, thent [F ] are primary key values for K in the referencedrelation over R1.

Referential integrity ensures that references to values donot become “dangling”.

Question. What are the foreign keys for thePubs-Drinkers DB tables?

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Translating from ER Diagrams to DatabaseSchemas

EssentiallyI each entity type becomes a relation schemaI each relationship type becomes a relation schema

but

I weak entity types need special treatmentI ISA relationship types need special treatmentI sometimes it makes sense to combine relations

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Modelling Entity Types in the RelationalModel

An entity type E , having attributes A1, . . . ,Am, is modelledby a relation schema R, with schema(R) = {A1, . . . ,Am}.

I The primary key of R is the primary key of E .

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Modelling Relationship Types in theRelational Model

A many-to-many relationship type M involving entity typesE1, . . . ,Em is modelled by a relation schema R, withschema(R) = (K1, . . . ,Km), where K1, . . . ,Km are theprimary keys of the entity types E1, . . . ,Em.

I If M has attributes, these are added to schema(R).I The primary key of R is (K1, . . . ,Km).

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Question. What happens if a relationship type ismany-to-one or one-to-one ?

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Answer. No new relation schemas need to defined butforeign keys need be present in the appropriate relationschemas.

Assume that we haveI a many-to-one relationship type from E2 to E1

I the primary key of E1 is K1

I relation schemas R1 modelling E1 and R2 modellingE2

Then K1 must be included in schema(R2).

I K1 is a foreign key in R2 referencing K1 in R1.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Modelling Weak Entities in the RelationalModel

Assume that, in an ERD, we haveI a weak entity type EI E has discriminator DI the other attributes of E are A1, . . . ,Am

I the owner entity type of E has primary key KThen E is modelled by a relation schema R, withschema(R) = {K ,D,A1, . . . ,Am}.

I The primary key of R is (K ,D).

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

Modelling ISA Relationship Types in theRelational Model

Assume that, in an ERD, we haveI entity type E1 with attributes A1, . . . ,Am

I entity type E2 with attributes B1, . . . ,Bn

I an ISA relationship type from E1 to E2

Then E1 can be modelled by a relation schema R, withschema(R) = {A1, . . . ,Am,B1, . . . ,Bn}.

I The primary key of R is the same as that of E2.

DatabaseManagement

Peter Wood

The RelationalData Model

Schemas

Relations andDatabases

IntegrityConstraints

ERD to Relations

For more information

See, e.g.,I Chapter 4 of [Connolly and Begg].I Chapters 2 and 7 of [Silberschatz et al.].I Chapters 2 and 4 of [Ullman and Widom].