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

lecture 2

The document discusses the relational model of data, introduced by Edgar F. Codd, which organizes data into tables and serves as the foundation for modern databases. It outlines the structure, operations, and constraints of data models, emphasizing the importance of relational and semi-structured data models. Additionally, it covers SQL, detailing its commands for data manipulation, definition, and control, along with examples of SQL queries.

Uploaded by

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

lecture 2

The document discusses the relational model of data, introduced by Edgar F. Codd, which organizes data into tables and serves as the foundation for modern databases. It outlines the structure, operations, and constraints of data models, emphasizing the importance of relational and semi-structured data models. Additionally, it covers SQL, detailing its commands for data manipulation, definition, and control, along with examples of SQL queries.

Uploaded by

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

DataBase System Lecture No 2

Chapter:2
The Relation Model of Data
The Relational model of data is a way to structure and query data in a database, introduced by
Edgar F. Codd in 1970. It organizes data into tables (also called relations), where each table
represents a single entity or concept. The relational model is the foundation of most modern
databases such as MySQL, PostgreSQL, and SQL Server.

Overview of Data Models


A Data model defines how data is structured, organized, and manipulated in a database. It serves
as a blueprint for designing and implementing databases by specifying how data is stored,
accessed, and related to other Data. Data models provide the conceptual and logical framework
for understanding the relationships and constraints between data elements. They also play a
crucial role in ensuring consistency, scalability, and efficiency in database management.

Parts of Data Models:


Data Model Basically Consist of three Major Parts:

1. Structural Part:
 Describes how data is organized and represented in the database.
 Defines the structure of data elements, their relationships, and constraints.
 Examples include tables, rows, and columns in the Relational model or nodes and
edges in the Graph model.
Key Elements:
 Entities: Objects or concepts (e.g., Student, Course).
 Attributes: Properties of entities (e.g., Name, Department).
 Relationships: Connections between entities (e.g., Student is enrolled in Course).
ID Name Department Course Session
2024-CS-16 Subhan CS Data Base 2024

2. Operations (Data Manipulation):

This part defines the actions that can be performed on the structured data.

Purpose: To interact with and modify the data stored in the database.

Common Operations:

1
DataBase System Lecture No 2

 Retrieval: Fetch data (e.g., SELECT query in SQL).


 Insertion: Add new data (e.g., INSERT INTO command).
 Update: Modify existing data (e.g., UPDATE command).
 Deletion: Remove data (e.g., DELETE FROM command).

Example SQL Queries:

 Retrieve all students:


SELECT * FROM Student;
 Add a new student:
INSERT INTO Student (RegistrationNumber, Name, Department,
Session) VALUES ('S002', 'Jane Smith', 'IT', 2020);

3. Constraints (Data Rules):

This part enforces Rules and Conditions to ensure data validity and integrity.

 Purpose: To ensure data is consistent, accurate, and meaningful.

 Common Types of Constraints:


1. Entity Integrity: Ensures unique identification of each record.
 Example: Primary Key (RegistrationNumber must be unique).
2. Referential Integrity: Maintains valid relationships between tables.
 Example: Foreign Key (Course_ID in the Student table must exist in the
Course table).
3. Domain Integrity: Ensures valid values for attributes.
 Example: Age must be a positive integer.

Summary of the Three Parts:


Part Description Examples
Structure Defines the organization of Student table with Name and
data (entities, attributes, Department.
relationships).
Operations Defines how to interact with SELECT, INSERT, UPDATE,
data (retrieve, insert, update, DELETE.
delete).

2
DataBase System Lecture No 2

Constraints Defines rules to ensure data Primary Key, Foreign Key,


integrity (entity, referential, NOT NULL
domain).

Important Data Models:


Today’s Two Prominent Model That is being Preferred Whole over The word is Given

1. Relational Data Model:


 Structure: Organizes data into tables (relations) with rows (tuples) and columns
(attributes).
 Key Feature: Relationships are established using keys (Primary and Foreign).
 Operations:
 CRUD operations using SQL (SELECT, INSERT, UPDATE, DELETE).
 Complex queries supported using joins and subqueries.
 Constraints:
Entity Integrity: Primary keys must be unique and not null.
Referential Integrity: Foreign keys must reference valid primary keys.
Domain Integrity: Attributes must adhere to defined domains (e.g., data types)
Example: E-commerce database with Users, Orders, and Products tables.
 Advantages:
 Simplicity and ease of querying with SQL.
 Widely supported and scalable.
 Disadvantages:
 Performance may decline with large-scale joins.
 Schema changes can be difficult.

ID Name Department Course Session


2024-CS-16 Subhan CS Data Base 2024

3
DataBase System Lecture No 2

2. Semi-Structured Data Model:

 Structure:
Data is organized in a hierarchical or nested format without a fixed schema.
Examples include key-value pairs, XML, and JSON documents.
Each data record may have a different structure, but metadata (tags or keys)
describes the data.
 Key Feature:
