Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / SQL
Tip/Trick

Understanding Normal Forms of Relationship for database

Rate me:
Please Sign up or sign in to vote.
4.76/5 (7 votes)
15 Apr 2014CPOL4 min read 20K   133   16   7
How to create databese with forst 3 normal forms of relationship

Download EducationModel.edmx.zip

Introduction

For creating of this articles was used some materials from books and separate articles that published in Internet, also some changes for this materials was added and some parts was added (please see list in the end of articles).

In this article i want to describe how I try to create databese with first 3 normal forms (NF) of reelationship. I prepare some step-by-step explanation what was done.

Background

Currently I want better understand main principles of creating and managing databses. As first step I want to create good database, that allow me to prepare adequate UI and code for managing it.

What was used:

  • Microsoft SQL;
  • .Net technologies;
  • Microsoft SQL management Studion (2012) or Visual studio 2012

Step-by-Step: explanation how to

First of all I read some information about SQL language (Itzik Ben-Gan - Microsoft SQL Server 2008: T-SQL Fundamentals) - it's help to better understand what goin on when you use some command and how it's work. After some trying without additional learning about NF i got database, but after some usage of it with UI I got some problems, as result i start to find some rules for creating databases. And I found even more - NF relationships rules.

Ther are 6 NF for databese (or even more). But even usage of first 3 of them allow you to create really good databse. Lets look a little bit detailed - how I create databse with first 3 NF.

1NF - this is normal relationship, mean all relation already in 1NF. Main properties of such relationship (according to this article) are:

  • Table must be two-dimensional, with rows and columns.
  • Each row contains data that pertains to one thing or one portion of a thing.
  • Each column contains data for a single attribute of the thing being described.
  • Each cell (intersection of row and column) of the table must be single-valued.
  • All entries in a column must be of the same kind.
  • Each column must have a unique name.
  • No two rows may be identical.
  • The order of the columns and of the rows does not matter.

As example let assume that we need to create database for some school. As result we can get next domain model can be like described below:

  1. All teachers and student have some school
  2. All schools have some grade
  3. All school have some specialized classes with some name and number
  4. All students have some class
  5. All studenst, teachers and schools have some address
  6. Students have some dedicated teacher
  7. Teacher can have few classes
  8. Student and teacher can have some project
  9. Each project have name, speciallity and dueDate

During first step of creating database you can modeling your database as 1 table in 1 relationship, and as result you get 1 table where all information was combined: info about school, teacher, student. Also as result you can got situation, when same information can be duplicated - so additional problems and resources will be needed during updating info about teacher, that have 10 students - you need 10 times so same work - result of such activities - additional code, time required. So data that stored in such way have very big redundantly.

Main problems:

  • Anomalies of inserting (INSERT)
  • Anomalies of updating (UPDATE)
  • Anomalies of deleting (DELETE)

So this example already in 1NF, but it have some anomalies, so 1NF not enough for correct working of database. We need to use 2NF.

2NF - relation ship is relationship of 2NF only if there is no non-key attributes that depend from keys that are already parts of the complex key. Non-key attribute - attribute that excluded from any potential keys.

Requirements for 2NF are:

  • Table must be in first normal form (1NF).
  • All non-key attributes (columns) must be dependent on the entire key.

If we look at our example it's mean that (few samples):

--> - dependence

  • {StudentName, StudentSurname, StudentSecondName} --> StudentID
  • {SchoolAddress, SchoolName} --> SchoolId
  • ProjectName -->ProjectId
  • ClassName -->ClassId
  • {TeacherName, SchoolName, ClassName} --> StudentId
  • etc.

As you can see part of anomalies that was described in 1NF part, are solved - all data about students, teachers, schools now stored in separate tables - its mean that now now this information stored without any redundancy, but another parts of data still have same problems: as example: {TeacherName, SchoolName, ClassName} --> StudentId.

For solving all remain anomalies we must to use rules from 3NF - all non-key attributes must be independent. Also requirements for this 3NF are next:

  • Table must be in second normal form (2NF).
  • Table has no transitive dependencies.

After applying all rules to sample database i got next structure:

Image 1

Points of Interest

You can create database in few way:

  • using VisualStudion (as separate *.mdf file, during adding EntityFramework model etc)
  • using Microsoft SQL Management Studio
  • using another software

I use Visual Studio and EntityFramework Model First. After finishing i got generated script of database: you can download *.sql file as sample

Usefull links:

Whats to do next:

Next step - creating programm that will be use this database.

Comments:

Please look at my database, hope all was done correctly, if no - please comment and let me know where i was wrong.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood stuff ! Pin
Volynsky Alex15-Apr-14 23:06
professionalVolynsky Alex15-Apr-14 23:06 
AnswerRe: Good stuff ! Pin
Kirill__16-Apr-14 1:36
Kirill__16-Apr-14 1:36 
GeneralRe: Good stuff ! Pin
Volynsky Alex16-Apr-14 3:06
professionalVolynsky Alex16-Apr-14 3:06 
SuggestionSpellcheckers are not food, they are friends ... Pin
_Noctis_14-Apr-14 19:32
professional_Noctis_14-Apr-14 19:32 
GeneralRe: Spellcheckers are not food, they are friends ... Pin
Kirill__14-Apr-14 22:41
Kirill__14-Apr-14 22:41 
GeneralRe: Spellcheckers are not food, they are friends ... Pin
_Noctis_14-Apr-14 23:50
professional_Noctis_14-Apr-14 23:50 
GeneralRe: Spellcheckers are not food, they are friends ... Pin
Kirill__14-Apr-14 23:51
Kirill__14-Apr-14 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.