0% found this document useful (0 votes)
12 views

Data Models Notes Part 2 (1)

Note for software engineering

Uploaded by

ryanticha969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Models Notes Part 2 (1)

Note for software engineering

Uploaded by

ryanticha969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction to Logical Modeling

 Definition: Logical modeling is the process of creating a data model that organizes and
structures the data elements and defines the relationships among them without concern for
how they will be physically implemented. It represents the structure of information within
a system in a way that is independent of specific technology.

 Purpose: The logical model aims to design a clear blueprint of the system’s data structure,
providing a foundation that can later be converted into a physical model for
implementation.

2. Converting a Conceptual Model to a Logical Model

 Conceptual Model Overview:

o The conceptual model, often created in the early design stages, is a high-level
representation of the system’s entities, attributes, and relationships.

o It focuses on the what, not the how, of data management, using ER (Entity-
Relationship) diagrams to map out the entities and relationships.

Steps to Convert from Conceptual to Logical Model:

Identify Entities and Attributes:

 In the conceptual model, entities represent objects (like Customer, Order, Product)
relevant to the system.

 Each entity’s attributes, such as CustomerName, CustomerID, and Address, are


identified and carried over into the logical model.

3. Define Primary Keys:

 Each entity needs a unique identifier, or primary key, to ensure that each instance (or
row) is distinguishable.

 For example, a CustomerID attribute may serve as the primary key for the Customer
entity.

4. Establish Relationships Between Entities:


 Relationships identified in the conceptual model, such as one-to-many or many-to-
many, are maintained and specified in the logical model.

 If a conceptual model shows that a Customer places Orders, a one-to-many


relationship (1

) exists where one Customer can place multiple Orders.

5. Normalize Data to Reduce Redundancy:

 Normalization is the process of organizing data to minimize redundancy and


dependency. For example:

 If an attribute depends only on another attribute (not the entity as a whole), it


may be moved to a new entity.

 For example, a Customer entity with an Address attribute could lead to the
creation of an Address entity if different customers can share the same address.

6. Convert Relationships to Foreign Keys:

 Foreign keys link entities together by referencing the primary key of one entity within
another.

 For instance, CustomerID in the Customer entity can be a foreign key in the Order
entity to indicate which customer placed which order.

7. Define Data Types and Constraints:

 Assign data types (e.g., String, Integer, Date) to each attribute and specify constraints
to enforce the structure and integrity of the data.

 Example:

If a conceptual model shows an Employee entity related to a Department entity (where each
employee belongs to one department), the logical model will contain:

o Entities: Employee (with attributes like EmployeeID, Name, JobTitle) and


Department (with attributes like DepartmentID, DepartmentName).
o Primary Keys: EmployeeID for Employee, DepartmentID for Department.

o Relationships: A foreign key DepartmentID in Employee links employees to


departments.

3. Integrity Constraints

 Definition: Integrity constraints are rules enforced on data within a database to maintain
its accuracy, validity, and consistency.

 Types of Integrity Constraints:

1. Entity Integrity:

 Ensures that each entity has a unique identifier, typically enforced by the
primary key.

 Rule: No part of a primary key can be null since each record must be
identifiable.

 Example: In an Order entity, OrderID serves as the primary key and cannot
be null.

2. Referential Integrity:

 Ensures that relationships between tables remain consistent, often enforced


using foreign keys.

 Rule: A foreign key in one table must match an existing primary key value
in another table, or be null (if the relationship allows).

 Example: If an Order table has a CustomerID foreign key, it must reference


an existing CustomerID in the Customer table.

3. Domain Integrity:

 Defines permissible values for attributes based on the data type, range, or
format.
 Rule: Enforces that data entered into an attribute conforms to its defined
format and type.

 Example: An Age attribute may be constrained to only accept integer


values from 0 to 120.

4. User-Defined Integrity:

 Custom rules that reflect specific business logic, which are not covered by
other integrity constraints.

 Rule: Imposes constraints specific to the organization’s requirements.

 Example: In a company, an employee’s work hours might be capped at 40


hours per week for non-overtime employees.

 Importance of Integrity Constraints:

o Data Accuracy: Prevents errors by restricting invalid data entry.

o Data Consistency: Ensures all parts of the database remain in sync by enforcing
strict relationships between tables.

