Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Integrity Constraints
used to ensure accuracy and consistency of data
in a relational database. Data integrity is handled
in a relational database through the concept of
referential integrity.
something like 'be right' and consistent.
The constraints available in SQL are Foreign Key,
Primary Key, Not Null, Unique, Check.
Constraints can be defined in two ways:
The constraints can be specified immediately after the
column definition. This is called column-level
definition.
The constraints can be specified after all the columns
are defined. This is called table-level definition.
define
Not
Null
constraint_name]
CHECK
Domain Integrity
Definition of a valid set of values for an attribute.
They are easy to test for when data is
entered
data type,
lenght or size,
is null value allowed,
is the value unique or not for an attribute.
For example,
a domain of date is the set of all possible valid dates,
a domain of integer is all possible whole numbers,
a domain of day-of-week is Monday, Tuesday ... Sunday.
Feature of Relational DB
Bad design with following
Problems
Data Redundancy
Update Anomalies
Insertion Anomalies
Deletion Anomalies
Good design
Goal of relational schema design is
to
avoid
anomalies
and
redundancy.
Eliminate data dependency
What is an Anomaly?
Definition
Problems that can occur in poorly
planned, un-normalized databases
where all the data is stored in one
table (a flat-file database).
Types of anomalies:
Insert
Delete
Update
Insert Anomaly
occurs when certain attributes cannot be
inserted into the database without the
presence of other attributes.
Course _no
Tutor
Room
Room_size
En_limit
353
Smith
A532
45
40
351
Smith
C320
100
60
355
Clark
H940
400
300
456
Turner
H940
400
45
Delete Anomaly
Exists when certain attributes are lost
because of the deletion of other
attributes.
Course_no
Tutor
Room
Room_size
En_limit
353
Smith
A532
45
40
351
Smith
C320
100
60
355
Clark
H940
400
300
456
Turner
H940
400
45
Update Anomaly
Exists when one or more instances of
duplicated data is updated, but not all.
Course_no
Tutor
Room
Room_size
En_limit
353
Smith
A532
45
40
351
Smith
C320
100
60
355
Clark
H940
400
300
456
Turner
H940
400
45
Dependencies
Normalization is based on the concept of
dependencies between the attributes of a
table.
Types of dependencies:
Functional dependency
Full Functional dependency
Partial dependency
Transitive dependency
Multi-Valued dependency
Join dependency
Functional Dependency
Functional Dependency
Y is determinant & X is
determined
Functional Dependency
Functional Dependency
FFD
Rollno, Name, course_id, Course_name,
Grade
Grades FFD on Rollno and course_id.
Name and course_name Not FFD
Because only rollno and course_id are
needed to determine the values of Name
and course_name.
Partial Dependency
A relationship between attributes such that the value of
one attribute is dependent on or determined by the value
of another attribute which is part of the composite key.
Transitive Dependency
Exists when values of an attribute is dependent on the
value of another dependent attribute.
For example:
A
Departm
ent
Dept_id
1
CSE
Balraj Singh
IT
Kewal Krishan
ECE
R K Sharma
Transitive Dependency:
3
Dept_id
Dept_nam Cod_name
e
Dept_name
Cod_name
Multi-Valued Dependency
Attribute B has a MVD on attribute A, if for each value of
attribute A, there are more than one values of attribute B.
For example:
Name
Mobile_No
Perso
n
Name
Mobile_No
Preeti
9878793933
Sweety
9915656789
Akanksha
9813234343
Preeti
9912213343
Akanksha
9843432234
Supriti
9984348989
Join Dependency
A table T is subject to a Join dependency,
if T can always be reconstructed by
joining multiple tables each having a
subset of the attributes of T.
If one of the tables in the join has all the
attributes of the table T, then the join
dependency -> TRIVIAL.
It is used in the Fifth Normal Form ->
project join normal form.
Normalization
Normal form represents a good DB design.
Used to eliminate:
Anomalies
Inconsistencies
Types of NF:
First Normal Form
Second Normal Form
Third Normal Form
Boyce Codd Normal Form
Forth Normal Form
Fifth Normal Form
Normalization
Normal Forms
A relation is in a particular normal form if it satisfies
certain normalization properties.
There are several normal forms defined:
Normal Forms
1NF
A relational schema is in 1NF, if the values
in the domain of each attribute of the
relation are simple or atomic
Only one value is associated with
each attribute & the value is not a
set of values.
A DB schema is in 1NF, if all relation
schemas in the DBs are in 1NF.
1NF
Take the following table.
StudentID is the primary key.
Is it 1NF?
1NF
No. There are repeating groups
(subject, subjectcost, grade)
1NF
Create new rows so each cell
contains only one value
1NF
No the studentID no longer
uniquely identifies each row
1NF
So. We now have 1NF.
Stude
nt
Subjects
detail
Is it 2NF?
A non-1NF Relation
Two ways to convert a non-1NF relation to a 1NF relation:
1) Splitting Method - Divide the existing relation into two relations: nonrepeating attributes and repeating attributes.
2) Flattening Method - Create new tuples for the repeating data combined
with the data that does not repeat.
EmpPhone
233-9876
233-1231
233-1231
EmpDegrees
BTech
BA, BSc, PhD
BSc, MSc
Employee
EmpNum
123
333
679
EmpPhone
233-9876
233-1231
233-1231
EmpNum EmpDegree
333
BA
333
BSc
333
PhD
679
BSc
679
MSc
2NF
A relation is in 2NF if:
It is in 1NF &
All its non-primary attributes are FFD on
the primary key.
Multi-Valued Dependencies
A multi-valued dependency (MVD) occurs when two
independent, multi-valued attributes are present in the schema.
When these multi-valued attributes are flattened into a 1NF
relation, we must have a tuple for every combination of the
values in the two attributes.
It may seem strange why we would want to do this as it
obviously increases the number of tuples and redundancy.
The reason is that since the two attributes are independent it
does not make sense to store some combinations and not the
others because all combinations are equally valid. By leaving
out some combination, we are unintentionally favoring one
combination over the other which should not be the case.
Multi-Valued Dependencies
Example Employee may:
- work on many projects
- be in many departments
Lossless-join Dependency
The lossless-join dependency refers to the
fact that whenever we decompose relations
using normalization we can rejoin the relations
to produce the original relation such that no
spurious tuples are generated when relations
are natural joined.
K
N
A
TH
!
!
!
U
YO
T
S
E
B
E
H
T
L
AL
E
T
M
R
FO