Chapter 2 - Relational Data Model
Chapter 2 - Relational Data Model
Degree is 7, Cardinality is 5
More on Domain..(data type?)
Each tuple assigns a value to each attribute from its domain
For example, the domain of Cust-name is of character strings of max
length 25(varchar25), an age from a domain of integers between 0 and
Base Relation
A named relation
corresponding to an entity
in conceptual schema,
whose tuples are physically
stored in the database
Not a derived relation
Has physical existence
While it can manipulate the
conceptual or physical
relations stored in the data,
it does not provide security
View
The dynamic result of one
or more relational
operations operating on
base relations to produce
another relation
A virtual relation that does
not necessarily actally exist
in the database but is
produced upon request, at
time of request
(virtual existence)
As views are dynamic,
changes made to base
relations that affect view
attributes are immediately
reflected in the view
(if base relations update
values of
attributes/attributes in view
will also be affected)
Contents of a view are
defined as a query on one
or more base relations
Modification through a
view(e.g.
insert,update,delete
operations not permitted!)
Derived
relation!
Relation
Table
File
Alternate Terminology
Tuple
Row
Record
Attribute
Column
Field
Properties of Relations
Relation name is DISTINCT from all other relation names in relational
schemas
Each cell of relation contains exactly ONE atomic value
Each attribute as a DISTINCT name
Keys
Candida
te Key
Description
Minimal set of
attributes that can
uniquely identify each
tuple in a relation
Is a subset of the
super key
A relation may have
one or more candidate
keys
Candidate keys can
Conditions/Criteria
1. Must contain
unique values
2. Must not contain
null values since
a null value is not
guaranteed to be
unique & we would
not be able to
identify each _
uniquely
Example
StudentID, NRIC,
firstName +
lastName
(combined)
consist of a single
attribute or multiple
attributes(together)
Multiple attribute keys
are known as
composite key
Primary
Key
Alternat
e Key
Foreign
Key
3. Must contain
minimum no. of
fields(attributes) to
ensure
uniqueness(as
above)
NRIC,StudentId
firstName +
lastName
(combined)
Student(R1)
Sch_code :
SBM,SIT(FK)
Student(R2)
sch : SIT,SBM(PK)
Nulls
Purpose : a way to deal with incomplete/exceptional data.
Represents a value for an attribute that is currently unknown or is
not applicable
for this tuple OR represent an unknown piece of data
Relational Integrity
Integrity Rules
Can be specified on a relational database schema
Ensure accuracy & integrity of the data in the database
(Applied to data items to make sure that the data is valid &
consistent)
2 Principal Rules
1. Entity Integrity
Primary key in a base relation must not contain any nulls
Why? A primary key is a minimal identifier that is used to identify
tuples uniquely.
Hence, no subset of the primary key is sufficient to provide unique
identification of
tuples.
Example :
Description - The image cncpt055.gif shows Table DEPT, which has three columns: DEPTNO,
DNAME, and LOC.The DEPTNO column has a Primary Key--no row may duplicate a value in
the key and no null values are allowed.This illustration shows that two rows cannot be
inserted into Table DEPT because one duplicates an existing value in the primary key, while
the other contains a null value for the primary key
http://docs.oracle.com/cd/B19306_01/server.102/b14220/data_int.htm#i3771
2. Referential Integrity
If foreign key exists in a relation, either foreign key value must
match a candidate key
value of some tuple in its home relation or foreign key must be
wholly null
Purpose? Ensures that relationships between tuples in related
relations are
consistent and valid AND ensures that related tuples are
If AGENT_CODE
not accidentally deleted/
in Customer
changed?
Table is not
Example #1 Not violated :
found in Agent
Table, we would
have a problem
in identifying
the agent of this
customer. There
is a data
integrity
Example #2 Violated :
In the example scenario shown in Figure4.2, the record for customer OBrain (pink
shaded) in the CUSTOMER table (dependant table) is disturbing the referential integrity
of the database because there exist no reference for the records AGENT_CODE (504) in
the related table, AGENT (primary table)
Example #3 Violated :
In Figure 21-7, the referential integrity constraint ensures that every
value in the mgr column of the emp table corresponds to a value that
currently exists in theempno column of the same table, but not
necessarily in the same row, because every manager must also be an
employee. This integrity constraint eliminates the possibility of
erroneous employee numbers in the mgr column.
You cannot delete a parent record if any existing child record is there. If you
have to first delete the child record before deleting the parent record. In the
above example you cannot delete row of employee no. 101 since its child
exist in the ATTENDANCE table. However, you can delete the row of employee
no. 103 since no child record exist for this employee in ATTENDANCE table. If
you define the FOREIGN KEY with ON DELETE CASCADE option then you can
delete the parent record and if any child record exist it will be automatically
deleted.
Enterprise Constraints
Additional rules that are specified by the users or database
administrator that the
data must satisfy
E.g. business rules no more than 25 students to be assigned to a
mentor group
Relational Algebra
Relational algebra operations work on one or more relations to
define another
relation without changing the original relations
Both operands and results are relations
Uses :
- Define a scope of retrieval
- Define a scope for update
- Define a view
- Define access rights
- Define integrity constraints
Operations :
1. SELECT operation(horizontal!)
Notation : sigma
Select a subset of the tuples in a relation that satisfy a selection
condition
Note: SELECT operation in relational algebra has nothing to do with
SQL keyword
select. Selection in relational algebra returns those tuples in
a relation that
fulfill a condition!
Examples
2. PROJECT operation(vertical!)
Used to select certain attributes from the relation and discards the
other attributes
(Delete unwanted columns from relation)
Examples :
3. UNION operation(R
S)
The result is a relation that includes all tuples that are either in
relation R or in
relation S or in both. Duplicate tuples are elimintated.
Relation with tuples from R and S with duplicates removed.
Example :
R
R
S
First
First
Bill
Smith
Sally
Green
Bill
Sally
Mary
Keen
Tony
Jones
Mary
Tony
(
Dept = 'CS')
(Rank =
LastLast
Smith
Age
22
22
Green
28
28
Keen
23
23
Jones
32
32
Age
Show the
Gump
36names of all employees working in the CS
department:
27
name (
Results:
DeMarco
Dept = 'CS'
(EMP) )
(EMP) )
Name
Result:
Name
Green
Smith
Assistant
First
Smith
Rank
S
Last
Associate
Age
Forrest
Gump
36
Sally
Green
28
DonJuan
DeMarco
27
4. INTERSECTION operation(R
S)
The result is a relation that includes all tuples in both relation R & S
(Relation with tuples that appear in both R & S)
Example :
R
S
First
Sally
Last
Green
Age
28
5. DIFFERENCE operation(R S)
First
Last
Age
Bill
Smith
22
Mary
Keen
23
Tony
Jones
32
The result is a relation that includes all tuples that are in relation R
but not in
relation S
Example :
6. CARTESIAN PRODUCT(X)
The operation is used to combine tuples from two relations so that
related tuples can
be identified
My definition repeat the second relation?
Employees
Employee
Smith
Projects
Project
A
Pritchett
Pritchett
Code
Name
A
Venus
B
Mars
A
B
Employees X Projects
Employee
Smith
Smith
Pritchett
Pritchett
Pritchett
Pritchett
Project
A
A
A
A
B
B
Code
A
B
A
B
A
B
Name
Venus
Mars
Venus
Mars
Venus
Mars
7. JOIN operation(
)
This operation is used to combine related tuples from two relations
into a single
tuple
Types of joins:
Inner Join Combines tuples from two relations using comparison
operators
in a condition
- Condition can be = <, >, >= , <= , /=
- Equijoin(when the join condition is = ), Natural
join(operator is *)
E.g. Select * from Employee emp INNER JOIN Area
area on area.EmpId =
emp.EmpId
- Natural join is same as equijoin but the difference is
that in natural join,
the common attribute only appear once
- E.g. Select * from Employee emp NATURAL JOIN Area
area on area.
EmpId = emp.Id;
Example at : http://javacodeimpl.blogspot.sg/2013/08/difference-between-equi-joinnatural.html#.U24ZN_mSySo
8. Outer Join
Combine tuples from two relations and keep all the tuples in the
result even if
NAME1
TOM
DICK
HARRY
CHILD
EMP |X| CHILD
A
B
C
EMP#
NAME
1
TOM
1
TOM
2
DICK
3
HARRY
DAD
1
1
2
CHILD
A
B
C
NULL
DAD
1
1
2
NULL\