o Data Security: Protects the data by ensuring that changes to sensitive areas follow
defined rules.

o Example: Referential integrity protects against deleting a Customer record that has
linked Order records, preserving data consistency.

4. Benefits of Logical Modeling and Integrity Constraints

 Improved Database Design: Logical models provide a clear, organized representation of


data structures, allowing for efficient database design and smoother transition to the
physical model.

 Error Prevention: Integrity constraints reduce the likelihood of data anomalies, ensuring
consistent and error-free data.

 Efficient Querying: A logical model simplifies complex relationships, improving the


accuracy and speed of data retrieval.
The Physical Data Model

A Physical Data Model (PDM) is a detailed blueprint for building the actual database. It specifies
how the logical model will be implemented in the database management system (DBMS). It
represents the structure of the database at a low level, incorporating technical details such as data
types, indexes, primary keys, and foreign keys.

Key Components of a Physical Data Model

1. Tables – These are derived from entities in the logical model. Each entity becomes a table
in the physical model.

2. Columns and Data Types – Each attribute in the logical model becomes a column in the
physical model, and specific data types are assigned (e.g., INTEGER, VARCHAR,
DATE).

3. Primary Keys – Identifiers that uniquely determine each row in a table. Primary keys are
critical for enforcing data integrity.

4. Foreign Keys – Keys that establish relationships between tables. Foreign keys ensure
referential integrity by linking rows from one table to another.

5. Indexes – Structures that improve the speed of data retrieval. Indexes are often created on
frequently accessed columns to speed up queries.

6. Constraints – Rules applied to columns to maintain data accuracy and integrity. Examples
include NOT NULL, UNIQUE, and CHECK constraints.

Differences Between Logical and Physical Data Models

Feature Logical Data Model Physical Data Model

Defines data structures and Specifies how the logical model is implemented
Purpose
relationships in the DBMS

Detailed tables, columns, data types, and


Focus High-level entities and relationships
indexes
Feature Logical Data Model Physical Data Model

Data Database-specific (e.g., VARCHAR, INT,


Abstract (e.g., String, Number)
Types DATE)

Primary and foreign keys as abstract


Keys Implemented as actual primary and foreign keys
concepts

Not concerned with storage and Considers storage, indexing, and performance
Storage
indexing optimization

Converting a Logical Model to a Physical Data Model

Converting a logical model to a physical model involves transforming entities and relationships
from an abstract level to a detailed, implementable structure. Here’s a step-by-step guide:

1. Define Tables for Each Entity

 In the logical model, entities represent real-world objects. In the physical model, each
entity becomes a table.

 For example, if you have an entity called Customer, it becomes a table called Customer in
the physical model.

2. Determine Columns and Assign Data Types

 Each attribute in the logical model becomes a column in the physical model.

 Assign data types based on the DBMS. For instance:

o Name might be VARCHAR(50) if the DBMS is MySQL.

o Age could be INT.

o DateOfBirth could be DATE.

 The choice of data types impacts storage and performance, so it’s essential to pick types
that balance accuracy and efficiency.
3. Identify and Set Primary Keys

 Each table needs a primary key to uniquely identify each row.

 Primary keys are usually single columns but can also be composite (multiple columns).

 For example, in a Customer table, a column called CustomerID may be used as a primary
key.

4. Establish Relationships and Foreign Keys

 Relationships between entities are established using foreign keys in the physical model.

 For example, if a Customer places an Order, there would be a foreign key in the Order table
that references the CustomerID in the Customer table.

 This foreign key helps maintain referential integrity by ensuring that orders are only placed
by existing customers.

5. Define Indexes for Performance

 Indexes are created on frequently searched columns to speed up data retrieval.

 For example, if LastName in the Customer table is frequently used in search queries,
adding an index on LastName will improve search performance.

6. Apply Constraints

 Constraints ensure data accuracy and integrity. Common constraints include:

o NOT NULL – Ensures a column cannot have a NULL value.

o UNIQUE – Ensures all values in a column are unique.

o CHECK – Enforces a specific condition on a column.

 For instance, a CHECK constraint could ensure that Age in the Customer table is greater
than 0.
Importance of a Physical Data Model

A physical data model is critical for:

1. Performance Optimization – By using indexes and appropriate data types, it improves


database performance.

