You are on page 1of 21

Week 5, DBMS

Azamat Serek
ERD
The Entity-Relationship Diagram (ERD) tool is a To open the ERD canvas, you can simply select
database design tool that provides a graphical a database and select ERD Tool from the Tools
representation of database tables, columns, and menu. You will get a blank canvas to create your
inter-relationships. An ERD can give sufficient ERD from scratch.
information for the database administrator to
follow when developing and maintaining the
database. pgAdmin allows you to create an ERD using an
existing database or table as well. You can right
click on a database, select “ERD for database”
option to generate an ERD from the database.
Generate ERD
Saving ERD
Export ERD
One to one
When a row in a table is related to only CREATE TABLE Country
one role in another table and vice
(
versa,we say that is a one to one
relationship. This relationship can be Pk_Country_Id INT IDENTITY
created using Primary key-Unique foreign PRIMARY KEY,
key constraints. Name VARCHAR(100),

Officiallang VARCHAR(100),
For instance a Country can only have one Size INT(11),
UN Representative, and also a UN
Representative can only represent one );
Country.
CREATE TABLE UNrepresentative INSERT INTO Country
('Name','Officiallang',’Size’)
(
VALUES ('Nigeria','English',923,768);
Pk_UNrepresentative_Id INT PRIMARY KEY,

Name VARCHAR(100),
INSERT INTO UNrepresentative
Gender VARCHAR(100), ('Pk_Unrepresentative_Id','Name','Gender'
,'Fk_Country_Id')
Fk_Country_Id INT UNIQUE FOREIGN KEY
REFERENCES Country(Pk_Country_Id) VALUES (51,'Abubakar Ahmad','Male',1);
);
One to many
CREATE TABLE Car CREATE TABLE Engineer

( (

Pk_Car_Id INT PRIMARY KEY, Pk_Engineer_Id INT PRIMARY KEY,

Brand VARCHAR(100), FullName VARCHAR(100),

Model VARCHAR(100) MobileNo CHAR(11),

); Fk_Car_Id INT FOREIGN KEY REFERENCES


Car(Pk_Car_Id)

);
INSERT INTO Car ('Brand','Model') INSERT INTO Engineer
('Pk_Engineer_Id','FullName','MobileN
o','Fk_Car_Id')VALUES(50,'Elvis
Young','08038888888',2);
VALUES ('Benz','GLK350');

INSERT INTO Engineer


INSERT INTO Car ('Brand','Model') ('Pk_Engineer_Id','FullName','MobileN
o','Fk_Car_Id')VALUES(51,'Bola
Johnson','08020000000',1);

VALUES ('Toyota','Camry XLE');


Many to many
CREATE TABLE Student( CREATE TABLE Class(

StudentID INT(10) PRIMARY KEY, ClassID INT(10) PRIMARY KEY,

Name VARCHAR(100), Course VARCHAR(100),

); );
CREATE TABLE StudentClassRelation(

StudentID INT(15) NOT NULL,

ClassID INT(14) NOT NULL,

FOREIGN KEY (StudentID) REFERENCES


Student(StudentID),

FOREIGN KEY (ClassID) REFERENCES


Class(ClassID),

UNIQUE (StudentID, ClassID)

);
Normalization
Database normalization is a process by which
database and table structures are created or
modified in order to address
inefficiencies/complexities related to the
following:

● data storage
● data modification
● querying database tables
1NF
A 1NF database is an atomic database. In this
case, atomic means that each cell contains one
value and each row is unique. In the given
example, we can see that the non-atomic table
has cells with more than one value and
non-unique rows.
2NF
When a database is said to be 2NF, that means
the database is both 1NF and contains no partial
dependencies. A partial dependency is when an
attribute depends partly on the table’s primary key.
3NF
When making a 3NF database, two goals need to
be accomplished. The first being that the
database is already 2NF, and the second being
that the database contains no transitive functional
dependencies. A transitive functional dependency
is when a non-prime attribute is dependent on
another non-prime attribute.
Practice exercise to solidify understanding

Build ER diagram related to university


admission process and corresponding
tables with relationships consisting of
at least 5 entities (tables). Ensure that
they follow 3NF.
References
1) https://www.enterprisedb.com/blog/create-
erd-pgadmin-4
2) https://medium.com/geekculture/creating-a
-sql-entity-relationship-diagram-erd-ced5a
d1239d0
3) https://medium.com/@emekadc/how-to-im
plement-one-to-one-one-to-many-and-man
y-to-many-relationships-when-designing-a-
database-9da2de684710
4) https://www.codecademy.com/learn/fscp-2
2-advanced-postgresql/modules/wdcp-22-
normalizing-a-database/cheatsheet

You might also like