Database Design
& Development
Unit_04
ESOFT Metro Campus
Lecturer - Ms. Malshama Perera
Table of Contents
01 02
ER Diagrams Normalization
How to design ER How to normalize a
Diagrams. database.
03 04
SQL Testing
How to write sql How to test the
queries. system and create
test plans.
01
ER
Diagrams
1.1 What is ER Diagram
(Entity Relationship Diagrams)
● It is a visual representation of a database design before it is
actually implemented.
● It shows how entities (such as people, objects, or concepts) relate to
each other within the system.
Key components of ERD:
● Entities
● Attributes
● Relationships
● Cardinality
● Primary Key(PK) / Foreign Key(FK)
1.1.1 Entities
These are objects or concepts that can store data. In the diagram, entities are typically
represented by rectangles.
Ex : In a school database,
● Teachers
● Students
● Subjects are the entities.
1.1.2 Attributes
These are the data elements associated with entities. Attributes are often represented by
ovals connected to their corresponding entities.
Ex : In student entity,
● Student ID
● Student Name
● Student Birthday are the attributes.
1.1.2.1 Attribute Types
Composite Attributes Atomic Attributes
Composite attributes are attributes Atomic attributes (also known as
that can be divided into smaller simple attributes) are indivisible units
sub-parts, each of which is of data that cannot be broken down
meaningful on its own further into smaller meaningful
components.
fist_name
Stu_name
Stu_name Stu_id
last_name Student
Student Stu_age
Multi-valued Single-valued
Attributes
Multi-valued attributes are Attributes
Single-valued attributes are attributes
attributes that can hold multiple that can hold only one value for a
values for a particular entity particular entity instance.
instance.
Stu_name Stu_name
mobile_
Stu_id Student number Stu_id Student
Stu_age Stu_age
Multi-valued
attribute
Derived Attributes Stored Attributes
Derived attributes are attributes Stored attributes are attributes whose
whose values are not stored values are directly stored in the
directly but are instead computed database.
or derived from other attributes.
Stu_name Stu_name
Stu_birthd
Stu_id Student ate Stu_id Student
Stu_age Stu_age
Derived attribute
1.1.3 Relationships
These describe how entities interact with each other. Relationships are typically depicted as
diamonds connected to the entities involved.
Ex : In school database,
● Teacher teaches students.
● Students take subjects.
● Student has grades are the relationships.
1.1.4 Cardinality
This indicates the number of instances of one entity that can or must be associated with
instances of another entity.
1.1.2.1 Cardinality Types
● One to one (1:1)
● One to many (1:M)
● Many to many (M:N)
One to One (1:1)
Ex : In school database,
Student has a report card.
1 1 Report
Student has
card
One to Many (1:M)
Ex : In school database,
School has classrooms.
1 M
School has Classroom
Many to Many (M:N)
Ex : In school database,
Students study subjects.
M M
Student study Subject
1.1.5 Primary Key (PK)
Primary key is the unique identifier for each data row in a table.
Characteristics of Primary Key
● Minimal
● Not NULL value Stu_id Student
● Accessible
● Unique
Ex: In Student table,
Student ID can be taken as the Primary Key.
1.1.6 Foreign Key (FK)
Foreign Key is the Primary key of another table which uses to connect tables.
Characteristics of Foreign
Key
● Minimal
● Not NULL value Stu_id Student
● Accessible
● Unique
cla_id
Primary Key & Foreign Key
Class Table Student Table
Cla_id Cla_name Stu_id Stu_name Cla_id
12 Science 007 Science 12
13 Maths 008 Maths 13
14 Commerce 009 Commerce 14
Primary Key Foreign Key
1.1.7 Example
Scenario -
Maplewood Library, nestled in a small town, is a beloved spot for readers like Anna and Mr. Baker. Each book in
the library has a unique ID, and members borrow these books regularly.
When Anna borrowed "The Great Gatsby" on August 1st, and Mr. Baker borrowed "A Brief History of Time" on
August 3rd, these events were recorded in the Loan system. Each loan connects a member to a specific book,
tracking the borrowing and return dates.
This simple yet effective system keeps Maplewood Library running smoothly, ensuring that members can enjoy their
favorite books whenever they visit.
Answers -
Entities and attributes
● Member ● Book ● Loan
Member ID (PK) Book ID (PK) Loan ID (PK)
Member Name Book Name Member ID (FK)
Membership Date Author Name Book ID (PK)
Loan Date
Return Date
ER Diagram -
Bk_na Author_
Mem_ Mebersh Bk_id me name
Mem_id name ip_date
Member Book
1 M
Borrowed
Borrows
By
M Loan M
Loan Return
Loan_id Mem_id Bk_id
_date _date
02
Normalizatio
n
2.1 What is Normalization
● Normalization is the process of minimizing data redundancy and dependency
in database.
● It is a way of ensuring that data is stored efficiently and consistently.
Why Normalization?
● Efficiency - It reduces data storage by minimizing data redundancy.
● Data Integrity - It ensures that data is stored in a structured manner.
● Maintainability - It organizes the database in to logical units.
● Performance - It increases the performance by reducing the data redundancy when it
comes to complex queries.
2.2 Types of Normalization
1. 1st Normal Form - Make sure that each column is unique and every column
should have atomic values.
2. 2nd Normal Form - Achieves the 1st Form. All the non-key attributes should
be fully dependent on the Primary Key.
3. 3rd Normal Form - Achieves the 2nd Form. All the non-key attributes should
be fully independent of each other.
4. BCNF
2.3 Example
Normalize the below database table.
StudentID StudentName CourseID CourseName InstructorID InstructorName
101 John CSE101 Commerce 50 Dr. Sam
102 Alice MAT101 Maths 51 Dr. Jones
101 John MAL101 Maths 51 Dr. Jones
1st Normal Form
● The table is already in 1NF.
● Each column has atomic values.
● There’s no repeating values.
StudentID StudentName CourseID CourseName InstructorID InstructorName
101 John CSE101 Commerce 50 Dr. Sam
102 Alice MAT101 Maths 51 Dr. Jones
101 John MAT101 Maths 51 Dr. Jones
2nd Normal Form
Student Table Course Table
StudentID StudentName CourseID CourseName InstructorID
101 John CSE101 Commerce 50
102 Alice MAT101 Maths 51
Enrollment Table
StudentID CourseID
101 CSE101
102 MAT101
101 MAT101
3rd Normal Form
Course Table
CourseID CourseName InstructorID
CSE101 Commerce 50
MAT101 Maths 51
Instructor Table
InstructorID InstructorName
50 Dr. Sam
51 Dr. Jones
Final tables after normalization
Student Table Course Table
StudentID StudentName CourseID CourseName InstructorID
101 John CSE101 Commerce 50
102 Alice MAT101 Maths 51
Instructor Table Enrollment Table
InstructorID InstructorName StudentID CourseID
50 Dr. Sam 101 CSE101
51 Dr. Jones 102 MAT101
101 MAT101
03
SQL
3.1 What is SQL
SQL (Structured Query Language) is a standardized programming language used to manage and
manipulate relational databases.
Why SQL?
● SQL stands for Structured Query Language
● SQL lets you access and manipulate databases
● SQL is an ANSI (American National Standards Institute) standard
3.2 SQL Queries
A query is a command or a set of commands written to perform a specific operation on the data
stored in a database
Important SQL Queries
● Create Table ● Between
● Select ● In
● Where ● Group By
● Insert ● Order By
● Update ● Having
● Create Query ● Select Query ● Where Query
CREATE TABLE employees ( SELECT * FROM employees SELECT * FROM employees
id INT PRIMARY KEY, WHERE department = 'Sales'; WHERE department = 'Sales';
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2) );
● Insert Query ● Update Query ● Between Query
INSERT INTO employees (name, UPDATE employees SET salary = SELECT name, salary
department, salary) VALUES ('John 55000 WHERE name = 'John Doe'; FROM employees
Doe', 'Marketing', 50000); WHERE salary BETWEEN 40000
AND 60000;
● In Query ● Group By Query ● Order By Query
SELECT name, city SELECT department, COUNT(*) AS SELECT name
FROM customers num_employees FROM employees
WHERE city IN ('New York', 'Los FROM employees ORDER BY name ASC;
Angeles', 'Chicago'); GROUP BY department;
● Having Query
SELECT department, COUNT(*) AS
num_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
04
Testing
4.1 Testing Methods
● Testing methods are techniques which used to verify and validate that the software
functions are working correctly.
Testing Methods :
● Blackbox Testing
● Whitebox Testing
● System Testing
● Test Plan
● Refer this article for your further knowledge - Database Testing and Tools
4.2 Test Plan
● A test plan outlines the strategy, scope, resources, and schedule for the testing activities.
● It serves as a guide to ensure that testing is carried out systematically and that all critical
aspects of the software are covered.
Steps -
1. Write the objective.
2. Identify test scope.
3. Identify test environment.
4. Create test cases.
5. Create test schedule.
6. Risk Management.
7. Test Deliverables.
4.3 Test Case
● A test case is a predetermined set of circumstances, inputs, actions, and anticipated
outcomes intended to confirm a given software application feature or operation.
Components of test case -
● Test case id
● Test case description
● Pre-conditions
● Post-condition
● Expected result
● Actual result
● Pass/Fail criteria
Test Case Example
Test Case ID The id number of the test case
Test Case Description Describe the function you are going to test.
Pre-conditions Describe the pre-conditions that should be done
before testing this.
Post-conditions Describe the post-conditions that should be done
after testing this.
Expected Result Write the result you expect to have.
Actual Result Write the result you received after executing the
function.
Pass/Fail Mention the pass/fail criteria.
Thanks
Do you have any questions?
[email protected] +94 77 456 0331