Docsity

Relational Model: Data Structure, Integrity, and Manipulation in Databases, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
The Relational Model G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
The Relational Model
• Introduced in E.F.
Codd’s 1970 paper
“A Relation al Model
of Data for Large
Shared Databanks”
• The foundation for
most (but not all)
current database
systems
• Concerned with 3
main things
• Data structure (how
data is represented)
• Data integrity (what
data is allowed)
•Data manipulation
(what you can do with
the data)
The Relational Model G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Relational Data Structure
• Data is stored in
relations (tables)
• Each relation has a
scheme (heading)
• The scheme defines
the relation’s
attributes (columns)
• Data takes the form
of tuples (rows)
Name Age
John 23
Mary 20
Mark 18
Jane 21
The Relational Model G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Relational Data Structure
•More formally -
• A scheme is a set
of attributes
• A tuple assigns a
value to each
attribute in its
scheme
• A relat ion is a set
of tuples with the
same scheme
{ { (Name, John), (Age, 23) },
{ (Name, Mary), (Age, 20) },
{ (Name, Mark), (Age, 18) },
{ (Name, Jane), (Age , 21) } }
Name Age
John 23
Mary 20
Mark 18
Jane 21
The Relational Model G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Properties of Relations
• Since relations are
sets of tuples, the
tuples of a relation
are unique and
unordered
• The number of
tuples in a r elation is
called the cardinality
of the relation
• Since schemes are
sets of attributes,
the attributes of a
relation are unique
and unordered
• The number of
attributes in a
relation is the
relation’s degree
The Relational Model G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Relations
ID Name Salary Department
M139 John Smith 18,000 Marketing
M140 Mary Jones 22,000 M arketing
A368 Jane Brown 22,000 Accounts
P222 Mar k Brown 24,000 Per sonnel
A367 Dav id Jones 20,000 Accounts
The Relational Model G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Relational Data Integrity
• Data integrity controls what data can be
in a relation
•Domains restrict the possible values a tuple
can assign to each attribute
•Candidate and Primary Keys identify tuples
within a re lation
•Foreign Keys link relations to each other
pf3
pf4

Partial preview of the text

Download Relational Model: Data Structure, Integrity, and Manipulation in Databases and more Slides Introduction to Database Management Systems in PDF only on Docsity!

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

The Relational Model

• Introduced in E.F.

Codd’s 1970 paper

“A Relational Model

of Data for Large

Shared Databanks”

• The foundation for

most (but not all)

current database

systems

• Concerned with 3

main things

  • Data structure (how

data is represented)

  • Data integrity (what

data is allowed)

  • Data manipulation

(what you can do with

the data)

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Relational Data Structure

• Data is stored in

relations (tables)

• Each relation has a

scheme (heading)

• The scheme defines

the relation’s

attributes (columns)

• Data takes the form

of tuples (rows)

Name Age John 23 Mary 20 Mark 18 Jane 21

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Relational Data Structure

• More formally -

• A scheme is a set

of attributes

• A tuple assigns a

value to each

attribute in its

scheme

• A relation is a set

of tuples with the

same scheme

{ { (Name, John), (Age, 23) }, { (Name, Mary), (Age, 20) }, { (Name, Mark), (Age, 18) }, { (Name, Jane), (Age, 21) } }

Name Age John 23 Mary 20 Mark 18 Jane 21

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Properties of Relations

• Since relations are

sets of tuples, the

tuples of a relation

are unique and

unordered

• The number of

tuples in a relation is

called the cardinality

of the relation

• Since schemes are

sets of attributes,

the attributes of a

relation are unique

and unordered

• The number of

attributes in a

relation is the

relation’s degree

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Relations

ID Name Salary Department

M139 John Smith 18,000 Marketing M140 Mary Jones 22,000 Marketing A368 Jane Brown 22,000 Accounts P222 Mark Brown 24,000 Personnel A367 David Jones 20,000 Accounts

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Relational Data Integrity

• Data integrity controls what data can be

in a relation

• Domains restrict the possible values a tuple

can assign to each attribute

• Candidate and Primary Keys identify tuples

within a relation

• Foreign Keys link relations to each other

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Attributes and Domains

• A domain is given for

each attribute

• The domain lists the

possible values for

that attribute

• Each tuple assigns a

value to each

attribute from its

domain

• Examples

  • An ‘age’ might have

to come from the set