2. Data Integrity – Ensures data remains accurate and consistent with keys and constraints.

Storage Efficiency – Helps optimize storage requirements by choosing the right data types and
storage options.
Structured Query Language (SQL)

SQL stands for Structured Query Language. It's a specialized programming language designed for
interacting with relational databases. Here's a brief introduction:

What it does:

Manage Data: SQL allows you to create, manipulate, and retrieve data stored in relational
databases.

Queries: You write SQL statements called queries to interact with the database.

Structured Data: Relational databases organize data in tables with rows and columns, making it
easy to query and organize information.

Core functionalities:

Data Definition Language (DDL): Enables creating, modifying, and deleting database structures
like tables, columns, and indexes.

Data Manipulation Language (DML): Allows inserting, updating, and deleting data within the
tables. Data Query Language (DQL): Provides the ability to retrieve specific data from the
database based on certain criteria.

Data Control Language (DCL): Manages access permissions to the database objects.

Transaction Control Language (TCL): Manages transactions in the database.

Basic SQL Data Types

Category Data Type Description Example

Numeric INT Integer data type for whole numbers. INT = 100

Floating-point number for decimal


FLOAT FLOAT = 3.14
values.

Fixed-point decimal number, with DECIMAL(10, 2) =


DECIMAL(p,s)
precision p and scale s. 123.45
Category Data Type Description Example

Large integer data type, allows for BIGINT =


BIGINT
greater range than INT. 123456789012345

Small integer data type, uses less


SMALLINT SMALLINT = 32767
storage than INT.

Double-precision floating-point
DOUBLE DOUBLE = 0.123456789
number.

Fixed-length character string of length


String CHAR(n) CHAR(5) = 'Hello'
n.

Variable-length character string up to VARCHAR(10) =


VARCHAR(n)
length n. 'Database'

Large text data type for strings with


TEXT = 'Sample
TEXT variable lengths, often for descriptive
description'
text.

Date and
DATE Stores date in YYYY-MM-DD format. DATE = '2024-01-01'
Time

TIME Stores time in HH:MM:SS format. TIME = '13:45:00'

Stores both date and time in YYYY- DATETIME = '2024-01-


DATETIME
MM-DD HH:MM:SS format. 01 13:45:00'

Stores date and time, often for tracking TIMESTAMP = '2024-01-


TIMESTAMP
changes, in UTC format. 01 13:45:00'

Stores true or false values, typically


Boolean BOOLEAN BOOLEAN = true
represented as 1 (true) and 0 (false).
Category Data Type Description Example

Binary Large Object, used for storing


Binary BLOB large binary data such as images or BLOB = Binary image data
files.

Stores spatial data, such as geometric


Spatial GEOMETRY GEOMETRY = Point(1,1)
shapes, points, and lines.

Basic SQL Commands

Creating a Database: To create a new database, you use the CREATE DATABASE statement.

CREATE DATABASE library;

Creating Tables: To create a new table within a database, you use the CREATE TABLE
statement.

CREATE TABLE books (


book_id INT PRIMARY KEY,
title VARCHAR(100),

author_id INT,
publication_year INT,
genre VARCHAR(50)

);
The above SQL query will create an empty table:

book_id title author_id publication_year genre

Inserting Data: To insert new records into a table, you use the INSERT INTO statement.
INSERT INTO books (book_id, title, author_id, publication_year, genre)
VALUES (1, 'Harry Potter', 1, 1997, 'Fantasy');

The above SQL query will insert the data into the table:

Updating Data: To update existing records in a table, you use the UPDATE statement.

UPDATE books

SET genre = 'Mystery'

WHERE title = 'Harry Potter';

The above SQL query will update the table:

book_id title author_id publication_year genre

1 Harry Potter and the Philosopher's Stone 1 1997 Mystery

Deleting Data: To delete records from a table, you use the DELETE FROM statement.

DELETE FROM books

WHERE title = 'Harry Potter';


The above SQL query will delete all rows where the title is Harry Potter:

book_id title author_id publication_year genre

Dropping Tables and Databases:

Dropping a Table: To delete a table and all its data, you use the DROP TABLE statement.

DROP TABLE books;


Dropping a Database: To delete an entire database, you use the DROP DATABASE statement.

DROP DATABASE library;

You might also like