Data Models Notes Part 2 (1)
Data Models Notes Part 2 (1)
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.
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.
In the conceptual model, entities represent objects (like Customer, Order, Product)
relevant to the system.
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.
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.
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.
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:
3. Integrity Constraints
Definition: Integrity constraints are rules enforced on data within a database to maintain
its accuracy, validity, and consistency.
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:
Rule: A foreign key in one table must match an existing primary key value
in another table, or be null (if the relationship allows).
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.
4. User-Defined Integrity:
Custom rules that reflect specific business logic, which are not covered by
other integrity constraints.
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.
Error Prevention: Integrity constraints reduce the likelihood of data anomalies, ensuring
consistent and error-free data.
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.
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.
Defines data structures and Specifies how the logical model is implemented
Purpose
relationships in the DBMS
Not concerned with storage and Considers storage, indexing, and performance
Storage
indexing optimization
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:
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.
Each attribute in the logical model becomes a column in the physical model.
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
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.
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.
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
For instance, a CHECK constraint could ensure that Age in the Customer table is greater
than 0.
Importance of a Physical Data Model
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.
Numeric INT Integer data type for whole numbers. INT = 100
Double-precision floating-point
DOUBLE DOUBLE = 0.123456789
number.
Date and
DATE Stores date in YYYY-MM-DD format. DATE = '2024-01-01'
Time
Creating a Database: To create a new database, you use the CREATE DATABASE statement.
Creating Tables: To create a new table within a database, you use the CREATE TABLE
statement.
author_id INT,
publication_year INT,
genre VARCHAR(50)
);
The above SQL query will create an empty table:
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
Deleting Data: To delete records from a table, you use the DELETE FROM statement.
Dropping a Table: To delete a table and all its data, you use the DROP TABLE statement.