lecture 2
lecture 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.
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
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
This part enforces Rules and Conditions to ensure data validity and integrity.
2
DataBase System Lecture No 2
3
DataBase System Lecture No 2
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:
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.
5
DataBase System Lecture No 2
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
6
DataBase System Lecture No 2
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.
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
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.
Table: Students
Table: Courses
8
DataBase System Lecture No 2
Relationships:
a) Selection (σ - Sigma)
σ Age > 20 (Students) Retrieves all students where Age > 20.
b) Projection (π - Pi)
9
DataBase System Lecture No 2
Retrieves only the Name and Age columns from the Students table.
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.
11
DataBase System Lecture No 2
INSERT INTO Students (StudentID, Name, Age) VALUES (101, 'A', 22);
12
DataBase System Lecture No 2
BEGIN TRANSACTION;
UPDATE Students SET Age = 23 WHERE StudentID = 101;
ROLLBACK;
13
DataBase System Lecture No 2
14
DataBase System Lecture No 2
15