Fundamentals Additional Midterm

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Fundamentals of Database Systems

SCHOOL OF INFORMATION TECHNOLOGY

SUBJECT CODE: IM101

SCHOOL OF INFORMATION TECHNOLOGY

Module 4: Database Design and RDBMS

Lecture Objectives:

At the end of this lesson, students should be able to:

1. Understand the principles of good database design.


2. Identify and apply the stages of database design (conceptual, logical, and
physical).
3. Explain the structure and components of a Relational Database Management
System (RDBMS).
4. Use SQL to create, modify, and query relational databases.
5. Recognize the role of normalization in database design.

Introduction to Database Design

Database design is the process of producing a detailed data model of a database. It


involves specifying the data structure and the relationships between data points.
Proper design is crucial for ensuring that the database is efficient, reliable, and
scalable.

Why is Database Design Important?

 A well-designed database ensures data consistency and integrity.


 It reduces data redundancy and improves data retrieval efficiency.
 A poorly designed database leads to anomalies, redundant data, and complex,
slow queries.

Types of Database Design

1. Conceptual Design
2. Logical Design
3. Physical Design

Conceptual Design
This phase involves creating a high-level representation of the database that outlines
the entities, their attributes, and the relationships between entities. The Entity-
Relationship Diagram (ERD) is the primary tool used in this phase.

Entity-Relationship Diagram (ERD)

 Entities: Objects or things in the system (e.g., Customer, Order, Product).


 Attributes: Properties of entities (e.g., Customer Name, Order Date, Product
Price).
 Relationships: Connections between entities (e.g., a Customer places an
Order).

Example ERD

For an Online Store, the ERD would have entities such as:

 Customer: Attributes include Customer ID (Primary Key), Name, and Address.


 Order: Attributes include Order ID (Primary Key), Order Date, and Total
Amount.
 Product: Attributes include Product ID (Primary Key), Product Name, and Price.
 Relationships: A Customer places many Orders, and an Order can include
many Products.

Primary Keys and Foreign Keys

 Primary Key: A unique identifier for each record in a table (e.g., Customer ID,
Order ID).
 Foreign Key: A field in one table that references the primary key in another
table, used to establish a relationship between tables (e.g., Order table has a
Customer ID that references Customer table).

Logical Design

After the conceptual design, the ERD is transformed into a relational schema that can
be implemented in a database. In this phase, tables are defined, and relationships
between tables are formalized.

Relational Schema
 Tables: Represent entities from the ERD.
 Columns: Represent attributes, and each row represents a record.
 Relationships: Defined by primary and foreign keys.

Example Relational Schema

For the Online Store example:

 Customer Table: (Customer ID, Name, Address)


 Order Table: (Order ID, Order Date, Customer ID (Foreign Key))
 Product Table: (Product ID, Product Name, Price)
 Order_Product Table: (Order ID (Foreign Key), Product ID (Foreign Key),
Quantity)

Data Types and Constraints

 Data Types: Each attribute in a table must be assigned a data type (e.g.,
VARCHAR for text, INT for integers, DATE for dates).
 Constraints: Ensure data integrity, such as NOT NULL (a field must always
have a value), UNIQUE (no duplicate values), and FOREIGN KEY constraints for
relationships.

Physical Design

The Physical Design phase involves mapping the logical schema to the physical
storage in the chosen Database Management System (DBMS). This phase focuses on
optimizing the design for performance and storage efficiency.

Storage Considerations

 Choosing appropriate data types based on the expected data size and
frequency of updates.
 Implementing indexes to speed up query performance.
o Example: Indexing a frequently searched field like Customer ID to make
retrieval faster.

Optimization Techniques

 Normalization: Reduces redundancy and improves data integrity.


 Denormalization: Used in some cases to improve query performance,
especially for read-heavy operations (at the cost of potential redundancy).

Principles of Good Database Design

A well-designed database follows these essential principles:

