RDBMS Important 5^010 Marks Unit Wise
RDBMS Important 5^010 Marks Unit Wise
An Entity-Relationship (ER) Diagram is a tool used to model the data structure of a system
by visually representing entities, their attributes, and relationships. The main building blocks
of an ER Diagram are:
1. Entities: These represent objects or concepts that hold data. Entities are usually depicted as
rectangles and could represent people, places, things, or events relevant to the system. Each
entity has a unique identifier known as a primary key.
3. Relationships: These define how entities are related to each other. Relationships are
represented by diamonds and connect entities, showing their association. Relationships have
degrees (like binary or ternary) and cardinalities that specify the number of entities involved
(e.g., one-to-one, one-to-many, many-to-many).
4. Cardinality: Cardinality defines the number of instances of one entity that can be
associated with instances of another entity. It determines whether a relationship is one-to-one
(1:1), one-to-many (1:N), or many-to-many (M:N).
5. Primary Key: A unique identifier for an entity that ensures each record can be distinguished
from others. It is typically underlined in the ER Diagram.
6. Foreign Key: A field in one entity that links to the primary key of another entity,
establishing a relationship between the two entities.
ER DIAGRAM
2B) Explain E-R model with an example
o ER model stands for an Entity-Relationship model. It is a high-level data model. This
model is used to define the data elements and relationship for a specified system.
o It develops a conceptual design for the database. It also develops a very simple and easy to
design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an entity-
relationship diagram.
Components
ER diagram basically having three components:
• Entities − It is a real-world thing which can be a person, place, or even a concept. For
Example: Department, Admin, Courses, Teachers, Students, Building, etc are some of the
entities of a School Management System.
• Attributes − An entity which contains a real-world property called an attribute. For
Example: The entity employee has the property like employee id, salary, age, etc.
• Relationship − Relationship tells how two attributes are related. For Example: Employee
works for a department.
Database Architecture
Database architecture refers to the design and structure of a database system, including how
data is stored, accessed, and managed. It defines the system's components, their relationships,
and the processes that govern how they interact. There are different types of database
architectures, each suited for different needs and scenarios. The most commonly used
architectures are:
Definition:
In a one-tier architecture, the database and the user interface (UI) exist on the same system.
The application, database management system (DBMS), and data storage are all handled on a
single platform.
Advantages:
- Simplicity: This architecture is straightforward and easy to implement.
- Less Overhead: There's no need for complex communication between client and server, as
everything is on the same machine.
- Cost-Effective: Because the system is less complex, the hardware and maintenance costs are
low.
Disadvantages:
- Scalability Issues: As the user base grows, the system becomes less efficient. There's limited
ability to handle large numbers of users or large datasets.
- Limited Performance: The system’s performance might degrade as it has to manage both the
application and the database on a single machine.
- Lack of Flexibility: A single-tier architecture is less flexible, as all processes are tightly
coupled.
2. Two-Tier Architecture
Definition:
In two-tier architecture, the database and the application are separated. The client (user
interface) communicates directly with the database server, which handles the DBMS.
Components:
- Client Tier: This is the user interface where users interact with the application.
- Database Server Tier: This layer is responsible for managing the database, handling queries,
and storing data.
Advantages:
- Improved Performance: Since the application and database are separate, they can be
optimized individually for performance.
- Better Scalability: The architecture can scale better than one-tier systems, as the server can
be upgraded independently of the client.
Disadvantages:
- Limited Scalability: While better than one-tier, two-tier architecture may still struggle with
handling a large number of clients simultaneously.
- Security Risks: Direct communication between the client and the database server can
expose sensitive data to potential security threats.
3. Three-Tier Architecture
Definition:
Three-tier architecture involves three separate layers: the presentation layer, the logic layer,
and the data layer. This is one of the most common architectures used in modern web
applications.
Components:
1. Presentation Layer: This is the client-side user interface (UI), such as a web or mobile app,
that users interact with.
2. Logic Layer: This middle layer contains the business logic or application logic, usually
implemented as a web server or an application server.
3. Data Layer: The data layer is where the DBMS resides, responsible for storing and
retrieving data.
Advantages:
- Scalability: Each layer can be scaled independently. For example, you can add more
application servers or database servers as needed.
- Flexibility: The logic layer separates the business logic from the UI and database, making it
easier to modify and update without affecting the entire system.
- Security: Communication between the client and database is mediated through the
application layer, providing an additional layer of security.
- Maintainability: Changes in one layer (e.g., UI or business logic) can be made
independently without disturbing other layers.
Disadvantages:
- Complexity: With three separate layers, the architecture becomes more complex, requiring
more careful planning and management.
- Latency: The additional layer introduces more communication steps, which could lead to
increased latency in data processing and response times.
- Higher Costs: The increased infrastructure, maintenance, and development costs make this
architecture more expensive than simpler ones.
4. Multi-Tier Architecture
Definition:
N-tier architecture is an extension of the three-tier architecture, where the system is divided
into multiple layers, each serving a specific purpose. Typically used for large-scale enterprise
applications, it can have many more than three layers, each performing specialized functions
like caching, business logic, and transaction management.
Advantages:
- High Scalability: Each tier can be independently scaled to handle large amounts of data,
requests, and users.
- Modularity: Allows greater flexibility in modifying individual layers without disrupting the
entire system.
- Fault Tolerance: Failure in one tier does not necessarily affect the entire system, improving
the system’s reliability.
Disadvantages:
- Very Complex: Managing multiple layers requires a high level of expertise and resources.
- Higher Latency: The more tiers there are, the longer it takes to process a request as it moves
through all the layers.
- Costly: Building and maintaining a multi-tier architecture is expensive in terms of hardware,
software, and personnel.
1. Database Engine
Definition:
The Database Engine is the core component of the DBMS that handles the storage, retrieval,
and updating of data. It processes requests from applications or users and interacts with the
database's storage to perform various operations such as querying, inserting, updating, and
deleting data.
Functions:
- Data Storage Management: It organizes how data is stored in the database and manages
access to the physical storage.
- Query Processing: The engine interprets and executes SQL queries to retrieve or manipulate
data.
- Transaction Management: Ensures that database operations are executed correctly and that
they are atomic, consistent, isolated, and durable (ACID properties).
Example:
In relational DBMS systems, popular database engines include MySQL, PostgreSQL, Oracle,
and SQL Server.
Definition:
The Query Processor translates SQL queries into low-level commands that the database
engine can execute. The query processor includes several stages: parsing, optimization, and
execution.
Functions:
- Parsing: Analyzes SQL queries for syntax and checks them for correctness.
- Query Optimization: Determines the most efficient way to execute a query (e.g., choosing
the best index or join strategy).
- Execution: Executes the query by interacting with the storage engine to fetch or modify
data.
Example:
When a user submits a SQL query, the query processor parses it, checks for errors, optimizes
the execution plan, and retrieves the requested data.
3. Data Dictionary (Metadata)
Definition:
The Data Dictionary (or Metadata Repository) is a centralized collection of metadata—data
about the data. It stores definitions and properties of the database's objects such as tables,
columns, indexes, and relationships.
Functions:
- Cataloging Database Objects: Contains descriptions of all the database objects, including
table definitions, field types, constraints, and indexes.
- Access Control: It may store information about user privileges and access control.
- Tracking Changes: Keeps track of changes to the database schema over time, such as the
creation of new tables or modifications to existing ones.
Example:
The metadata for a Customer table might include information such as column names
("CustomerID," "FirstName," "LastName"), data types (integer, varchar), and any constraints
(e.g., CustomerID as a primary key).
Definition:
The Data Storage Management component is responsible for managing the physical storage
of data. It involves how data is stored on disk, organized, and retrieved in an efficient manner.
Functions:
- File Management: Determines how data is physically stored, usually in data files on a disk.
- Indexing: Helps improve the speed of data retrieval by creating and maintaining indexes on
frequently queried fields.
- Buffer Management: Uses memory buffers to temporarily store data retrieved from the disk
to reduce the number of disk I/O operations.
- Data Compression: May involve techniques to reduce the physical storage requirements by
compressing the data.
Example:
In a relational DBMS, data for each table is stored in a file, and indexes may be created on
key columns (e.g., an index on the CustomerID in a Customers table).
Advantages of DBMS
A Database Management System (DBMS) offers several key benefits over traditional file-
based systems. Below are the primary advantages of using a DBMS:
1. Data Redundancy Control
- Explanation: DBMS minimizes data redundancy (duplication of data) by storing data
centrally. This ensures that the same data is not repeated across multiple files.
- Benefit: Reduces storage space, improves data integrity, and avoids inconsistencies.
4. Data Independence
- Explanation: DBMS separates the data from application programs, meaning that changes
in the data structure do not affect the application programs.
- Benefit: Enables flexibility and simplifies application development and maintenance.
6. Concurrent Access
- Explanation: DBMS allows multiple users to access the database simultaneously while
ensuring that data remains consistent and transactions are isolated.
- Benefit: Supports multi-user environments and enhances collaboration without causing
data conflicts.
Unit -2
1.Structure of a Relational Database
A relational database is structured around tables, which consist of rows and columns. The key
elements in a relational database structure include:
1. Tables (Relations)
- Definition: A table is the fundamental unit of data storage in a relational database. It stores
data in rows (also called records or tuples) and columns (attributes or fields).
- Structure:
- Rows: Each row represents a unique record or instance of an entity (e.g., a row in the
Customers table represents a single customer).
- Columns: Each column represents a specific attribute of the data (e.g., the column
CustomerName represents the name of a customer).
- Example: A Customers table might have columns like `CustomerID`, `FirstName`,
`LastName`, and `Email`.
- Definition: Attributes are the properties or characteristics of an entity that are represented as
columns in a table.
- Structure:
- Each attribute (column) has a defined data type (e.g., `INTEGER`, `VARCHAR`,
`DATE`).
- Attributes must follow constraints like NOT NULL, UNIQUE, or DEFAULT.
- Structure:
- A tuple is a set of values for the attributes (columns).
- Each row has a unique combination of values for the columns and represents a specific
data entry.
4. Primary Key
- Definition: A Primary Key is a column or set of columns that uniquely identifies each row
in a table. No two rows can have the same value for the primary key.
5. Foreign Key
- Definition: A Foreign Key is a column or set of columns that establishes a link between the
data in two tables. It references the primary key of another table.
- Purpose: Maintains referential integrity between related tables by ensuring that the foreign
key value in one table matches a primary key value in another.
- Example: In an Orders table, the column `CustomerID` might be a foreign key referencing
the `CustomerID` in the Customers table.
6. Indexes
- Definition: An index is a database object that improves the speed of data retrieval operations
on a table.
- Purpose: Indexes allow for quick searches and retrieval by creating a mapping to the data
rows based on specified columns.
- Example: An index on the `ProductID` column in a Products table allows faster retrieval of
product data based on the product ID.
7. Constraints
- Definition: Constraints are rules applied to the columns in a table to enforce data integrity
and consistency.
- Types:
- NOT NULL: Ensures that a column cannot have a NULL value.
- UNIQUE: Ensures that all values in a column are distinct.
- CHECK: Ensures that the values in a column meet a specific condition.
- DEFAULT: Specifies a default value for a column when no value is provided.
Relational algebra is a formal language used to query and manipulate relational databases. It
consists of a set of operations that take one or two relations as input and produce a new
relation as output. These operations are fundamental to the functionality of a Relational
Database Management System (RDBMS). The main relational algebra operations include:
1. Select (σ)
- Purpose: The Select operation is used to retrieve rows (tuples) from a relation that satisfy a
given condition.
- Syntax:
σ_condition(Relation)
- Description: Selects the rows from the input relation that match the specified condition
(predicate). The result is a subset of the original relation with rows that satisfy the condition.
- Example:
If we have a Students table, we can select students who are older than 20:
- Result: This returns all rows from Students where `age > 20`.
2. Project (π)
- Purpose: The Project operation is used to retrieve specific columns (attributes) from a
relation, essentially "filtering" the columns.
- Syntax:
π_column1, column2,...(Relation)
- Description: Projects a relation onto a subset of its attributes (columns). The result is a
relation with only the specified columns and without duplicates.
- Example:
If we want to see only the names and ages of students in the Students table:
π_name, age(Students)
- Result: This returns a relation with just the `name` and `age` columns from the Students
table.
3. Union (∪)
- Purpose: The Union operation combines the results of two relations, removing duplicate
rows.
- Syntax:
Relation1 ∪ Relation2
- Description: The union operation combines the tuples of two relations that are union-
compatible (same number and type of attributes). The result includes all tuples from both
relations without duplication.
- Example:
If we have two relations, TableA and TableB, with similar columns:
TableA ∪ TableB
- Result: This returns a relation with all unique tuples from both TableA and TableB.
- Purpose: The Set Difference operation returns the rows that are in one relation but not in
another.
- Syntax:
Relation1 − Relation2
- Description: The set difference returns tuples from the first relation that are not present in
the second relation. The two relations must be union-compatible.
- Example:
If we have two relations, Students and Graduates, we can find the students who have not
graduated:
Students − Graduates
- Result: This returns a relation with all students who are not in the Graduates table.
5. Intersection (∩)
- Purpose: The Intersection operation returns the common tuples between two relations.
- Syntax:
Relation1 ∩ Relation2
- Description: The intersection operation returns the set of tuples that appear in both relations.
The two relations must be union-compatible.
- Example:
If we have two relations, A and B, the intersection of these relations is:
A∩B
- Result: This returns the tuples that are common to both relations A and B.
6. Join (⨝)
- Purpose: The Join operation combines tuples from two relations based on a common
attribute (usually a key).
- Types of Joins:
- Theta Join (⨝_θ): Combines tuples based on a specified condition (θ), such as equality.
- Equi Join (⨝_=): A special case of theta join where the condition is equality.
- Natural Join (⨝): Automatically joins relations on all common attributes.
- Syntax:
- Description: The join operation pairs rows from two relations based on a join condition,
typically involving equality on common attributes. The result is a single relation that
combines the matched rows.
- Example:
For a Customers and Orders table, we might use a natural join on `CustomerID`:
Customers ⨝ Orders
- Result: This returns a relation combining data from both Customers and Orders based on
matching `CustomerID`.
Dr. E.F. Codd introduced the Relational Model for databases in 1970 and proposed a set of
rules (also known as Codd's Twelve Rules) that define what constitutes a true relational
database system. These rules aim to ensure that the database management system (DBMS)
adheres to the principles of the relational model, promoting data integrity, consistency, and
ease of use.
1. Information Rule
- Description: All information in a relational database should be represented in tables
(relations), and each piece of data should be stored as a value within a table’s cells.
- Implication: This means that data must be presented as tuples (rows) and attributes
(columns). Each column must hold atomic (indivisible) values.
- Example: Data like customer names, ages, and orders should be stored in tables where each
row represents a single entity (e.g., a customer) and each column contains one attribute (e.g.,
name, age).
- Example: To retrieve a customer's name, you would use the `CustomerID` (primary key)
and column name `CustomerName` to find the specific value in the Customers table.
- Implication: Null values must be treated as distinct from actual data values and should not
interfere with query results. There should be a clear distinction between unknown, not
applicable, and missing data.
- Example: A `null` value in a Customers table's `PhoneNumber` column would indicate that
a phone number has not been provided, not that the customer’s phone number is zero.
- Implication: Changes to the data should be performed easily while preserving relationships
between data in different tables, ensuring no inconsistency is introduced.
- Example: When a customer's address changes, the system should allow an update to the
address without disrupting other parts of the database (such as customer order records).
- Implication: The data language must support a full range of relational operations to manage
and manipulate data. It should allow users to express their requirements in a structured
manner.
- Example: SQL (Structured Query Language) allows for commands like `SELECT`,
`INSERT`, `UPDATE`, `DELETE`, and `ALTER` for data manipulation and schema
definition.
- Implication: Views are used to simplify complex queries and can act as a layer between the
user and the database. The DBMS must be able to allow updates to be reflected in the base
tables for certain types of views.
- Example: A view that displays customer names and order details should allow an update to
the `Orders` table when a change is made to the view, like updating the order amount.
- Implication: You should be able to perform operations on a set of data in a single command,
improving efficiency.
- Example: SQL allows inserting multiple rows into a table with a single `INSERT`
statement, updating multiple rows with a single `UPDATE` statement, and deleting multiple
rows with a single `DELETE` statement.
- Implication: This ensures flexibility and scalability by allowing changes to the database's
physical storage without disrupting its logical structure.
- Example: A database should be able to switch from one storage format or hardware system
to another without requiring changes to the tables or queries.
- Implication: This ensures that applications remain stable even when the database schema
evolves over time.
- Example: Adding a new column to a table (e.g., `PhoneNumber`) should not affect existing
queries that do not reference that column.
10. Integrity Independence
- Description: The DBMS should support integrity constraints that are stored in the system
catalog, ensuring that data integrity rules (like primary keys, foreign keys, and check
constraints) are enforced automatically by the system.
- Implication: The system should ensure the consistency of data through integrity constraints
without needing user intervention.
- Example: A foreign key constraint ensures that a `CustomerID` in an Orders table must exist
in the Customers table.
- Implication: This rule ensures that users can access the data seamlessly, regardless of
whether it is stored in one or many locations.
- Example: A distributed database system should allow queries to pull data from multiple
servers transparently.
- Implication: Lower-level access should not compromise the relational model or data
integrity.
- Example: Even if the DBMS allows direct access to records through programming
languages, it should still enforce relational integrity rules like foreign key constraints.
Query By Example (QBE) is a simple and visual way to interact with relational databases. It
allows users to create queries by providing examples of the data they want, rather than
writing complex commands like in SQL. QBE is designed to be easy for non-technical users
to use.
How QBE Works
1. Select the Table: In QBE, the user starts by selecting the table they want to query.
2. Fill in Example Data: The user enters examples of the data they want to retrieve. Instead of
writing detailed commands, users just provide examples in a table format (grid).
3. Query Execution: Once the user fills out the table with examples, the system automatically
turns this into a database query and fetches the results.
- Easy to Use: Users can create queries by filling in examples, without needing to know SQL
or any other complex query language.
- Visual Interface: QBE uses a table/grid format, making it easy to see and work with the
data.
- No Need for Syntax: Users don’t need to worry about query syntax, making it less error-
prone.
Example of QBE
If you want to find employees in the HR department with a salary greater than 50,000, you
would:
This example is telling the system, "Show me employees from the HR department whose
salary is greater than 50,000."
The system would automatically convert this example into a SQL query like:
```sql
SELECT EmployeeID, FirstName, LastName, Department, Salary
FROM Employees
WHERE Department = 'HR' AND Salary > 50000;
```
Advantages of QBE
1. User-Friendly: QBE is very easy for people who don't know SQL to use. It provides a
simple interface where you just fill in examples.
2. Reduces Errors: Since users don’t write SQL queries manually, there’s less chance of
mistakes.
3. Quick Query Creation: Users can create queries quickly by just entering values in a grid.
Disadvantages of QBE
1. Not Flexible for Complex Queries: While QBE is great for simple queries, it can’t handle
very complex ones, like nested queries or advanced calculations.
2. Limited Support: Not all database systems support QBE, and it may not be available in
large or complex applications.
3. Learning Curve for Complex Queries: For more complex queries (involving joins or
conditions), users may still need to understand how to structure their examples correctly.
A transaction in DBMS passes through several states during its execution. These states define
the progress of a transaction and its interaction with the database system.
UNIT -3
1. New State:
- When a transaction is first initiated, it is in the New state.
- This is the initial state where the transaction has not yet started execution.
2. Active State:
- Once a transaction starts executing, it moves to the Active state.
- In this state, the transaction is performing its operations on the database (like reading or
writing data).
- It continues to execute until either all operations are complete or it reaches a point where
it needs to be aborted or committed.
4. Committed State:
- Once a transaction has been successfully executed and committed, it enters the Committed
state.
- In this state, the transaction is permanent, and its changes to the database are made
persistent.
- The transaction is now complete, and the changes are recorded in the database
permanently.
5. Failed State:
- If a transaction encounters an error (such as a system crash or violation of a constraint), it
enters the Failed state.
- A failed transaction cannot proceed, and it needs to be rolled back (undone).
6. Terminated State:
- After a transaction either commits or rolls back, it enters the Terminated state.
- In this state, the transaction has either successfully completed and committed its changes
or has been rolled back and its changes undone.
- This is the final state for a transaction.
1. New: The transaction has been created but hasn’t started yet.
2. Active: The transaction is performing operations on the database (reading, writing, etc.).
3. Partially Committed: The transaction has executed all operations but hasn’t been finalized.
4. Committed: The transaction is successfully completed and all changes are permanent.
5. Failed: The transaction encounters an error (like system failure or violation of constraints),
and needs to be aborted.
6. Terminated: The transaction has ended, either because it has been committed or rolled
back.
Database Security
1. Physical Security: Protects database hardware from threats like theft or fire.
2. Access Control: Restricts database access based on user roles and permissions.
3. Data Encryption: Secures data by making it unreadable without the correct decryption key,
both at rest and in transit.
4. Backup and Recovery: Regular backups ensure data can be restored after corruption or
loss.
5. Audit and Monitoring: Tracks and logs database activities to detect unauthorized access.
6. Database Firewalls: Protects against attacks like SQL injection.
7. Role-Based Access Control (RBAC): Assigns specific access permissions based on user
roles.
3. Normalization in DBMS
Normalization is the process of organizing the data in a database to reduce redundancy and
improve data integrity. The goal is to ensure that the database is structured efficiently, so that
it is easy to maintain, update, and query.
Normalization involves dividing large tables into smaller ones and defining relationships
between them, which helps in minimizing duplicate data. It is done by applying a set of rules
known as normal forms.
Example:
A table storing student information might have a column for courses, but if multiple courses
are listed in a single column, this violates 1NF.
Before 1NF:
| StudentID | Name | Courses |
|1 | John | Math, English |
|2 | Jane | History, Art |
Example:
A table that stores order details where both the order number and product ID are the
primary key.
Before 2NF:
| OrderID | ProductID | ProductName | Quantity |
| 101 | A | Apple | 10 |
| 101 | B | Banana |5 |
Products:
| ProductID | ProductName |
|A | Apple |
|B | Banana |
Example:
In a table that stores employee information, if the department manager is dependent on the
department, it creates a transitive dependency.
Before 3NF:
| EmployeeID | Name | Department | Manager |
| 101 | John | HR | Alice |
| 102 | Jane | IT | Bob |
Employees:
| EmployeeID | Name | Department |
| 101 | John | HR |
| 102 | Jane | IT |
Departments:
| Department | Manager |
| HR | Alice |
| IT | Bob |
Benefits of Normalization
1. Reduces Data Redundancy: Eliminates duplicate data across tables, reducing storage
requirements.
2. Improves Data Integrity: Ensures that data is stored logically, reducing errors and
inconsistencies.
3. Enhances Query Performance: Reduces unnecessary data storage and improves database
efficiency.
In other words, a functional dependency occurs when a value of one attribute uniquely
determines the value of another attribute.
Definition
If for a given set of attributes \( X \) and \( Y \), \( X \) uniquely determines \( Y \), we write it
as:
\[
X \right arrow Y\]
This means that if two rows of the table have the same value for \( X \), they must also have
the same value for \( Y \).
In this table, we have several attributes: StudentID, StudentName, Course, and Instructor.
Example 1:
- StudentID → StudentName:
- This means that the StudentID uniquely determines the StudentName.
- If you know a StudentID, you can uniquely identify the StudentName.
- In the table, for StudentID 101, the corresponding StudentName is John. For StudentID
103, it is John as well. Hence, the StudentID determines the StudentName.
Example 2:
- Course → Instructor:
- This means that a specific Course uniquely determines the Instructor.
- For example, Math is taught by Mr. A, English is taught by Mr. B, and Science is taught by
Mr. A.
- So, if you know the Course, you can determine the Instructor.
Key Points
1. One-to-One Relationship: When one attribute (or set of attributes) uniquely determines
another.
2. Transitive Dependency: If X → Y and Y → Z, then X → Z. This is an example of a
transitive dependency, where an indirect dependency exists.
3. Partial Dependency: In composite keys, if a non-key attribute is dependent on only a part
of the primary key, it is a partial dependency.
UNIT -4
1.Discuss in detail about Aggregate Functions in SQL.
Aggregate Functions in SQL
Aggregate functions in SQL are used to perform calculations on a set of values and return a
single value as the result. These functions are typically used in conjunction with the GROUP
BY clause to group rows that have the same values into summary rows.
The aggregate functions allow you to perform operations such as counting, summing,
averaging, and finding the minimum or maximum value in a group of rows.
1. COUNT()
- Purpose: Counts the number of rows or non-NULL values in a column.
- Syntax:
```sql
SELECT COUNT(column_name) FROM table_name;
```
- Example:
```sql
SELECT COUNT(*) FROM Employees;
```
This counts the total number of rows in the `Employees` table.
```sql
SELECT COUNT(DISTINCT department) FROM Employees;
```
This counts the number of distinct departments in the `Employees` table.
2. SUM()
- Purpose: Calculates the total sum of a numeric column.
- Syntax:
```sql
SELECT SUM(column_name) FROM table_name;
```
- Example:
```sql
SELECT SUM(salary) FROM Employees;
```
This returns the total sum of all salaries in the `Employees` table.
3. AVG()
- Purpose: Calculates the average value of a numeric column.
- Syntax:
```sql
SELECT AVG(column_name) FROM table_name;
```
- Example:
```sql
SELECT AVG(salary) FROM Employees;
```
This returns the average salary of all employees.
4. MIN()
- Purpose: Finds the minimum value in a column.
- Syntax:
```sql
SELECT MIN(column_name) FROM table_name;
```
- Example:
```sql
SELECT MIN(salary) FROM Employees;
```
This returns the lowest salary from the `Employees` table.
5. MAX()
- Purpose: Finds the maximum value in a column.
- Syntax:
```sql
SELECT MAX(column_name) FROM table_name;
```
- Example:
```sql
SELECT MAX(salary) FROM Employees;
```
This returns the highest salary from the `Employees` table.
When you want to apply aggregate functions to groups of rows based on a specific column,
you use the GROUP BY clause. This groups the rows into subsets, and the aggregate function
is then applied to each group.
Example:
```sql
SELECT department, AVG(salary)
FROM Employees
GROUP BY department;
```
- This query will return the average salary for each department in the `Employees` table.
```sql
SELECT department, COUNT(*) AS total_employees, AVG(salary) AS avg_salary
FROM Employees
GROUP BY department;
```
- This query will return the total number of employees and the average salary for each
department.
The HAVING clause is used to filter the results after applying an aggregate function, much
like WHERE is used to filter rows before aggregation.
Example:
```sql
SELECT department, COUNT(*) AS total_employees
FROM Employees
GROUP BY department
HAVING COUNT(*) > 10;
```
- This query will return the departments where the number of employees is greater than 10.
- COUNT() ignores NULL values, i.e., it only counts non-NULL values in the specified
column.
- SUM(), AVG(), MIN(), and MAX() treat NULL as unknown, and they will ignore NULL
values in the calculations.
Example:
```sql
SELECT AVG(salary) FROM Employees;
```
- This will calculate the average salary of all employees, ignoring any NULL salary values.
2.What are the DML Commands available in SQL?
DML (Data Manipulation Language) Commands in SQL
DML (Data Manipulation Language) is a subset of SQL used to manage and manipulate the
data stored within a database. DML commands allow you to perform operations like
inserting, updating, deleting, and retrieving data from tables. These commands work with the
actual data stored in the database, rather than the structure or schema of the database.
1. INSERT
- Purpose: Adds new rows of data to a table.
- Syntax:
```sql
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
```
- Example:
```sql
INSERT INTO Employees (EmployeeID, Name, Department, Salary)
VALUES (101, 'John Doe', 'HR', 50000);
```
- Explanation: The INSERT command adds a new row to the `Employees` table with the
specified values for each column.
2. UPDATE
- Purpose: Modifies existing data in a table.
- Syntax:
```sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
```
- Example:
```sql
UPDATE Employees
SET Salary = 55000
WHERE EmployeeID = 101;
```
- Explanation: The UPDATE command changes the salary of the employee with
EmployeeID = 101 to 55000.
3. DELETE
- Purpose: Removes rows from a table based on a condition.
- Syntax:
```sql
DELETE FROM table_name
WHERE condition;
```
- Example:
```sql
DELETE FROM Employees
WHERE EmployeeID = 101;
```
- Explanation: The DELETE command removes the row from the `Employees` table where
the EmployeeID is 101.
4. SELECT
- Purpose: Retrieves data from one or more tables.
- Syntax:
```sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
```
- Example:
```sql
SELECT Name, Salary
FROM Employees
WHERE Department = 'HR';
```
- Explanation: The SELECT command retrieves the Name and Salary of all employees in
the HR department.
Key Points
DDL commands are used to define and manage the structure of the database, including the
creation, alteration, and deletion of tables, schemas, and other database objects. DDL
commands do not manipulate the data itself but rather deal with the structure of the database.
1. CREATE
- Purpose: Creates a new database object like a table, view, index, or schema.
- Syntax:
```sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
```
- Example:
```sql
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
```
- Explanation: This creates a table called `Employees` with columns for EmployeeID,
Name, Department, and Salary.
2. ALTER
- Purpose: Modifies an existing database object, such as adding, deleting, or modifying
columns in a table.
- Syntax:
```sql
ALTER TABLE table_name
ADD column_name datatype;
3. DROP
- Purpose: Deletes a database object (e.g., a table, index, or view) from the database.
- Syntax:
```sql
DROP TABLE table_name;
```
- Example:
```sql
DROP TABLE Employees;
```
- Explanation: This deletes the `Employees` table from the database along with all its data.
4. TRUNCATE
- Purpose: Removes all rows from a table, but retains the table structure. This is faster than
DELETE because it does not log individual row deletions.
- Syntax:
```sql
TRUNCATE TABLE table_name;
```
- Example:
```sql
TRUNCATE TABLE Employees;
```
- Explanation: This command removes all records from the `Employees` table but keeps the
table and its structure intact.
5. RENAME
- Purpose: Renames an existing database object.
- Syntax:
```sql
RENAME TABLE old_table_name TO new_table_name;
```
- Example:
```sql
RENAME TABLE Employees TO Staff;
```
- Explanation: This renames the `Employees` table to `Staff`.
DML commands are used to manipulate and manage the data stored within the tables of a
database. These commands allow you to add, update, delete, and retrieve data from the
database.
1. INSERT
- Purpose: Adds new rows of data into a table.
- Syntax:
```sql
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
```
- Example:
```sql
INSERT INTO Employees (EmployeeID, Name, Department, Salary)
VALUES (101, 'John Doe', 'HR', 50000);
```
- Explanation: This adds a new row into the `Employees` table with the specified values for
each column.
2. UPDATE
- Purpose: Modifies the data in an existing record.
- Syntax:
```sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
```
- Example:
```sql
UPDATE Employees
SET Salary = 55000
WHERE EmployeeID = 101;
```
- Explanation: This updates the salary of the employee with `EmployeeID = 101` to 55000.
3. DELETE
- Purpose: Removes one or more rows from a table based on a condition.
- Syntax:
```sql
DELETE FROM table_name
WHERE condition;
```
- Example:
```sql
DELETE FROM Employees
WHERE EmployeeID = 101;
```
- Explanation: This deletes the row with `EmployeeID = 101` from the `Employees` table.
4. SELECT
- Purpose: Retrieves data from one or more tables. While SELECT is not technically a
DML command in some systems, it is often included as part of DML operations because it
manipulates data retrieval.
- Syntax:
```sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
```
- Example:
```sql
SELECT Name, Salary
FROM Employees
WHERE Department = 'HR';
```
- Explanation: This retrieves the Name and Salary of all employees in the HR department.
Set operations in SQL allow you to combine the results of two or more SELECT queries.
These operations are helpful when you want to retrieve data from multiple tables or queries in
a single result. SQL provides several set operations, including UNION, UNION ALL,
INTERSECT, and EXCEPT (or MINUS in some systems).
1. UNION
- Purpose: Combines the results of two or more queries, removing duplicate rows by default.
- Syntax:
```sql
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
```
- Example:
```sql
SELECT Name FROM Employees
UNION
SELECT Name FROM Contractors;
```
- Explanation: This returns a list of unique names from both the `Employees` and
`Contractors` tables, removing any duplicates.
2. UNION ALL
- Purpose: Similar to UNION, but includes all duplicate rows in the result.
- Syntax:
```sql
SELECT column1, column2, ...
FROM table1
UNION ALL
SELECT column1, column2, ...
FROM table2;
```
- Example:
```sql
SELECT Name FROM Employees
UNION ALL
SELECT Name FROM Contractors;
```
- Explanation: This returns all names from both `Employees` and `Contractors`, including
duplicates. It is faster than UNION because it doesn’t perform duplicate removal.
3. INTERSECT
- Purpose: Returns only the rows that are common to both queries.
- Syntax:
```sql
SELECT column1, column2, ...
FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;
```
- Example:
```sql
SELECT Name FROM Employees
INTERSECT
SELECT Name FROM Contractors;
```
- Explanation: This returns only the names that appear in both `Employees` and
`Contractors`.
1. UNION:
sql
Copy code
SELECT Name FROM Students_A
UNION
SELECT Name FROM Students_B;
Result:
Name
Alice
Bob
Charlie
David
Eva
2. UNION ALL:
sql
Copy code
SELECT Name FROM Students_A
UNION ALL
SELECT Name FROM Students_B;
Result:
Name
Alice
Bob
Charlie
Charlie
David
Eva
3. INTERSECT:
sql
Copy code
SELECT Name FROM Students_A
INTERSECT
SELECT Name FROM Students_B;
Result:
Name
Charlie
4. EXCEPT:
sql
Copy code
SELECT Name FROM Students_A
EXCEPT
SELECT Name FROM Students_B;
Result:
Name
Alice
Bob
Key Points
UNIT-5
3. Fetch Data:
- Retrieve each row in sequence.
- Example: `FETCH NEXT FROM cursor_name INTO variable1;`
4. Process Data:
- Apply any required operations on the fetched data.
Declare Section
Declare section declares the variables, constants, processes, functions, etc., to
be used in the other parts of program. It is an optional section.
Begin Section
It is the executable section. It consists of a set of SQL and PL/SQL statements,
which is executed when PL/SQL block runs. It is a compulsory section.
Exception Section
This section handles the errors, which occurs during execution of the PL/SQL
block. This section allows the user to define his/her own error messages. This
section executes only when an error occurs. It is an optional section.
End Section
This section indicates the end of PL/SQL block.
Every PL/SQL program must consist of at least one block, which may
consist of any number of nested sub-blocks. Figure 5.1 shows a typical PL/SQL
block.
Exception handling in PL/SQL is a mechanism that allows you to manage and respond to
runtime errors or exceptions that occur during program execution. By using the EXCEPTION
section in a PL/SQL block, you can define specific actions to handle errors gracefully,
preventing abrupt termination of the program.
In a PL/SQL block, the EXCEPTION section follows the BEGIN ... END block and allows
you to handle errors through predefined or custom exception handlers:
```plsql
BEGIN
-- Main code block where SQL and PL/SQL statements are executed.
EXCEPTION
WHEN exception_name THEN
-- Code to handle the exception.
END;
```
1. Predefined Exceptions:
- PL/SQL provides built-in exceptions for common errors.
- Examples:
- NO_DATA_FOUND: Raised when a `SELECT INTO` statement returns no rows.
- TOO_MANY_ROWS: Raised when a `SELECT INTO` statement returns more than one
row.
- ZERO_DIVIDE: Raised when dividing by zero.
2. User-Defined Exceptions:
- Custom exceptions defined by the user for specific error-handling needs.
- Declared in the DECLARE section and raised explicitly using the `RAISE` statement.
```plsql
DECLARE
emp_name VARCHAR2(50);
BEGIN
-- Attempt to fetch an employee’s name with a given ID.
SELECT name INTO emp_name FROM Employees WHERE EmployeeID = 999;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END;
```
- Explanation:
- NO_DATA_FOUND exception is raised if no row matches `EmployeeID = 999`.
- The exception handler prints an error message instead of terminating the program abruptly.
Key Points
4.Write a trigger program to display the appropriate message for inserting and deleting
records in a database table
Here is an example of a PL/SQL trigger program that displays appropriate messages when
inserting or deleting records from a database table called `Employees`.
Employees
We’ll create a trigger that displays a message when a record is inserted or deleted.
Trigger Program
```plsql
CREATE OR REPLACE TRIGGER trg_employee_audit
AFTER INSERT OR DELETE ON Employees
FOR EACH ROW
BEGIN
-- Message for Insert operation
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('New record inserted:');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || :NEW.EmployeeID);
DBMS_OUTPUT.PUT_LINE('Name: ' || :NEW.Name);
DBMS_OUTPUT.PUT_LINE('Department: ' || :NEW.Department);
DBMS_OUTPUT.PUT_LINE('Salary: ' || :NEW.Salary);
END IF;
Explanation
Usage
- To see the output messages, you need to enable `DBMS_OUTPUT` in your SQL
environment (such as SQL*Plus or SQL Developer).
- Example:
```sql
SET SERVEROUTPUT ON;
```
This trigger helps in auditing the `Employees` table by displaying messages whenever
records are inserted or deleted.
In PL/SQL, procedures and functions are subprograms that encapsulate a series of SQL and
PL/SQL statements. Both allow reusability, modularity, and better structure within PL/SQL
programs by enabling code reuse and logic organization. They are primarily used to perform
specific tasks within a database.
1. Procedure
A procedure is a PL/SQL block that performs a specific action or set of actions. It can accept
parameters and allows the use of `IN`, `OUT`, and `IN OUT` parameter modes.
Structure of a Procedure
```plsql
CREATE OR REPLACE PROCEDURE procedure_name (parameter1 IN datatype,
parameter2 OUT datatype) IS
-- Declaration section
BEGIN
-- Execution section with PL/SQL code
EXCEPTION
-- Exception handling section
END;
```
Example of a Procedure
```plsql
CREATE OR REPLACE PROCEDURE increase_salary (
emp_id IN NUMBER,
percent IN NUMBER,
new_salary OUT NUMBER
) IS
BEGIN
-- Update the salary
UPDATE Employees
SET Salary = Salary + (Salary * percent / 100)
WHERE EmployeeID = emp_id
RETURNING Salary INTO new_salary;
END;
```
- Explanation:
- The procedure takes `emp_id` (employee ID) and `percent` (percentage increase) as inputs.
- It uses the `OUT` parameter `new_salary` to return the updated salary.
- The salary is updated by the given percentage, and the new salary is stored in
`new_salary`.
2. Function
A function is similar to a procedure but is designed to return a single value. Functions are
often used when a value or calculation needs to be returned, and they can be called directly
within SQL statements.
Structure of a Function
```plsql
CREATE OR REPLACE FUNCTION function_name (parameter IN datatype) RETURN
datatype IS
-- Declaration section
BEGIN
-- Execution section with PL/SQL code
RETURN value; -- Must return a value
EXCEPTION
-- Exception handling section
END;
```
Example of a Function
This function calculates and returns the bonus for an employee based on their salary:
```plsql
CREATE OR REPLACE FUNCTION calculate_bonus (
emp_id IN NUMBER
) RETURN NUMBER IS
emp_salary NUMBER;
bonus NUMBER;
BEGIN
-- Get the salary of the employee
SELECT Salary INTO emp_salary FROM Employees WHERE EmployeeID = emp_id;
RETURN bonus;
END;
```
- Explanation:
- The function `calculate_bonus` takes `emp_id` as input and returns the bonus amount.
- It calculates the bonus as 10% of the employee’s salary and returns this value.
Key Points
1. Procedures are ideal when multiple actions or statements need to be performed, and no
return value is required.
2. Functions are suited for calculations or operations that return a single value and can be
used in SQL expressions.
3. Both can improve modularity, reusability, and readability in PL/SQL code.
4. Exception handling can be included in both to manage errors.