Uploaded by Dhen Gin

DATA ORGANIZATION ARCHITECTURE

advertisement
Click to edit Master title style
DATA ORGANIZATION
ARCHITECTURE
• Data Models
• Normal Forms
• Referential Integrity
• Entity Integrity
• Integrity Constraints
1
Click to edit Master title style
Data Models
Its types and functions
2 2
Click to edit Master title style
What is a Data Model?
3 3
Click to
What
is aedit
Data
Master
Model?
title style
• Data models are visual representations of an
enterprise’s data elements and the connections
between them. By helping to define and structure
data in the context of relevant business
processes, models support the development of
effective information systems. They enable
business and technical resources to
collaboratively decide how data will be stored,
accessed, shared, updated and leveraged across
an organization.
4 4
Click to edit Master title style
Types of Data Model
5 5
Click to edit Model
Hierarchical
Master title style
6 6
Click to edit Model
Hierarchical
Master title style
• As the name suggests, consist of databases where
records are stored in a tree-like or parent-child
structure. One parent node can have one or more
child nodes. However, a child node can have only a
single parent node.
To be a hierarchical database, the information should have this parentchild relationship and should not appear at multiple places in the
database.
7 7
Click to edit
Example
of Hierarchical
Master title Model
style
8 8
Schema
of Hierarchical
Click toExample
edit Master
title styleModel
9 9
Click to edit
Network
Model
Master title style
• Network Model in DBMS is a hierarchical model
that is used to represent the many-to-many
relationship among the database constraints. The
network model in DBMS is a hierarchal structure
but is different from the hierarchal database model
as there can be numerous parents of a member.
1010
Click to edit
Example
of Network
Master title
Model
style
1111
Example
Network
Model
Click to of
edit
Master
title style
1212
Click to edit Master title style
Lab Activity 1
Choose your own root entity (parent) and make one (1)
Hierarchical Model and one (1) Network Model along with
their respective attributes.
13
Click to edit
Relational
Model
Master title style
• In the concept of a relational
database management system,
data is organized into tables.
Tables are similar to folders in a
file system, where each table
stores a collection of
information. Tables are further
divided into columns and rows.
Columns represent the
attributes of an entity, while
rows represent the entities
themselves.
1414
Relational Model
• The relational
in DBMS
several features that make it well suited for
Click
to editmodel
Master
titlehas
style
storing data: atomicity, consistency, isolation, durability, data consistency,
data working together, data flexibility, and lookup relationships.
Features
Description
Atomicity
keeps data accurate by ensuring that all changes to the data are either made
completely or not made at all and that there are no partial changes to the data.
Consistency
ensures that the state of the database remains consistent throughout the
transaction.
Isolation
ensures that changes made by one transaction are not visible to other
transactions until the changes are committed.
Durability
ensures that once changes are committed, they will remain even if there is a
system failure.
Data consistency
means that all data in the database is consistent with itself.
Data flexibility
means that new data can be added without having to restructure the entire
database.
1515
Click to edit Master title style
Types of Relational Model
1616
Click to edit
Relational
Model:
Master
One-to-Many
title style Model
In this type of relationship, one
entity is related to multiple other
entities. The key feature of the
one-to-many model is that the
relationship between two entities
is not reciprocal.
1717
Click to edit
Relational
Model:
Master
One-to-One
title style Model
The one-to-one model of
relational models states that
there is a direct and unique
relationship between two specific
objects. In other words, each
object can only be related to one
other object and vice versa.
1818
Click to edit
Relational
Model:
Master
Many-to-Many
title style Model
• In a many-to-many model, each record in the primary table can be linked to
multiple records in the foreign table and vice versa.
• To create a many-to-many relationship, we need to create a third table, called a
junction table, that contains the primary keys of the other tables. The junction
table establishes the relationships between the rows in the two other tables.
1919
Click to edit
Relational
Model:
Master
Many-to-Many
title style Model
Why NO many-to-many
relationship?
• The problem with many-to-many
relationships is that it can cause
duplications in the returned datasets,
which can result in incorrect results
and might consume excessive
computing resources
2020
Click to edit Master title style
Entity Relationships
2121
Click to edit Master title style
Entity
Relationships and
Cardinalities
2222
Click to edit Master title style
Lab Activity 2
(Part 1)
Group yourselves with only three (3) members then do the ff:
• Use the relational model
• Relate the given entities into one concept.
• Label the relationship between the entities
23
ClickActivity
Lab
to edit 2Master title style
• Relate the given
entities into one
concept.
• If done, find a
group that has
the same answer
2424
Click to edit Master title style
Lab Activity 2
(Part 2)
Group yourselves with only four (4) members then do the ff:
• Use the relational model
• Relate the given entities into one concept.
• Label the relationship between the entities
25
ClickActivity
Lab
to edit 2Master title style
• Relate the given
entities into one
concept.
• If done, find a
group that has
the same answer
2626
Click to edit Master title style
Before we proceed at introducing
the rest of data models...
2727
Click to edit Master title style
Normalization /
Normal forms
•
What is it?
•
1NF
•
2NF
•
3NF
•
BCNF
•
4NF
•
5NF
2828
Click to edit Master title style
What is Normalization in
Database Management?
2929
Normalization
Click to edit Master title style
Normalization in DBMS is a technique using which you can
organize the data in the database tables so that:
• There is less repetition of data
• A large set of data is structured into a bunch of smaller tables
• and the tables have a proper relationship between them.
DBMS Normalization is a systematic approach to decompose (break down)
tables to eliminate data redundancy(repetition) and undesirable characteristics
like Insertion anomaly in DBMS, Update anomaly in DBMS, and Delete anomaly
in DBMS.
It is a multi-step process that puts data into tabular form, removes duplicate data,
and set up the relationship between tables.
3030
Example
Click to edit Master title style
rollno
name
branch
hod
office_tel
401
Inday
CSE
Mr. X
53337
402
Dodong
CSE
Mr. X
53337
403
Dingdong
CSE
Mr. X
53337
404
Tunday
CSE
Mr. X
53337
ANOMALY DETECTED
3131
rollno
name
branch
hod
office_tel
Inday title styleCSE
Click 401
to edit Master
Mr. X
53337
402
Dodong
CSE
Mr. X
53337
403
Dingdong
CSE
Mr. X
53337
404
Tunday
CSE
Mr. X
53337
In the table above, we have data for four Computer Sci. students
As we can see, data for the fields branch, hod(Head of Department), and office_tel are
repeated for the students who are in the same branch in the college, this is Data Redundancy.
These are the anomalies detected in this table:
• INSERTION ANOMALY
• UPDATION ANOMALY
• DELETION ANOMALY
3232
rollno
name
branch
hod
office_tel
Inday title styleCSE
Click 401
to edit Master
Mr. X
53337
402
Dodong
CSE
Mr. X
53337
403
Dingdong
CSE
Mr. X
53337
404
Tunday
CSE
Mr. X
53337
INSERTION ANOMALY
• If we have to insert data for 100 students of the same branch, then the branch information will
be repeated for all those 100 students.
• Suppose for a new admission, until and unless a student opts for a branch, data of the student
cannot be inserted, or else we will have to set the branch information as NULL.
3333
rollno
name
branch
hod
office_tel
Inday title styleCSE
Click 401
to edit Master
Mr. X
53337
402
Dodong
CSE
Mr. X
53337
403
Dingdong
CSE
Mr. X
53337
404
Tunday
CSE
Mr. X
53337
UPDATION ANOMALY
• What if Mr. X leaves the college? or Mr. X is no longer the HOD of the computer science
department? In that case, all the student records will have to be updated, and if by mistake we
miss any record, it will lead to data inconsistency.
• If there’s a new admission, we will have to set the branch information in each data WHICH
WOULD TAKE A LOT OF TIME AND EFFORT.
This is an Updation anomaly because you need to update all the records in your table just
because one piece of information got changed.
3434
rollno
name
branch
hod
office_tel
Inday title styleCSE
Click 401
to edit Master
Mr. X
53337
402
Dodong
CSE
Mr. X
53337
403
Dingdong
CSE
Mr. X
53337
404
Tunday
CSE
Mr. X
53337
DELETION ANOMALY
• In our Student table, two different pieces of information are kept together, the Student
information and the Branch information.
• So if only a single student is enrolled in a branch, and that student leaves the college, or for
some reason, the entry for the student is deleted, we will lose the branch information too.
3535
Example
Click to edit Master title style
rollno
name
branch
hod
office_tel
401
Inday
CSE
Mr. X
53337
402
Dodong
CSE
Mr. X
53337
403
Dingdong
CSE
Mr. X
53337
404
Tunday
CSE
Mr. X
53337
We can fix this through Normalization
3636
Click to edit Master title style
FIRST NORMAL FORM
1NF
3737
Click
First to
Normal
edit Master
Form (1NF)
title style
For a table to be in the First Normal Form, it should follow the
following 4 rules:
• It should only have single(atomic) valued attributes/columns.
• Values stored in a column should be of the same domain.
• All the columns in a table should have unique names.
• And the order in which data is stored should not matter.
3838
1NF
ClickExample
to edit Master title style
If we have an Employee table in which we store the employee information along with the employee skillset,
the table will look like this:
• All the columns have different names.
• All the columns hold values of the same type like emp_name has all the names, emp_mobile has all the
contact numbers, etc.
• But the emp_skills column holds multiple comma-separated values, while as per the First Normal form,
each column should have a single value.
3939
Create Separate tables for Employee and Employee Skills
Click
to edit Master title style
Employee Table
Employee_Skill Table
4040
Or...You can also Add Multiple rows for Multiple skills
Click to edit Master title style
You can also simply add multiple rows to add multiple skills. This will lead to repetition of the data,
but that can be handled as you further Normalize
4141
Click to edit Master title style
SECOND NORMAL FORM
2NF
4242
Click
Second
to edit
Normal
Master
Form
title
(2NF)
style
For a table to be in the Second Normal Form,
• It should be in the First Normal form.
• And, it should not have Partial Dependency.
4343
Click to edit Master title style
What is a Partial Dependency?
4444
Click
Partial
toDependency
edit Master title style
When a table has a primary key that is made up of two or more columns, then all
the columns(not included in the primary key) in that table should depend on the
entire primary key and not on a part of it. If any column(which is not in the primary
key) depends on a part of the primary key then we say we have Partial
dependency in the table.
Confused? Let's take
an example.
4545
Student table
Click to edit Master title style
Subject table
Score table
Now in the above table, the primary key is student_id + subject_id, because
both these information are required to select any row of data.
46
46
Student table
Click to edit Master title style
Subject table
Score table
But in the Score table, we have a column teacher_name, which depends on the
subject information or just the subject_id, so we should not keep that
4747
information in the Score table.
Student table
Click to edit Master title style
Subject table
Score table
The column teacher_name should be in the Subjects table. And then the entire
system will be Normalized as per the Second Normal Form.
48
48
Click to edit Master title style
THIRD NORMAL FORM
3NF
4949
Click
ThirdtoNormal
edit Master
Form title
(3NF)style
A table is said to be in the Third Normal Form when,
• It satisfies the First Normal Form and the Second Normal
form.
• And, it doesn't have Transitive Dependency.
5050
Click to edit Master title style
What is a Transitive
Dependency?
5151
Transitive
Dependency
Click
to edit
Master title style
“A transitive dependency occurs when one non-prime attribute is
dependent on another non-prime attribute.”
In a table we have some column that acts as the primary key and other columns
depends on this column. But what if a column that is not the primary key
depends on another column that is also not a primary key or part of it? Then we
have Transitive dependency in our table.
5252
Transitive Dependency
Click
edit Master
style
Let's taketo
an example.
We had thetitle
Score table
in the Second Normal Form above. If we have to store some
extra information in it, like,
• exam_type
• total_marks
To store the type of exam and the total marks in the exam so that we can later calculate the percentage of
marks scored by each student.
The Score table will look like this,
5353
Transitive Dependency
Click to edit Master title style
In the table above, the column exam_type depends on both student_id and subject_id, because,
➢ A student can be in the CSE branch or the Mechanical branch, and based on that they may
have different exam types for different subjects.
➢ The CSE students may have both Practical and Theory for Compiler Design, whereas
Mechanical branch students may only have Theory exams for Compiler Design.
But the column total_marks just depends on the exam_type column. And the exam_type
column is not a part of the primary key. Because the primary key is student_id + subject_id,
hence we have a Transitive dependency here.
5454
Transitive Dependency
Click to edit Master title style
You can create a separate table for ExamType and use it in the Score table.
New ExamType table:
5555
Transitive Dependency
Click to edit Master title style
exam_type_id
2
2
1
You can create a separate table for ExamType and use it in the Score table.
New ExamType table:
5656
Click to edit Master title style
Here are another
example of 3NF
5757
EXAMPLE
OF
3NF
Click to edit Master title style
5858
EXAMPLE
OF
3NF
Click to edit Master title style
• The Capacity column depends on the Ground column.
• This violates the 3NF
5959
EXAMPLE
OF
3NF
Click to edit Master title style
Match Table
Capacity Table
6060
EXAMPLE
OF
3NF
Click to edit Master title style
6161
EXAMPLE
OF
3NF
Click to edit Master title style
• The Salary column depends on the Designation column.
• This violates the 3NF
6262
EXAMPLE
OF
3NF
Click to edit Master title style
Employee Table
Salary Table
6363
Click to edit Master title style
BOYCE-CODD NORMAL
FORM
BCNF
6464
Click
Boyce-Codd
to edit Master
Normaltitle
Form
style
(BCNF)
• Boyce and Codd Normal Form is a higher version of the
Third Normal Form.
• This form deals with a certain type of anomaly that is not
handled by 3NF.
• A 3NF table that does not have multiple overlapping
candidate keys is said to be in BCNF.
6565
Click to edit Master title style
Let’s have a quick recap
6666
Click to edit Master title style
Prime Attribute
Non-prime
Attribute
Functional Dependency
And that’s a good thing ☺
6767
Click to edit Master title style
Part of Prime
Attribute
Non-prime
Attribute
Partial Dependency
I see what you did there, But it’s still not
acceptable
6868
Click to edit Master title style
Non-prime
Attribute
Non-prime
Attribute
Transitive Dependency
OH HELL NO!
6969
Click to edit Master title style
Non-prime
Attribute
Prime Attribute
THIS IS NOT IN BCNF
How is this possible?
7070
Click to edit Master title style
Example of BCNF
7171
Click to edit
Example
of BCNF
Master title style
Class table
id
student_id
subject
professor
1
10001
CC101
Mr. X
2
10001
CC102
Mr. Y
3
10002
CC103
Mr. X
4
10003
CC104
Mr. Z
7272
Click to edit
Example
of BCNF
Master title style
Class table
id
student_id
subject
professor
1
10001
CC101
Mr. X
2
10001
CC102
Mr. Y
3
10002
CC103
Mr. X
4
10003
CC104
Mr. Z
Non-Prime Attribute
Prime Attributes
7373
Click to edit
Example
of BCNF
Master title style
Class table
id
student_id
subject
professor
1
10001
CC101
Mr. X
2
10001
CC102
Mr. Y
3
10002
CC103
Mr. X
4
10003
CC104
Mr. Z
The subject column (Prime Attribute) is dependent to Professor (Non-Attribute)
7474
Click to edit Master title style
How to make the table
satisfy BCNF?
7575
Example of BCNF
Click toidedit Master student_id
title style
subject
professor
1
10001
CC101
Mr. X
2
10001
CC102
Mr. Y
3
10002
CC103
Mr. X
4
10003
CC104
Mr. Z
Class table
Professor Subject table
id
Student_id
Prof_sub_id
Prof_sub_id
Professor
Subject
1
10001
1
1
Mr. X
CC101
2
10001
3
2
Mr. X
CC103
3
10002
2
3
Mr. Y
CC102
4
10003
4
4
Mr. Z
CC104
7676
Click to edit Master title style
Another Example
7777
Click to edit
Example
of BCNF
Master title style
Branch_Number
Customer_ID
Loan_ID
Loan_Amount
1
100512
3141
150000
1
122099
3444
25000
2
099344
6501
30000
• One loan could have multiple customers, our primary key would have to be
Loan_ID + Customer_ID. Doing that would give us data duplication of the
Branch Number and the Loan Amount.
• Loan_ID also determines the Loan Amount, but it isn’t a primary key itself, so
this table is not in BCNF
7878
Click to edit
Example
of BCNF
Master title style
Branch_Number
Customer_ID
Loan_ID
Loan_Amount
1
100512
3141
150000
1
122099
3444
25000
2
099344
6501
30000
Branch_Number
Loan_ID
Loan_Amount
Customer_ID
Loan_ID
1
3141
150000
100512
3141
1
3444
25000
122099
3444
2
6501
30000
099344
6501
7979
Click to edit Master title style
Before we proceed with the topic,
let’s have an activity ☺
8080
Click to edit Master title style
Lab Activity 3
(Individual)
In order to pass this activity, students must do the ff:
• Use MS Word for this activity
• Normalize the given table into a 3NF/BCNF
• Submit your work in Google Classroom
Note: Before you normalize the given table into 3NF or BCNF don’t forget
that you’ll still need to follow the rules of the previous Normal Forms (1NF
and 2NF) in order to proceed with 3NF or BCNF
81
P_id
P_name
Diagnose
Hospital
Wing
Click to edit Master title style
10001
John Doe
Diabetes, UTI
Matthew Yap
Cancer
Tangub
Community
Hospital
South Wing
Room No.
Doctor
Total_Bill
Balance
SW – 301
Dr. Mario
15,000,000
10,000,000
WW – 301
Dr. Luigi
West Wing
10002
Will Smithy
HIV
Ozamiz
Community
Hospital
N/A
201
Dr. Snake
500,000
135,000
10003
Michael
Jackstone
Tuberculosis
Iligan
Community
Hospital
North Wing
NW – 101
Dr. Hee Hee
120,000
0
10004
Elvis Pretzels
Tuberculosis,
Diabetes
Tangub
Community
Hospital
East Wing
EW – 100
Dr. Hansel
1,000,000
1,000
10005
Frank Sinister
Cancer
Tangub
Community
Hospital
South Wing
SW – 302
Dr. Peter
Parker
9,000,000
9,000,000
10006
Ryan Donald
Cancer
Clarin
Community
Hospital
N/A
101
Dr. Wolverine
9,000,000
0
10007
John Xina
UTI
Iligan
Community
Hospital
South Wing
SW – 222
Dr. Big Show
90,000
80,000
10008
Donald Flute
Diabetes
Ozamiz
Community
Hospital
N/A
111
Dr. Obama
100,000
10,000
8282
Click to edit Master title style
That’s all for today
83
Download