of integers between 0

and 150

  • A ‘department’ might

come from a given list

of strings

  • A ‘notes’ field might

allow any string at all

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Candidate Keys

• A set of attributes in

a relation is called a

candidate key IFF

  • Every tuple has a

unique value for the

set of attributes

( uniqueness )

  • No proper subset of

the set has the

uniqueness property

( minimality )

ID First Last S139 John Smith S140 Mary Jones S141 John Brown S142 Jane Smith

Candidate keys are {ID} and {First, Last} {ID, First}, {ID, Last} and {ID, First, Last} satisfy uniqueness, but are not minimal

{First} and {Last} do not give a unique identifier for each row

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Primary Keys

• One Candidate Key is

usually chosen to be

used to identify

tuples in a relation

• This is called the

Primary Key

• Often a special ID

attribute is used as

the Primary Key

ID First Last S139 John Smith S140 Mary Jones S141 John Brown S142 Jane Smith

We could choose either {ID} or {First, Last} as the Primary Key. ID is more convenient as it is a single column and we know it will always be unique (what happens if another John Smith is added?)

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

NULLs and Primary Keys

• Missing

information can

be represented

using NULLs

• A NULL indicates

a missing or

unknown value

• More on this

later...

• Entity Integrity :

Primary Keys

cannot contain

NULL values

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Foreign Keys

• Foreign Keys are used to link data in

two relations. A set of attributes in the

first ( referencing ) relation is a Foreign

Key if its value always either

• Matches a Candidate Key value in the

second ( referenced ) relation, or

• Is wholly NULL

• This is called Referential Integrity

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Foreign Keys - Example

Department DID DName 13 Marketing 14 Accounts 15 Personnel

Employee EID EName DID 15 John Smith 13 16 Mary Brown 14 17 Mark Jones 13 18 Jane Smith NULL

{DID} is a Candidate Key for Department - Each entry has a unique value for DID

{DID} is a Foreign Key in Employee - each Employee’s DID value is either NULL, or matches an entry in the Department relation. This links each Employee to (at most) one Department

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

NULLIFY

• NULLIFY sets problem

values to NULL

  • If Marketing’s DID

changes then John

Smith’s and Mark Jones’

DIDs are set to NULL

  • If Accounts is deleted,

Mary Brown’s DID

becomes NULL

Department

DID DName 13 Marketing 14 Accounts 15 Personnel

Employee EID EName DID

15 John Smith 13 16 Mary Brown 14 17 Mark Jones 13 18 Jane Smith NULL

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

The Relational Algebra

• ‘Normal’ algebra

  • A set of operations

(+, -, ×, ÷, etc.)

  • Operators take

numbers as input

  • Operators return

numbers as results

  • Some operators have

restrictions - you can’t

divide by 0

• Relational algebra

  • A set of operations

(union, project, etc.)

  • Operators take

relations as input

  • Operators return

relations as results

  • Some operators have

restrictions

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

The Relational Algebra

• Combining operators

  • Since operators take

and return relations

they can be chained

together

  • This is just like in

normal algebra

(3 + 2) × 7

  • This is called closure

• Example operators

  • Select: Returns tuples

(rows) that satisfy a

given condition

  • Project: Returns the

attributes (columns)

specified

  • Union, intersection,

difference: the same

as the set operators

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Naming Conventions

• Naming conventions

  • A consistent naming

convention can help

to remind you of the

structure

  • Assign each table a

unique prefix, so a

student name may be

stuName, and a

module name

modName

• Naming keys

  • Having a unique

number as the

primary key can be

useful

  • If the table prefix is

abc, call this abcID

  • A foreign key to this

table is then also

called abcID

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

This Lecture in Exams

3 Amelie 17.99 2 2 2 Audry Tautou

2 Dracula 14.99 2 1 2 Gary Oldman

1 The Matrix14.99 1 1 1 Keanu Reeves

dvdID dvdTitle dvdPrice dvdID actID actIDactFirstactLast

DVD Stars Actors

The Relational Model G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

This Lecture in Exams

Give the two conditions that must be satisfied for a set of

attributes to be a candidate key of a relation (2 marks)

Given the data shown above, what are the candidate

key(s) of the tables DVD, Stars, and Actor? (3 marks)

Explain, with reference to the relations given, the term

entity integrity. (2 marks)

Explain, with reference to the relations given, the term

referential integrity. (2 marks)