1. Data Consistency: Ensures that data remains accurate and consistent across
the database.
o For example, in a customer order system, if a customer’s details are
updated in one place, all references to that customer should reflect those
changes.
2. Data Redundancy Minimization: Redundancy occurs when the same data is
stored in multiple places, leading to wasted space and potential inconsistencies.
A well-designed database minimizes redundancy by normalizing the data.
3. Data Integrity: Enforcing constraints (e.g., primary key, foreign key) ensures
the accuracy and consistency of the data.
o Referential Integrity: Ensures that foreign keys accurately reference
primary keys in related tables.
o Example: If a customer is deleted, all orders associated with that
customer must also be handled accordingly (cascade delete or restrict
delete).
4. Flexibility: A flexible database design allows for easy updates and
modifications as business needs evolve.
o Example: If new products are introduced or new customer data needs to
be captured, the design should allow such changes without extensive
restructuring.

Relational Database Management Systems (RDBMS)

 RDBMS: Systems that manage data using a structured format of tables (or
relations) with predefined relationships between them. They are the foundation
for most modern database applications.
 Data Storage: In Relational Database Management System (RDBMS) data is
stored in tables (relations) and relationships between data are defined using
keys.
 Data Organization: In an RDBMS, data is organized in rows and columns, with
each table containing records (rows) and attributes (columns). The system
supports querying, updating, and managing data through a structured query
language (SQL).
 Example: An employee database where each employee's details are stored in a
table, with relationships to other tables (e.g., departments, roles).

What is a Relational Database?


A relational database is a collection of data organized into tables (relations) where
each table consists of rows (tuples) and columns (attributes). The relationships
between these tables are established through keys (primary keys and foreign keys).

RDBMS

 RDBMS: A software system that allows users to create, manage, and


manipulate relational databases.
 Examples of popular software in DBMS use for RDBMS platforms
include:
o MySQL
o PostgreSQL

o Oracle
o Microsoft SQL Server

Components of RDBMS

 Tables (Relations): The primary structure used to store data in an RDBMS.


Each table contains rows (representing records) and columns (representing
attributes of the records).
o Example:

o Customer Table:

o Columns: Customer ID, Name, Address


o Rows: Individual customer records
 Primary Key: A unique identifier for each record in a table. No two rows can
have the same primary key value.
o Example:
o In the Customer Table, the Customer ID could be the primary key,
ensuring that each customer has a unique ID.
 Foreign Key: A column (or combination of columns) in one table that uniquely
identifies a row in another table. Foreign keys are used to establish relationships
between tables.
o Example:
o The Order Table may have a Customer ID as a foreign key that
references the primary key in the Customer Table.

Relationships Between Tables

 In relational databases, tables are connected using one-to-one, one-to-many, or


many-to-many relationships.
 Example:
o A Customer can place many Orders, so there’s a one-to-many relationship
between the Customer and Order tables.
Indexes

 Indexes are created to speed up data retrieval operations in a table. They allow
the RDBMS to find data quickly without scanning the entire table.
 Example:
o An index on the Customer ID in the Order Table allows faster searching
when retrieving all orders made by a specific customer.

SQL and Data Manipulation in RDBMS

SQL (Structured Query Language) - is the standard language used to interact with
RDBMS. It allows users to perform various operations such as creating tables,
inserting data, updating records, and retrieving data.

Basic SQL Operations

 CREATE: The CREATE statement is used to create a new database object, such
as a table, index, view, or database. It defines the structure of the object and
specifies the data types for each column.
o Syntax use in creating database:

o CREATE DATABASE StudentRecords;


o Syntax use in creating tables:
o CREATE TABLE Students ( StudentID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
BirthDate DATE, Gender CHAR(1),
EnrollmentDate DATE
);

 INSERT: The INSERT statement is used to insert new rows of data into a table.
