0% found this document useful (0 votes)
6 views

Dbs 3

Uploaded by

tiktak7719
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Dbs 3

Uploaded by

tiktak7719
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

The Relational Model

Database Systems Lecture 3


Natasha Alechina
In This Lecture
• Relational data integrity
• For more information
• Connolly and Begg chapter 3
• E.F. Codd’s paper
`A Relational Model of Data for Large Shared
Data Banks’ – a link from the module web
page, ~nza/G51DBS.

The Relational Model


The Relational Model
• Introduced in E.F. • Concerned with 3
Codd’s 1970 paper main things
“A Relational Model • Data structure (how
of Data for Large data is represented)
Shared Databanks” • Data integrity (what
data is allowed)
• The foundation for
• Data manipulation
most (but not all) (what you can do with
current database the data)
systems

The Relational Model


Relational Data Structure
• Data is stored in Attributes
relations (tables)
• Each relation has a
scheme (heading) Name Age Scheme

• The scheme defines John


Mary
23
20 Tuples
the relation’s Mark 18
attributes (columns) Jane 21

• Data takes the form


of tuples (rows)

The Relational Model


New thing:scheme (and
attributes)
Before… After

Attributes

1 2 Just numbers Name Age Scheme


of columns
John 23 John 23
Mary 20 Tuples Mary 20 Tuples
Mark 18 Mark 18
Jane 21 Jane 21

The Relational Model


Unnamed and named tuples
A tuple is A tuple is
<John, 23> {(Name,John), (Age,23)}
Attributes

1 2 Just numbers Name Age Scheme


of columns
John 23 John 23
Mary 20 Tuples Mary 20 Tuples
Mark 18 Mark 18
Jane 21 Jane 21

The Relational Model


Not a big difference!
• There is no fundamental difference between
named and unnamed perspectives on relations

• We could have written tuples <a,b,c> as sets


of pairs {(1,a), (2,b),(3,c)}, only we know
anyway in which order 1,2,3 go, so we can
skip the numbers.

• Written as sets of pairs (partial functions),


tuples can be written in any order, e.g.
{(3,c),(2,b),(1,a)}.
The Relational Model
Relational Data Structure
• More formally - Name Age

• A scheme is a set John 23


Mary 20
of attributes Mark 18
• A tuple assigns a Jane 21
value to each
{ { (Name, John), (Age, 23) },
attribute in its { (Name, Mary), (Age, 20) },
scheme { (Name, Mark), (Age, 18) },
{ (Name, Jane), (Age, 21) } }
• A relation is a set
of tuples with the
same scheme
The Relational Model
Relations
Scheme is {ID, Name, Salary, Department}

Attributes are ID, Name, Salary, and Department Degree is 4

ID Name Salary Department


M139 John Smith 18,000 Marketing Tuples, e.g.
M140 Mary Jones 22,000 Marketing { (ID, A368),
A368 Jane Brown 22,000 Accounts (Name, Jane Brown),
P222 Mark Brown 24,000 Personnel (Salary, 22,000),
A367 David Jones 20,000 Accounts (Department, Accounts) }

Cardinality is 5

The Relational Model


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


Attributes and Domains
• A domain is given for • Examples
each attribute • An ‘age’ might have
• The domain lists the to come from the set
of integers between 0
possible values for and 150
that attribute • A ‘department’ might
• Each tuple assigns a come from a given list
value to each of strings
attribute from its • A ‘notes’ field might
allow any string at all
domain

The Relational Model


Candidate Keys
• A set of attributes in ID First Last
a relation is called a S139 John Smith
candidate key if, and S140 Mary Jones
only if, S141 John Brown
S142 Jane Smith
• Every tuple has a
unique value for the Candidate key: {ID}; {First,Last}
set of attributes looks plausible but we may get
(uniqueness) people with the same name
• No proper subset of {ID, First}, {ID, Last} and {ID,
the set has the First, Last} satisfy uniqueness,
uniqueness property but are not minimal
(minimality) {First} and {Last} do not give
The Relational Model
a unique identifier for each row
Choosing Candidate Keys
• Important: don’t look just on the data in
the table to determine what is a candidate
key

• The table may contain just one tuple, so


anything would do!

• Use knowledge of the real world – what is


going to stay unique!

The Relational Model


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

The Relational Model


NULLs and Primary Keys
• Missing • Entity Integrity:
information can Primary Keys
be represented cannot contain
using NULLs NULL values
• A NULL indicates • Why: if primary
a missing or key has NULLs
unknown value then will not
• More on this uniquely identify
later... the tuple/entity
The Relational Model
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


Foreign Keys
• a Foreign Key
• matches a Candidate Key value in the
second (referenced) relation, or
• is wholly NULL
• This is called Referential Integrity
• Why: either we know precisely what
(which entity) we refer to, or we don’t
refer to any entity.
The Relational Model
Foreign Keys - Example
Department Employee
DID DName EID EName DID
13 Marketing 15 John Smith 13
14 Accounts 16 Mary Brown 14
15 Personnel 17 Mark Jones 13
18 Jane Smith NULL

{DID} is a Candidate Key {DID} is a Foreign Key in Employee -


for Department - Each each Employee’s DID value is either
entry has a unique value NULL, or matches an entry in the
for DID Department relation. This links each
Employee to (at most) one Department

The Relational Model


Foreign Keys - Example

Employee
{ID} is a Candidate Key for
ID Name Manager Employee, and {Manager} is
E1496 John Smith E1499 a Foreign Key, which refers
E1497 Mary Brown E1498 to the same relation - every
E1498 Mark Jones E1499 tuple’s Manager value is either
E1499 Jane Smith NULL NULL or matches an ID value

The Relational Model


Referential Integrity
• When relations are • There are a number
updated, referential of options:
integrity can be • RESTRICT - stop the
violated user from doing it
• This usually occurs • CASCADE - let the
changes flow on
when a referenced
• NULLIFY - make
tuple is updated or values NULL
deleted

The Relational Model


Referential Integrity -
Example
• What happens if Department
• Marketing’s DID is DID DName
changed to 16 in 13 Marketing
Department? 14 Accounts
• The entry for 15 Personnel
Accounts is deleted
from Department? Employee
EID EName DID
15 John Smith 13
16 Mary Brown 14
17 Mark Jones 13
18 Jane Smith NULL

The Relational Model


RESTRICT
• RESTRICT stops any Department
action that violates DID DName
integrity 13 Marketing
14 Accounts
• You cannot update or
15 Personnel
delete Marketing or
Accounts
Employee
• You can change
Personnel as it is not EID EName DID
referenced 15 John Smith 13
16 Mary Brown 14
17 Mark Jones 13
18 Jane Smith NULL

The Relational Model


CASCADE
• CASCADE allows the Department
changes made to flow DID DName
through 13 16 Marketing
14 Accounts
• If Marketing’s DID is
15 Personnel
changed to 16 in
Department, then the
Employee
DIDs for John Smith and
Mark Jones also change EID EName DID
• If Accounts is deleted 15 John Smith 13 16
then so is Mary Brown 16 Mary Brown 14
17 Mark Jones 13 16
18 Jane Smith NULL

The Relational Model


NULLIFY
• NULLIFY sets problem Department
values to NULL DID DName
• If Marketing’s DID 13 16 Marketing
changes then John 14 Accounts
Smith’s and Mark Jones’ 15 Personnel
DIDs are set to NULL
Employee
• If Accounts is deleted,
Mary Brown’s DID EID EName DID
becomes NULL 15 John Smith 13 NULL
16 Mary Brown 14 NULL
17 Mark Jones 13 NULL
18 Jane Smith NULL

The Relational Model


Naming Conventions
• Naming conventions • Naming keys
• A consistent naming • Having a unique
convention can help number as the
to remind you of the primary key can be
structure useful
• Assign each table a • If the table prefix is
unique prefix, so a abc, call this abcID
student name may be • A foreign key to this
stuName, and a table is then also
module name called abcID
modName

The Relational Model


Example

Student Enrolment Module


stuID stuName stuID modID modID modName

These entries are These entries are These entries are


clearly related to clearly related to clearly related to
the Student table tables other than the Module table
Enrolment

The Relational Model


Next Lecture
• Entity/Relationship models
• Entities and Attributes
• Relationships and Cardinality Ratios
• E/R Diagrams
• For more information
• Connolly and Begg chapter 11.
• Ullman and Widom chapter 2.

The Relational Model

You might also like