DBMS
DBMS
A Database Management System (DBMS) is software designed to store, retrieve, define, and manage data in a
structured manner. It provides an interface between users and databases, ensuring data consistency, security,
and efficient access.
Data Security & Integrity: Enforces access control and validation rules.
Efficient Query Processing: Optimizes data retrieval using query languages (e.g., SQL).
The database approach differs from traditional file-based systems in several ways:
1. Self-Describing Nature:
o The database contains metadata (data about data), such as table schemas, constraints, and
relationships.
2. Program-Data Independence:
6. Transaction Management:
3. DBMS Architecture
DBMS architecture defines how data is stored, accessed, and managed. The most common architecture is
the Three-Schema Architecture:
Three-Schema Architecture:
o Defines how different users view the data (e.g., customized reports).
o Example: A student sees only their grades, while a teacher sees all grades.
o Example: University database schema with tables for students, courses, and enrollments.
Advantages:
2. Three-Tier Architecture:
o Client Tier (UI) → Application Tier (Business Logic) → Database Tier (DBMS).
Advantages:
5. Data Models
A data model defines how data is structured, stored, and manipulated. Common models include:
1. Relational Model:
o Data stored in tables (relations) with rows (tuples) and columns (attributes).
2. Hierarchical Model:
3. Network Model:
4. Object-Oriented Model:
A distributed database system stores data across multiple locations (servers, sites).
Advantages:
Challenges:
Types of Schemas:
8. Data Independence
o Changes in the conceptual schema (e.g., adding a column) do not affect external schemas.
o Changes in the internal schema (e.g., indexing) do not affect the conceptual schema.
Importance:
The Entity-Relationship (ER) Model is a conceptual framework used to design and represent databases. It
helps in visualizing data requirements and relationships in a structured manner.
1. Entity
Examples:
2. Entity Type
Example:
3. Entity Set
Example:
Underlined
![Underlined Attribute] Primary Key
Attribute
Attributes
Example:
Types of Attributes
2. Composite Attribute
o Example:
3. Derived Attribute
o Example:
Age (derived from Date_of_Birth).
4. Multivalued Attribute
o Example:
Email_Addresses.
5. Key Attribute
o Example:
Keys in DBMS
1. Super Key
o Example:
For Student, (Roll_No, Name) is a super key, but Roll_No alone is sufficient.
2. Candidate Key
o Example:
3. Primary Key
o Example:
4. Foreign Key
o An attribute in one table that refers to the primary key of another table.
o Example:
6. Relationships
Example:
Types of Relationships
1. One-to-One (1:1)
o Example:
2. One-to-Many (1:N)
o Example:
3. Many-to-One (N:1)
o Example:
4. Many-to-Many (M:N)
o Example:
7. Weak Entity
An entity that cannot be uniquely identified without a relationship with another entity.
Depends on a strong (owner) entity.
Example:
1. Specialization
o Example:
2. Generalization
o Example:
o Opposite of specialization.
3. Aggregation
o Example:
Basic Syntax:
o This retrieves only the specified columns (column1, column2) from the table.
2. INSERT
Basic Syntax:
o You specify the table, columns, and the values that should be inserted.
Important Note: The number of values must match the number of columns listed.
3. UPDATE
Basic Syntax:
UPDATE table_name
WHERE condition;
o You specify which columns you want to update and the new values.
o The WHERE clause is essential to avoid updating every row in the table.
UPDATE employees
SET age = 31
Important Note: Without the WHERE clause, all rows in the table will be updated.
4. DELETE
Basic Syntax:
WHERE condition;
o You specify which rows to delete using a condition in the WHERE clause.
Example: To delete an employee named 'John Doe' from the employees table:
Important Note: If you omit the WHERE clause, ALL rows will be deleted, and data will be permanently
lost.
5. CREATE TABLE
Basic Syntax:
column1 datatype,
column2 datatype,
column3 datatype
);
o You define the table's name and the columns it will have, along with their data types (e.g., INT,
VARCHAR).
name VARCHAR(100),
age INT,
department VARCHAR(50)
);
Data Types: You define the type of data each column will hold (e.g., VARCHAR for text, INT for integers).
6. ALTER TABLE
o You can add new columns, modify data types, or even rename columns.
Important Note: You can also use DROP to remove columns, or MODIFY to change a column's datatype.
7. DROP TABLE
Purpose: Completely deletes a table from the database, including its structure and all data.
Basic Syntax:
8. WHERE
Basic Syntax:
9. ORDER BY
Purpose: Sorts the result set by one or more columns, either in ascending or descending order.
Basic Syntax:
10. GROUP BY
Purpose: Groups rows based on the values in one or more columns, often used with aggregate
functions like COUNT, SUM, AVG, etc.
Basic Syntax:
FROM table_name
GROUP BY column_name;
FROM employees
GROUP BY department;
Important Note: After GROUP BY, you can apply aggregate functions like:
11. HAVING
Purpose: Filters results after GROUP BY. It’s similar to WHERE, but HAVING works on aggregated data.
Basic Syntax:
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
FROM employees
GROUP BY department
Difference from WHERE: Use WHERE to filter rows before grouping; use HAVING to filter after
grouping.
12. JOIN
Purpose: Combines rows from two or more tables based on a related column, allowing you to retrieve
data from multiple tables at once.
Types of Joins:
o INNER JOIN: Returns only rows that have matching values in both tables.
o LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table, and matching rows from
the right table. If there’s no match, NULL is returned for columns from the right table.
o RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table, and matching rows
from the left table.
o FULL JOIN (or FULL OUTER JOIN): Returns all rows from both tables, with NULL where there is
no match.
Basic Syntax:
SELECT columns
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;
Example:
FROM employees
1. BETWEEN
The BETWEEN operator is used to filter the result set within a specified range.
Syntax:
SELECT column_name
FROM table_name
Example:
2. IN
The IN operator allows you to specify multiple possible values for a column.
Syntax:
SELECT column_name
FROM table_name
Example:
3. AND
All conditions must be true for a record to be included in the result set.
Syntax:
SELECT column_name
FROM table_name
WHERE condition1 AND condition2;
Example:
4. OR
The OR operator is used to combine multiple conditions where at least one condition must be true for a
record to be included in the result set.
Syntax:
SELECT column_name
FROM table_name
Example:
5. NOT
It can be combined with other operators like BETWEEN, IN, and LIKE.
Syntax:
SELECT column_name
FROM table_name
Example:
You can combine BETWEEN, IN, AND, OR, and NOT to form more complex queries.
Example:
Integrity constraints are rules that ensure the accuracy and consistency of data in relational databases. These
constraints are applied to columns in a table to enforce business logic and maintain data integrity. Below is a
summary of various types of integrity constraints along with other important SQL features such as nested
queries and set-comparison operators.
Key Points:
Example:
name VARCHAR(100)
);
Definition: The Not Null constraint ensures that a column cannot have NULL values.
Key Points:
Example:
);
3. Unique Constraint
Definition: The Unique constraint ensures that all values in a column (or a group of columns) are
unique.
Key Points:
o Unlike the primary key, a table can have multiple unique constraints.
Example:
);
4. Check Constraint
Definition: The Check constraint ensures that values in a column meet a specified condition.
Key Points:
o Validates domain integrity (e.g., ensuring a valid age range or salary limit).
Example:
);
Definition: A Foreign Key constraint enforces a link between two tables. It ensures referential integrity
by making sure that values in one table match the primary key of another table.
Key Points:
Example:
CREATE TABLE orders (
customer_id INT,
);
Definition: A nested query (also known as a subquery) is a query within another query. It is often used
to retrieve data that will be used in the outer query.
Key Points:
Example:
SELECT name
FROM employees
WHERE employee_id = (SELECT employee_id FROM employees WHERE name = 'John Doe');
Definition: A correlated subquery is a type of nested query that depends on the outer query for its
values.
Key Points:
o Unlike regular subqueries, correlated subqueries use values from the outer query.
o They are often executed once for each row selected by the outer query.
Example:
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department = e.department);
Set-Comparison Operators
Definition: Set-comparison operators allow comparison of a value to a set of values. These operators
are used to perform operations on multiple values.
Key Points:
o Operators include =, >, <, >=, <=, <> when applied to a set of values.
Examples:
o SELECT * FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE
department = 'Sales');
Definition: Aggregate functions are used to perform a calculation on a set of values and return a single
result. They are often used with the GROUP BY clause.
Key Points:
Examples:
o COUNT:
o SUM:
1. Domains
A domain is a set of atomic (indivisible) values from which attributes can take their values.
Example:
Characteristics of Domains:
Data Type: Specifies the type of data (e.g., integer, string, date).
2. Attributes
Types of Attributes:
2. Composite Attribute: Can be divided into sub-attributes (e.g., Name → First_Name, Last_Name).
4. Multi-Valued Attribute: Can hold multiple values (not directly supported in relational model; requires
normalization).
5. Derived Attribute: Computed from other attributes (e.g., Age derived from DOB).
3. Tuples
Example:
Atomic Values: Each attribute value must be atomic (no repeating groups or nested relations).
4. Relations
Example:
Characteristics of Relations:
4. Atomic Values: Each cell contains a single value (no composite/multi-valued attributes).
5. Relational Constraints
Example:
Types of Keys:
4. Foreign Key: An attribute that refers to the primary key of another relation (ensures referential
integrity).
Constraints:
o UNIQUE: Ensures no duplicates (but allows NULL unless combined with NOT NULL).
o Relation name
Example:
Copy
3. Constraints: Rules governing data integrity (PK, FK, NOT NULL, etc.).
7. Codd’s 12 Rules
Dr. E.F. Codd proposed 12 rules that a DBMS must satisfy to be considered a true relational database.
2. Guaranteed Access Rule: Every data item must be accessible via table name, primary key, and column
name.
3. Systematic Treatment of NULLs: NULL must represent missing/inapplicable data uniformly.
5. Comprehensive Data Sublanguage: Must support a language (like SQL) for defining and manipulating
data.
9. Logical Data Independence: Applications should not break if table structures change.
10. Integrity Independence: Integrity constraints must be definable in the data language.
Modern RDBMS like Oracle, MySQL, and PostgreSQL follow most of these rules.
Relational algebra is a procedural query language that operates on relations (tables) and produces relations as
results. It consists of a set of operations that can be performed on relations.
Purpose: Selects rows (tuples) from a relation that satisfy a given condition.
Notation: σ<sub>condition</sub>(R)
Example:
Notation: π<sub>attribute_list</sub>(R)
Example:
o Relation: Employee(EmpID, Name, Salary, Dept)
These operations require relations to be union-compatible (same number of attributes with matching
domains).
Notation: R ∪ S
Example:
o R: Employees in Dept A
o S: Employees in Dept B
o Relational Algebra: R ∪ S
Notation: R ∩ S
Example:
o S: Employees in Dept IT
o Relational Algebra: R ∩ S
Notation: R – S
Example:
o R: All employees
o S: Employees in Dept HR
o Query: Employees not in HR
o Relational Algebra: R – S
Notation: R ÷ S
Example:
o Relational Algebra: R ÷ S
3. Join Operations
Notation: R ⨝<sub>condition</sub> S
Example:
o Department(DeptID, DeptName)
Purpose: Returns all tuples from the left relation and matching tuples from the right (fills with NULL if
no match).
Notation: R ⟕ S
Example:
Notation: R ⟖ S
Example:
Purpose: Returns all tuples from both relations, filling NULL where no match exists.
Notation: R ⟗ S
Example:
o Query: All employees and all departments, with NULL where no match exists
4. ER to Relational Mapping
The primary key of the entity becomes the primary key of the table.
Many-to-Many (M:N): Create a junction table with foreign keys from both entities.
The table includes the primary key of the strong entity as a foreign key (part of its composite primary
key).
Create a separate table for the multi-valued attribute linked via a foreign key.
5. Data Normalization
1. Reflexivity: If Y ⊆ X, then X → Y.
2NF In 1NF + no partial dependency (non-prime attributes depend on the full primary key).
1. Unnormalized Table:
Copy
Copy
Student(StudentID, Name)
Course(CourseID, CourseName)
4. 3NF: Remove transitive dependencies (e.g., if CourseName depends on CourseID, it should be in the
Course table).
5. BCNF: Ensure every determinant is a superkey.