It can insert data into all columns or only specific columns.
o Insert a Single Row:
o INSERT INTO Students (StudentID, FirstName, LastName, BirthDate,
Gender, EnrollmentDate)
VALUES (101, 'John', 'Doe', '2001-05-15', 'M', '2024-08-01’);
o Insert a Row without Specifying All Columns:
o INSERT INTO Students (FirstName, LastName, Gender)
VALUES ('Jane', 'Smith', 'F’);
o Insert Multiple Rows:
o INSERT INTO Students (StudentID, FirstName, LastName, BirthDate,
Gender, EnrollmentDate)
VALUES (102, 'Alice', 'Johnson', '2000-04-10', 'F', '2024-07-15'),
(103, 'Bob', 'Williams',
'1999-11-22', 'M', '2024-07-20');
 SELECT: The SELECT statement is used to query and retrieve data from a
database. It can fetch data from specific columns or all columns, and it can also
apply filters to select specific rows.
o Sample syntax for select:
o SELECT * FROM table_name;
o SELECT FirstName, LastName FROM Students;

o SELECT FirstName, LastName FROM Students WHERE Gender = 'M';


 UPDATE: The UPDATE statement is used to modify existing records in a table. It
allows you to change values in one or more columns for specific rows.
o Update Specific Rows:
o UPDATE Students
SET LastName = 'Doe', FirstName = 'John'
WHERE StudentID = 101;
o Update Multiple Columns:
o UPDATE Students
SET LastName = 'Smith', Gender = 'M’
WHERE StudentID = 202;
 DELETE: The DELETE statement is used to remove rows from a table. It can
delete specific rows based on conditions, or all rows if no condition is specified.
o Syntax for Delete Specific Rows:
o DELETE FROM Students WHERE StudentID = 101;
o Syntax for Delete All Rows (without deleting the table):
o DELETE FROM Students;

Normalization in RDBMS

Normalization is a process of organizing data to minimize redundancy and


dependency. It involves dividing large tables into smaller, related tables and defining
relationships between them.

Why Normalize?

 Avoids data redundancy: Ensures that each piece of data is stored only once.
 Ensures data integrity: Prevents data anomalies (insertion, update, and deletion
anomalies).

Normal Forms

 First Normal Form (1NF): Ensures that each table column contains atomic
(indivisible) values, and each entry in a column contains only one value.
o Example: A phone number column should not contain multiple phone
numbers separated by commas.
 Second Normal Form (2NF): A table is in 2NF if it is in 1NF and all non-key
attributes are fully dependent on the primary key.
o Example: In a table where the primary key is a combination of two fields,
no other fields should depend on just one part of the key.
 Third Normal Form (3NF): A table is in 3NF if it is in 2NF and all its attributes
are directly dependent only on the primary key.
o Example: A table with OrderID as the primary key should not store
customer details, as these should be kept in a separate Customer table.

Properties in RDBMS

RDBMS properties to ensure data reliability and consistency, especially in transaction


processing.

1. Atomicity: Ensures that all operations in a transaction are completed


successfully. If any operation fails, the entire transaction is rolled back.
o Example: If an order transaction involves updating two tables (order and
inventory), either both updates happen, or neither happens.
2. Consistency: Ensures that the database moves from one valid state to
another. Any transaction will bring the database from one valid state to another,
maintaining data integrity.
3. Isolation: Ensures that transactions are processed independently. Multiple
transactions happening at the same time should not affect each other.
4. Durability: Ensures that once a transaction is committed, it remains in the
system even in the event of a system failure.

Advantages of RDBMS

 Data Integrity and Security: RDBMS ensures that data remains consistent
and secure using constraints and access control.
 Flexibility in Querying: SQL allows complex queries to be written easily,
enabling data retrieval in various ways.
 Reduced Redundancy: Data is stored only once, thanks to normalization,
reducing redundancy.
 Concurrency Control: Multiple users can access the database simultaneously,
with the system ensuring isolation and consistency.

Disadvantages of RDBMS

 Complexity in Handling Large Datasets: As the data grows, managing and


querying very large datasets can become complex and slower.
 Cost of Setup and Maintenance: High-end RDBMS solutions can be costly in
terms of licensing, hardware, and maintenance.
 Performance Bottlenecks with Complex Queries: While RDBMS are
efficient, highly complex queries with multiple joins can slow down performance.

Thank you!

Glenda M. Flores
Instructor

You might also like