Skip to main content

Module 16: Database Normalization - PostgreSQL Installation

Database Normalization

Normalization is a process used to organize data efficiently in a database. It avoid anomalies and reduce redundancy in the database.

What is an Anomaly?

Anomalies in databases are inconsistencies or errors that can occur when data is stored in a non-normalized or poorly structured manner. These issues arise when all data is kept in a single table or when redundancy exists.

There are three main types of anomalies:


1. Insertion Anomaly

Problem: Occurs when certain attributes cannot be inserted into the database without the presence of other attributes.

Example Table:

StudentIDStudentNameCourseInstructor
1AliceDBMSMr. Sami
2BobOOPMrs. Laila

Suppose a new course "Networking" is to be offered, but there are no students enrolled yet. You can't insert (null, null, Networking, Mr. Riaz) because StudentID and StudentName are required.


2. Update Anomaly

Problem: Occurs when redundant data exists, so updating one instance of data requires updating all other instances.

Example Table:

StudentIDStudentNameCourseInstructor
1AliceDBMSMr. Sami
2BobDBMSMr. Sami

If Mr. Sami changes his name to Mr. Khalid, every row for DBMS taught by Mr. Sami must be updated. Missing any row leads to inconsistency.


3. Deletion Anomaly

Problem: Occurs when deletion of an entry inadvertently removes additional, valuable information.

Example Table:

StudentIDStudentNameCourseInstructor
3JohnNetworkingMr. Riaz

If the only student enrolled in "Networking" (John) leaves and you delete his row, information about the "Networking" course and its instructor is lost from the table.


First Normal Form (1NF) Rules

  1. The table must have a primary key that uniquely identifies each row.
  2. Every attribute in the table must contain only atomic (indivisible) values—no repeating groups or arrays.

Second Normal Form (2NF) Rules

  1. The table is already in 1NF.
  2. Every non-key attribute in the table must be fully functionally dependent on the primary key.

Example:

Suppose we have the following table (not in 2NF):

StudentIDCourseInstructorGrade
1DBMSMr. SamiA
1OOPMrs. LailaB
2DBMSMr. SamiB

Here, the primary key is the combination of (StudentID, Course). However, the attribute "Instructor" depends only on the "Course", not on the full primary key.

To convert to 2NF:

  • Separate the instructor information into its own table.

StudentCourse Table:

StudentIDCourseGrade
1DBMSA
1OOPB
2DBMSB

CourseInstructor Table:

CourseInstructor
DBMSMr. Sami
OOPMrs. Laila

Now, all non-key attributes are fully dependent on the entire primary key.

Third Normal Form (3NF) Rules

  1. The table must already meet all the requirements of 2NF.
  2. There should be no transitive dependencies; that is, every non-key attribute must depend directly on the primary key, not through another non-key attribute.

What is a transitive dependency?

if A -> B -> C, A -> C is a transitive dependency.

A transitive dependency occurs when a non-key attribute depends on another non-key attribute, rather than directly on the primary key.

Example:

Suppose we have this table:

StudentIDCourseInstructorInstructorEmail
1DBMSMr. Samisami@gmail.com
2OOPMrs. Lailalaila@gmail.com
3DBMSMr. Samisami@gmail.com

Here, the primary key is the combination of (StudentID, Course).

  • "InstructorEmail" is dependent on "Instructor", not directly on (StudentID, Course). This is a transitive dependency.

To bring the table to 3NF:

  • Move instructor details (name and email) to a separate table.

StudentCourse Table:

StudentIDCourse
1DBMS
2OOP
3DBMS

Instructor Table:

CourseInstructorInstructorEmail
DBMSMr. Samisami@gmail.com
OOPMrs. Lailalaila@gmail.com

PostgreSQL Installation For Linux Arch

PostgreSQL Install
sudo pacman -S postgresql
  1. Initialize the database cluster
sudo -iu postgres
initdb --locale=en_US.UTF-8 -D /var/lib/postgres/data
exit
  1. Start and enable the database server
sudo systemctl start postgresql
sudo systemctl enable postgresql
  1. Access PostgreSQL CLI
sudo -iu postgres
psql
\q # Exit psql
  1. Create a new database & user
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
  1. Connect as your user
psql -U myuser -d mydb
  1. Authentication config (if get authentication error)
sudo nvim /var/lib/postgres/data/pg_hba.conf
# Change this
local all all peer
# To this
local all all md5

# Restart PostgreSQL
sudo systemctl restart postgresql

Some PostgreSQL Commands

  1. Connection Info
\conninfo
  1. List all databases
\l
  1. List all tables
\dt
  1. List all users
\du
  1. Show all commands
\?
  1. Create a table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(55) NOT NULL
);
  1. Show tables data
SELECT * FROM users;
  1. Switch to another database
\c mydb
  1. Connect to a PostgreSQL database as user "myuser" and database "mydb"
psql -U myuser -d mydb