Data is self-descriptive, with tags or keys that provide structure, even if the schema
varies.
 Operations:
 Querying is done using specialized languages like XPath (for XML) or JSONPath
(for JSON).
 NoSQL databases (e.g., MongoDB, Cassandra) support CRUD operations on semi-
structured data.
 Example:
A product catalog where each product may have different attributes:

{
"product_id": 101,
"name": "Laptop",
"specs": {
"processor": "Intel i7",
"ram": "16GB"
}
}
{
"product_id": 102,
"name": "Book",
"author": "John Doe"
}

 Advantages:

 Flexibility: No fixed schema, making it easy to handle diverse or evolving data.


 Scalable: Ideal for large, varied datasets (e.g., logs, IoT data).
 Integration: Easily integrates with modern systems and APIs.

 Disadvantages:

 Query Complexity: Querying can be harder compared to SQL for structured data.

4
DataBase System Lecture No 2

 Storage Overhead: Metadata (tags, keys) increases the size of the data.
 Processing Difficulty: Nested and hierarchical structures require advanced processing.

Figure 1 How Relational Data Model and Semi-Structured Model Works

Other Data Models:


Following are Other Types of Data Model:

Figure 2 Types of Data Model

5
DataBase System Lecture No 2

Basics of the Relational Model


1. Structure

 Data Organization: .
 Rows: Represent individual records (also called tuples).
 Columns: Represent attributes (properties of data).
 Schema: Defines the structure of a table, including column names, data types, and
constraints.

2. Keys

 Primary Key: A unique identifier for each row (e.g., Student ID in a Student table).
 Foreign Key: A column that references the primary key of another table to establish a
relationship.

3. Operations

 CRUD operations (Create, Read, Update, Delete) are performed using SQL:
 SELECT: Retrieve data.
 INSERT: Add new records.
 UPDATE: Modify existing records.
 DELETE: Remove records.
 Complex queries like joins, subqueries, and aggregations allow relational databases to
process interconnected data.

4. Constraints

 Entity Integrity: Primary keys must be unique and non-null.


 Referential Integrity: Foreign keys must reference valid rows in related tables.
 Domain Integrity: Ensures values in a column are valid (e.g., data type restrictions).

5. Schema in the Relational Data Model


The schema in a relational model is the structure that defines the tables, their columns
(attributes), data types, and constraints. It provides the blueprint for how data is organized in the
database.
i.e
In Above Student table schema of Relational Data Modal Is Represented as
Student(ID,Name,Department,Course,Session)

6
DataBase System Lecture No 2

6. Domain in the Relational Data Model


The domain in a relational model refers to the set of permissible values that an attribute (column)
can have. It defines the allowable data types and the range or constraints on the values stored in
the table.

Student(ID:INT,Name:String,Department:String,Course:String,Session:String)
7. Relation Instance

A relation instance refers to a specific, current set of data or a snapshot of the data in a table
at a particular point in time in a relational database.

In simple terms:

 A relation (or table) defines the structure (schema) of the data, such as the columns
(attributes) and their data types.
 A relation instance is the actual data stored in the table at a given moment, i.e., the
rows (tuples) that conform to the table’s schema.

Figure 3 Relational Data Model

Database Schema:
A database schema is the structure of a database that defines how data is organized, stored, and
related within a database system. It includes tables, fields, data types, relationships, constraints,
and other database objects.

7
DataBase System Lecture No 2

Components of a Database Schema:

A database schema consists of the following elements:

Tables
 Store structured data in rows and columns.
 Each table has a unique name.
Fields (Columns/Attributes)
 Define the type of data stored in a table (e.g., name VARCHAR(50), age INT).
 Records (Rows/Tuples)
 Actual data entries stored in a table.
Constraints
 Ensure data integrity and consistency. Common constraints include:
 Primary Key: Uniquely identifies each record.
 Foreign Key: Maintains relationships between tables.
 Not Null: Ensures a field cannot be empty.
 Unique: Prevents duplicate values in a column.
Relationships
 Defines connections between tables (One-to-One, One-to-Many, Many-to-Many).
Indexes
 Improve search performance by indexing specific columns.

Example of a Database Schema

Table: Students

StudentID Name Age CourseID


101 A 18 CSE101
102 B 19 CS104

Table: Courses

8
DataBase System Lecture No 2

CourseID CourseName Instructor


CSE101 DBMS SIR.A
CS104 AI SIR.B

Relationships:

 StudentID is the Primary Key in the Students table.


 CourseID is a Foreign Key linking Students to Courses.

Algebra Query Language (Relational Algebra in Databases)


Introduction to Relational Algebra:
Relational Algebra is a procedural query language that works with relational databases. It
provides a set of operations to retrieve and manipulate data from relations (tables). It serves as
the foundation for SQL queries in relational database management systems (RDBMS).

Types of Relational Algebra Operations:

Relational algebra consists of two main types of operations:

1. Basic Operations – Select, Project, Union, Set Difference, Cartesian Product.


