UNIT 3 Dbms
UNIT 3 Dbms
UNIT 3 Dbms
The Entity-Relationship (ER) model is a conceptual framework used in database design to - **Relationships** describe how entities are related to each other.
represent and organize the structure of a database. It provides a visual way to model and
understand the data and its relationships. Here’s a comprehensive introduction to the ER - They are represented as diamonds connecting entities.
model:
- Relationships can be:
1. **What is the ER Model?**
- **Unary (Recursive):** An entity is related to itself (e.g., an employee supervising
The Entity-Relationship (ER) model is a high-level data model that describes data and their other employees).
relationships in a structured way. It is used to design databases at a conceptual level,
before translating them into a physical database. - **Binary:** Two different entities are related (e.g., employees and departments).
2. **Key Components of the ER Model** - **Ternary:** Involving three different entities (e.g., a project involving employees
and departments).
1. **Entities:**
4. **Entities and Relationships Examples:**
- An **entity** represents a real-world object or concept that can be distinctly
identified. - **Employee** (Entity) - Attributes: Employee_ID, Name, Date_of_Birth
- Entities are typically nouns and can be physical objects (like "Employee") or abstract - **Department** (Entity) - Attributes: Department_ID, Department_Name
concepts (like "Project").
- **Works_In** (Relationship) - Connects Employee and Department
- Each entity has attributes that describe its characteristics.
5. **Cardinality:**
2. **Attributes:**
- **Cardinality** specifies the number of instances of one entity that can be associated
- **Attributes** are properties or characteristics of entities. with instances of another entity.
- For example, an "Employee" entity might have attributes like "Employee_ID," "Name," - Types of cardinality include:
and "Date_of_Birth."
- **One-to-One (1:1):** Each instance of Entity A is associated with at most one
- Attributes can be: instance of Entity B, and vice versa.
- **Simple:** Indivisible (e.g., a name). - **One-to-Many (1:N):** Each instance of Entity A can be associated with multiple
instances of Entity B, but each instance of Entity B is associated with only one instance of
- **Composite:** Made up of multiple sub-attributes (e.g., a full address with street, Entity A.
city, and zip code).
- **Many-to-Many (M:N):** Instances of Entity A can be associated with multiple
- **Derived:** Calculated from other attributes (e.g., age derived from date of birth). instances of Entity B, and vice versa.
- A **primary key** is a unique identifier for each entity instance. 1. **Basic ER Diagram:**
- For example, "Employee_ID" might be used as a primary key for the "Employee" entity. +-------------+ +-------------+
- A **foreign key** is an attribute that creates a link between entities. It refers to the +-------------+ +-------------+
primary key of another entity.
| Emp_ID (PK) |-------| Dept_ID (PK)|
- For example, in the "Works_In" relationship, "Employee_ID" might be a foreign key
linking to the "Employee" entity. | Name | | Dept_Name |
+-------------+
+-------------+ +-------------+
| Employee | | Department |
+-------------+ +-------------+
| Name | | Dept_Name |
An **ER diagram** (or ERD) is a visual representation of the ER model. It uses symbols to | DOB | +-------------+
represent entities, attributes, and relationships, and the lines connecting them illustrate
their relationships. +-------------+
**Symbols:** |
| Project | **Entities** represent real-world objects or concepts that can have distinct existence.
They can be physical objects (e.g., a person, a car) or abstract concepts (e.g., a
+-------------+ department, a project).
| Project_ID | **Representation:**
+-------------+ - **Name:** The name of the entity is written inside the rectangle.
1. **Identify Entities:** Determine the key entities that need to be represented. - A rectangle labeled **"Employee"** might represent an entity in a company database.
2. **Identify Attributes:** Determine the attributes for each entity. **Example ER Diagram:**
3. **Determine Relationships:** Identify how entities are related and the type of +-------------+
relationships.
| Employee |
4. **Define Cardinality:** Establish the cardinality of relationships.
+-------------+
5. **Draw ER Diagram:** Create a visual representation of the entities, attributes, and
relationships. 2. **Attributes**
5. **Benefits of ER Modeling** **Attributes** describe properties or characteristics of entities. Each entity can have
multiple attributes.
- **Clear Visualization:** Provides a clear, graphical representation of the database
structure. **Representation:**
- **Improved Communication:** Helps in understanding and discussing the database - **ER Diagram:** Attributes are represented by **ellipses** connected to their entity
design. rectangle by lines.
- **Foundation for Implementation:** Serves as a blueprint for creating a physical - **Types of Attributes:**
database schema.
- **Simple Attribute:** Cannot be divided further (e.g., Name).
- **Composite Attribute:** Can be divided into smaller attributes (e.g., Address can be
In the Entity-Relationship (ER) model, entities, attributes, entity sets, and relationships are divided into Street, City, and Zip Code).
fundamental components used to design and represent the structure of a database.
Here’s how each is represented and utilized:
- **Multi-valued Attribute:** Can hold multiple values (e.g., Phone Numbers). **Representation:**
**Example:** - **ER Diagram:** Represented by a rectangle (same as for entities) but often used in the
context of a set of entities.
- For an **Employee** entity, attributes might include **Employee_ID**, **Name**, and
**Date_of_Birth**. - **Example:** An entity set called **"Employees"** could include all instances of the
**Employee** entity.
**Example ER Diagram:**
**Example ER Diagram:**
+-------------+
+----------------+
| Employee |
| Employees |
+-------------+
+----------------+
|
4. **Relationships**
|
**Relationships** define how entities are related to each other. They describe the
+----------+ associations between entities.
| Emp_ID | **Representation:**
| +-------------+
| |
+-------------+ |
| Department | +-------------+
+-------------+ | Works_In |
Here’s how all these components might come together in a complete ER diagram: In the Entity-Relationship (ER) model, the **relationship set** and **constraints** are
important concepts that help define and manage the interactions between entities and
**Entities:** ensure the integrity of the database. Here's a detailed explanation of each:
| Employee | | Department | - **ER Diagram:** Represented by a **diamond** connected to the involved entity
rectangles. The diamond is labeled with the name of the relationship set.
+-------------+ +-------------+
**Example:**
| Emp_ID | | Dept_ID |
- Define restrictions on the attributes of entities and relationships, such as value ranges,
uniqueness, or format.
An attribute constraint might specify that **Employee_ID** must be unique and cannot In the context of database design and the Entity-Relationship (ER) model, the concepts of
be null. **subclasses** and **superclasses** are used to represent hierarchical relationships
between entities. These concepts are important for capturing and managing data that has
**ER Diagram Representation of Constraints:** a hierarchical or inheritance structure.
- Represented by symbols on the lines connecting entities to relationships. **Superclass** and **subclass** are terms borrowed from object-oriented programming
and are used in ER modeling to describe hierarchical relationships between entities.
- **One-to-One:** A line with a "1" on both ends.
**Superclass**
- **One-to-Many:** A line with "1" on one end and "M" on the other.
- **Definition:** A superclass is a general entity that represents a broader category or a
- **Many-to-Many:** A line with "M" on both ends. parent entity in a hierarchy.
**Participation Constraints:** - **Characteristics:** It has common attributes and relationships that apply to itself and
its subclasses.
- Indicated by annotations near the entity or relationship, such as total or partial
participation. - **Example:** In a university database, **Person** might be a superclass with attributes
that apply to both students and faculty members.
**Example ER Diagram with Constraints:**
**Example Attributes:**
+-------------+ +-------------+
- **Person:** Person_ID, Name, Date_of_Birth
| Employee | | Department |
**Subclass**
+-------------+ +-------------+
- **Definition:** A subclass is a more specific entity that inherits attributes and
| Emp_ID (PK) | | Dept_ID (PK)|
relationships from its superclass. It represents a subset of the superclass and may have
| Name | | Dept_Name | additional attributes or relationships.
+-------------+ +-------------+ - **Characteristics:** It inherits all attributes and relationships from the superclass but
can also have its own unique attributes and relationships.
| |
- **Example:** **Student** and **Faculty** might be subclasses of the **Person**
|1 N| superclass.
| Student | | Faculty | | Staff | - **Incomplete Subclasses:** Some instances of the superclass may not belong to any
subclass. For example, some persons might not be categorized as students or faculty
+-----------+ +-----------+ +-------------+ members.
| Student_ID| | Faculty_ID| | Staff_ID | A **database schema** is a blueprint or structure that defines how data is organized,
stored, and related within a database. It provides a formal description of the database’s
| Major | | Department| | Position | structure, including the tables, columns, data types, and the relationships between tables.
+-----------+ +-----------+ +-------------+ Here’s a detailed look at what a database schema encompasses:
3. **Rows:** - **`NOT NULL`**: Ensures that a column cannot have NULL values.
- **Definition:** Rows represent individual records or instances in a table. - **`UNIQUE`**: Ensures that all values in a column are unique.
- **Example:** A row in the `Employees` table might represent a specific employee with - **`CHECK`**: Ensures that values in a column meet a specific condition.
values for `Employee_ID`, `Name`, `Position`, and `Salary`.
- **`FOREIGN KEY`**: Ensures that a value in a column matches a value in the
4. **Primary Keys:** referenced table.
- **Definition:** A primary key is a unique identifier for each row in a table. 8. **Relationships:**
- **Purpose:** Ensures that each record can be uniquely identified and accessed. - **Definition:** Defines how tables are related to each other.
- **Definition:** A foreign key is a column or set of columns in one table that references - **One-to-Many (1:N)**
the primary key of another table.
- **Many-to-Many (M:N)**
- **Definition:** Describes the logical structure of the database. - **Emp_ID** (Primary Key)
- **Details:** Includes tables, columns, data types, and relationships without concern - **Name**
for how the data is physically stored.
- **Position**
- **Example:** The design of tables, relationships, and constraints.
- **Dept_ID** (Foreign Key referencing `Department` table)
3. **View Schema:**
- **Department Table:**
- **Definition:** Describes the different views of the data that users can access.
- **Dept_ID** (Primary Key)
- **Details:** Includes user-specific views, permissions, and derived tables.
- **Dept_Name**
- **Example:** Custom views or subsets of data that are presented to users based on
their role. 4. **Creating a Database Schema**
3. **ER Diagram and Schema Mapping** To create a database schema, follow these general steps:
ER diagrams (Entity-Relationship diagrams) are often used to design the logical schema of 1. **Identify Entities and Attributes:** Determine the entities (tables) and their attributes
a database. The ER diagram can be translated into a database schema by mapping entities (columns).
to tables, attributes to columns, and relationships to foreign keys.
2. **Define Relationships:** Identify how entities are related and define relationships
**ER Diagram Example:** (foreign keys).
+-------------+ +-------------+ 3. **Specify Constraints:** Apply constraints to ensure data integrity and consistency.
4. **Design Physical Storage:** Plan for physical storage and indexing if necessary.
- **Description:** Represents whole numbers. - **Length:** Variable length; storage size depends on the actual length of the string.
- **Range:** Depends on the system (e.g., `-2,147,483,648` to `2,147,483,647` for a - **Description:** Represents long text strings.
standard 4-byte integer).
- **Example:** `'This is a long text string that could span many lines.'`
- **Floating Point (`FLOAT`, `DOUBLE`, `REAL`)**
- **Length:** No fixed length; used for large amounts of text.
- **Description:** Represents numbers with fractional parts.
**1.3. Date and Time Data Types**
- **Example:** `3.14`, `-0.001`
- **Date (`DATE`)**
- **Precision:** Varies; `FLOAT` has less precision than `DOUBLE`.
- **Description:** Represents calendar dates.
- **Decimal (`DECIMAL`, `NUMERIC`)**
- **Example:** `2024-07-23`
- **Description:** Represents exact numeric values with a fixed number of decimal
places. - **Format:** Typically stored in `YYYY-MM-DD` format.
- **Precision:** Defined by the user (e.g., `DECIMAL(10,2)` for a total of 10 digits, with 2 - **Description:** Represents time of day.
after the decimal point).
- **Example:** `14:30:00`
**1.2. Character and String Data Types**
- **Format:** Typically stored in `HH:MM:SS` format.
- **Usage:** Varies by database system. - **Description:** Represents data in JavaScript Object Notation format.
- **Boolean (`BOOLEAN`, `BOOL`)** - **Usage:** For storing and querying JSON data.
- **Example:** `TRUE`, `FALSE` - **Description:** Represents data in Extensible Markup Language format.
2. **Complex Data Types** - **Usage:** For storing and querying XML data.
- **Binary (`BINARY`)** Different database management systems (DBMS) may have their own data types or
variations. Some common DBMS-specific types include:
- **Description:** Represents fixed-length binary data.
- **Oracle:**
- **Example:** `0x4F`, `0x1234`
- `NUMBER` (numeric values)
- **Length:** Fixed length.
- `CLOB` (Character Large Object)
- **Varbinary (`VARBINARY`)**
- `RAW` (binary data)
- **Description:** Represents variable-length binary data.
- **MySQL:**
- **Example:** `0xDEADBEEF`
**Data Definition Language (DDL)** operations are used to define and manage the **Creating an Index**
structure of a database. These operations allow you to create, modify, and remove
database objects like tables, indexes, and schemas. Here’s a breakdown of the main DDL **Syntax:**
operations: `CREATE`, `ALTER`, `DROP`, and `RENAME`, along with their syntax and
examples. ```sql
The `CREATE` statement is used to create new database objects such as tables, indexes, ON table_name (column1, column2, ...);
views, and schemas.
**Example:**
**Creating a Table**
```sql
**Syntax:**
CREATE INDEX idx_name ON Employees (Name);
```sql
2. **ALTER**
CREATE TABLE table_name (
The `ALTER` statement is used to modify the structure of an existing database object. You
column1 datatype [constraints], can add, modify, or drop columns in a table, or change other properties.
); **Syntax:**
**Example:** ```sql
**Example:** 3. **DROP**
```sql The `DROP` statement is used to delete existing database objects. Be cautious with
`DROP` as it will remove the object and all its data.
ALTER TABLE Employees
**Dropping a Table**
ADD Department_ID INT;
**Syntax:**
#### **Modifying a Column**
```sql
**Syntax:**
DROP TABLE table_name;
```sql
**Example:**
ALTER TABLE table_name
```sql
MODIFY COLUMN column_name datatype [constraints];
DROP TABLE Employees;
**Example:**
**Dropping an Index**
```sql
**Syntax:**
ALTER TABLE Employees
```sql
MODIFY COLUMN Salary DECIMAL(12, 2);
DROP INDEX index_name;
**Dropping a Column**
**Example:**
**Syntax:**
```sql
```sql
DROP INDEX idx_name;
ALTER TABLE table_name
4. **RENAME**
DROP COLUMN column_name;
The `RENAME` statement is used to change the name of a database object. Not all
**Example:** database systems support `RENAME` operations in the same way, but here’s a general
syntax:
```sql
**Renaming a Table**
ALTER TABLE Employees
```sql **Data Manipulation Language (DML)** : operations are used to manage and
manipulate the data stored in a database. These operations allow you to add, modify, and
ALTER TABLE Employees remove data within the database tables. The primary DML operations are `INSERT`,
`DELETE`, and `UPDATE`. Here’s a detailed look at each operation, including syntax and
RENAME TO Staff; examples.
**Renaming a Column** (Note: Some databases require a different syntax or support 1. **INSERT**
`RENAME COLUMN` directly)
The `INSERT` statement is used to add new records into a table.
**Syntax:**
**Syntax:**
```sql
```sql
ALTER TABLE table_name
INSERT INTO table_name (column1, column2, ...)
RENAME COLUMN old_column_name TO new_column_name;
VALUES (value1, value2, ...);
**Example:**
**Example:**
```sql
```sql
ALTER TABLE Staff
INSERT INTO Employees (Employee_ID, Name, Position, Salary, Hire_Date)
RENAME COLUMN Position TO Job_Title;
VALUES (1, 'John Doe', 'Software Engineer', 75000.00, '2024-07-23');
### Summary
**Inserting Multiple Rows:**
- **CREATE:** Used to create new database objects.
**Syntax:**
- **Example:** `CREATE TABLE Employees (...);`
```sql
- **ALTER:** Used to modify existing database objects.
INSERT INTO table_name (column1, column2, ...)
INSERT INTO Employees (Employee_ID, Name, Position, Salary, Hire_Date) **Important:** Omitting the `WHERE` clause will delete all records from the table.
**Example:** ```sql
SELECT Employee_ID, Name, Position The `UPDATE` statement is used to modify existing records in a table.
The `DELETE` statement is used to remove existing records from a table. SET column1 = value1, column2 = value2, ...
SELECT * **Examples:**
**Example:** **Example:**
```sql ```sql
**Projection** refers to selecting specific columns from a table, which is essentially what This query retrieves the `Name` and `Position` columns for employees whose salary is
the `SELECT` statement does. greater than 70,000.
```sql You can use logical operators such as `AND`, `OR`, and `NOT` to combine multiple
conditions.
SELECT Name, Salary
**Example:**
FROM Employees;
```sql
This query projects or selects only the `Name` and `Salary` columns from the `Employees`
table. SELECT Name, Position, Salary
The `WHERE` clause is used to filter records based on specific conditions. It restricts the WHERE Salary > 70000 AND Position = 'Software Engineer';
rows returned by the `SELECT` statement.
This query retrieves the `Name`, `Position`, and `Salary` columns for employees who are
**Basic Syntax:** `Software Engineers` and whose salary is greater than 70,000.
SELECT column1, column2, ... The `LIKE` operator is used for pattern matching with wildcard characters.
**5. Checking for Null Values:** - **Description:** Subtracts one number from another.
The `IS NULL` and `IS NOT NULL` operators are used to check for NULL values. - **Example:**
**Example:** ```sql
- **Example:**
- **Example:** ```sql
FROM Employees; WHERE Salary > 70000 AND Position = 'Software Engineer';
- **Description:** Returns the remainder of a division operation. - **Description:** Returns TRUE if at least one condition is TRUE.
- **Example:** - **Syntax:**
```sql ```sql
Logical operations are used to compare values and return boolean results (TRUE, FALSE, - **Example:**
or NULL). These operations are commonly used in the `WHERE` clause to filter records
based on multiple conditions. ```sql
- **Description:** Returns TRUE if all conditions are TRUE. WHERE Salary > 70000 OR Position = 'Software Engineer';
- **Syntax:** - **NOT**
- **Syntax:** - **IN**
- **Description:** Returns TRUE if a value is within a specified range. WHERE Position IN ('Software Engineer', 'Data Analyst');
- **Syntax:** - **LIKE**
WHERE Name LIKE 'J%'; -- Names starting with 'J' FROM table_name
- **Syntax:** ```sql
FROM table_name This query counts the total number of rows in the `Employees` table.
```sql - **Syntax:**
Aggregation in SQL refers to the process of summarizing or grouping data to provide WHERE condition;
meaningful insights and statistics. Aggregation functions operate on a set of values and
return a single value that summarizes or represents the data. Common aggregation - **Example:**
functions include `COUNT`, `SUM`, `AVG`, `MAX`, and `MIN`.
```sql
**Common Aggregation Functions**
SELECT SUM(Salary) AS TotalSalary
1. **COUNT**
FROM Employees;
- **Description:** Returns the number of rows that match a specified condition.
This query calculates the total salary paid to all employees.
- **Syntax:**
3. **AVG**
- **Syntax:** 5. **MIN**
This query calculates the average salary of all employees. SELECT MIN(Salary) AS LowestSalary
```sql
FROM table_name Aggregation functions are often used with the `GROUP BY` clause to group rows that have
the same values in specified columns and then perform aggregation on these groups.
WHERE condition;
**Syntax:**
- **Example:**
```sql
```sql
SELECT column1, AGGREGATE_FUNCTION(column2)
SELECT MAX(Salary) AS HighestSalary
FROM table_name
FROM Employees;
This query calculates the average salary for each department. HAVING AVG(Salary) > 70000;
**2. Counting Employees in Each Position:** This query returns departments where the average salary is greater than 70,000.
```sql
FROM Employees
GROUP BY Position; **Grouping** in SQL is a way to aggregate and summarize data by grouping rows that
have the same values in specified columns. The `GROUP BY` clause is used to arrange
This query counts the number of employees for each job position. identical data into groups. This allows you to perform aggregation functions on each
group of data.
**Using Aggregation Functions with HAVING**
**Using GROUP BY**
The `HAVING` clause is used to filter groups of rows after aggregation has been
performed. It is similar to the `WHERE` clause but operates on grouped data. The `GROUP BY` clause is used in conjunction with aggregation functions like `SUM`,
`COUNT`, `AVG`, `MAX`, and `MIN` to produce summary results for each group.
**Syntax:**
**Basic Syntax:**
```sql
```sql
SELECT column1, AGGREGATE_FUNCTION(column2)
SELECT column1, AGGREGATE_FUNCTION(column2)
FROM table_name
FROM table_name
GROUP BY Department_ID; This query groups the rows by both `Department_ID` and `Position` and calculates the
average salary for each combination of department and position.
This query groups the rows in the `Employees` table by `Department_ID` and counts the
number of employees in each department. **HAVING Clause**
**2. Average Salary by Position** The `HAVING` clause is used to filter groups based on aggregate function results. It is
similar to the `WHERE` clause but is used with grouped data.
```sql
**Syntax:**
SELECT Position, AVG(Salary) AS AverageSalary
```sql
FROM Employees
SELECT column1, AGGREGATE_FUNCTION(column2)
GROUP BY Position;
FROM table_name
This query groups the rows by `Position` and calculates the average salary for each
position. GROUP BY column1
You can group by more than one column to create more granular groups. **Example:**
```sql ```sql
This query groups the rows by `Department_ID` and returns departments that have more GROUP BY Department_ID
than 10 employees.
HAVING SUM(Salary) > 50000
**2. Average Salary Greater Than 70000 by Department**
ORDER BY TotalSalary DESC;
```sql
This query groups rows by `Department_ID`, calculates the total salary for each
SELECT Department_ID, AVG(Salary) AS AverageSalary department, filters out departments where the total salary is 50,000 or less, and orders
the results by total salary in descending order.
FROM Employees
**Ordering** in SQL is used to sort the results of a query based on one or more columns.
GROUP BY Department_ID The `ORDER BY` clause specifies the column(s) to sort the results and the direction of the
sort (ascending or descending). This is useful for presenting data in a meaningful way,
HAVING AVG(Salary) > 70000; such as sorting by salary to find the highest or lowest earners or arranging dates
chronologically.
This query groups the rows by `Department_ID` and returns departments where the
average salary is greater than 70,000. **Using ORDER BY**
**Order of SQL Clauses** The `ORDER BY` clause is used to sort the query results.
The typical order of SQL clauses is: **Basic Syntax:**
1. **SELECT:** Specifies the columns to be returned. ```sql
2. **FROM:** Specifies the tables from which to retrieve the data. SELECT column1, column2, ...
3. **WHERE:** Filters rows before grouping. FROM table_name
4. **GROUP BY:** Groups rows into summary rows. ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
5. **HAVING:** Filters groups after aggregation. - **`ASC` (Ascending)**: Sorts the data from the smallest to the largest value (default).
6. **ORDER BY:** Orders the results. - **`DESC` (Descending)**: Sorts the data from the largest to the smallest value.
**Example with Full SQL Query:** **Examples**
**1. Total Salary by Department, Ordered by Total Salary** **1. Sorting in Ascending Order**
```sql **Syntax:**
SELECT Department_ID, SUM(Salary) AS TotalSalary
SELECT column1, column2 This query retrieves the `Name`, `Department_ID`, and `Salary` columns and sorts the
results first by `Department_ID` in ascending order and then by `Salary` in descending
FROM table_name order within each department.
**Example:** You can use `ORDER BY` with aggregate functions to sort grouped results.
```sql **Example:**
This query groups the results by `Department_ID`, calculates the average salary for each **Example:**
department, and sorts the results by `AverageSalary` in descending order.
```sql
**ORDER BY with NULL Values**
SELECT Name, Salary
When sorting, NULL values are treated as the lowest possible values by default.
FROM Employees
**Sorting with NULLs First:**
ORDER BY Salary ASC NULLS LAST;
**Syntax:**
This query sorts the `Salary` column in ascending order, with NULL values appearing last.
```sql
SELECT column1
FROM table_name
**Example:**
```sql
FROM Employees
This query sorts the `Salary` column in ascending order, with NULL values appearing first.
**Syntax:**