2. Advanced Operations – Join, Intersection, Division, Aggregation.

Basic Operations in Relational Algebra:

a) Selection (σ - Sigma)

 Used to filter rows (tuples) based on a condition.


 Notation: σ_condition (Relation_Name)
 Example: (Table is at Above page)

σ Age > 20 (Students) Retrieves all students where Age > 20.

b) Projection (π - Pi)

 Used to select specific columns (attributes).

9
DataBase System Lecture No 2

 Notation: π_column1, column2, ... (Relation_Name)


 Example: (Table is at Above page)

π Name, Age (Students)

Retrieves only the Name and Age columns from the Students table.

Importance of Relational Algebra:


Following are importance of Relational Algebra
Foundation for SQL – Helps understand SQL operations deeply.
Query Optimization – Helps in designing efficient queries.
Mathematical Framework – Provides a formal approach to database querying.

10
DataBase System Lecture No 2

Chapter: 6
The Data Base Language SQL

1. Introduction to SQL

SQL (Structured Query Language) is a standardized programming language used for managing
and manipulating relational databases. It allows users to create, retrieve, update, and delete
data from a database efficiently.

Types of SQL Commands

SQL is categorized into five main types of commands:

a) Data Query Language (DQL)

 Used for retrieving data from a database.


 Example: (Table is at Above page)

SELECT * FROM Students;

Retrieves all records from the Students table.

b) Data Definition Language (DDL)

11
DataBase System Lecture No 2

 Used to define and modify database structures.


 Commands:
o CREATE – Creates tables, databases, etc.
o ALTER – Modifies existing database structures.
o DROP – Deletes tables or databases.
o TRUNCATE – Removes all records from a table but keeps its structure.
 Example: (Table is at Above page)

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);

Creates a Students table with StudentID, Name, and Age columns.

c) Data Manipulation Language (DML)

 Used to manipulate existing data in tables.


 Commands:
o INSERT – Adds new records.
o UPDATE – Modifies existing records.
o DELETE – Removes records.
 Example:(Table is at Above page)

INSERT INTO Students (StudentID, Name, Age) VALUES (101, 'A', 22);

Inserts a new student into the Students table.

d) Data Control Language (DCL)

 Used to control access to data.


 Commands:
o GRANT – Provides access.
o REVOKE – Removes access.
 Example: (Table is at Above page)

GRANT SELECT ON Students TO User1;

Grants User1 permission to retrieve data from the Students table.

e) Transaction Control Language (TCL)

12
DataBase System Lecture No 2

 Used to manage database transactions.


 Commands:
o COMMIT – Saves all changes permanently.
o ROLLBACK – Undoes uncommitted changes.
o SAVEPOINT – Creates a temporary save point.
 Example: (Table is at Above page)

BEGIN TRANSACTION;
UPDATE Students SET Age = 23 WHERE StudentID = 101;
ROLLBACK;

Undoes the age update for student 101.

3. SQL Queries and Operations

a) Retrieving Data (SELECT)

 Used to fetch data from a database.


 Example: (Table is at Above page)

SELECT Name, Age FROM Students WHERE Age > 21;

Retrieves students older than 21.

b) Filtering Data (WHERE, LIKE, BETWEEN, IN)

 Example (WHERE): (Table is at Above page)

SELECT * FROM Students WHERE Age = 22;

Retrieves students aged 22.

 Example (LIKE - Pattern Matching): (Table is at Above page)

SELECT * FROM Students WHERE Name LIKE 'J%';

Retrieves names starting with "J".

 Example (BETWEEN - Range Selection): (Table is at Above page)

SELECT * FROM Students WHERE Age BETWEEN 20 AND 25;

13
DataBase System Lecture No 2

Retrieves students aged between 20 and 25.

 Example (IN - Multiple Conditions): (Table is at Above page)

SELECT * FROM Students WHERE Name IN ('John', 'Alice');

Retrieves students named John or Alice.

SQL Commands Detailed Table

SQL Command Description Example


SELECT Retrieves data from a SELECT * FROM
database employees;
INSERT Inserts new data into a INSERT INTO employees
table (name, age) VALUES
("John", 30);
UPDATE Modifies existing data in a UPDATE employees SET
table age = 31 WHERE name =
"John";
DELETE Deletes data from a table DELETE FROM
employees WHERE name
= "John";
CREATE Creates a new database or CREATE TABLE
table employees (id INT, name
VARCHAR(100));
DROP Deletes a table or database DROP TABLE employees;
ALTER Modifies an existing table ALTER TABLE
employees ADD
COLUMN salary INT;

14
DataBase System Lecture No 2

JOIN Combines data from two SELECT * FROM


tables based on a related employees JOIN
column departments ON
employees.department_id
= departments.id;
WHERE Filters records based on a SELECT * FROM
condition employees WHERE age >
30;
GROUP BY Groups records based on a SELECT department_id,
column COUNT(*) FROM
employees GROUP BY
department_id;

15

You might also like