DBMS Ia2
DBMS Ia2
1. Explain how group By clause works? What is the difference between WHERE and Having?
Ans - The GROUP BY clause in SQL is used to arrange identical data into groups based on one
or more columns. It is often used with aggregate functions (like COUNT(), SUM(), AVG(),
MAX(), MIN()) to perform operations on each group of data, rather than on individual rows.
How GROUP BY Works:
1. Grouping Rows: The GROUP BY clause collects rows that have the same values in specified
columns into groups.
2. Aggregate Function Application: After rows are grouped, aggregate functions can be applied
to perform calculations on each group, like counting the rows in a group or summing a
column's values.
3. Order of Execution: The GROUP BY clause is executed after the WHERE clause but before the
ORDER BY clause.
Basic Syntax:
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s);
It is implemented in column
Basic It is implemented in row operations.
operations.
The WHERE
clause fetches the The HAVING clause first fetches
Data specific data from the complete data. It then
fetching particular rows separates them according to the
based on the given condition.
specified condition
Aggregate The WHERE clause does not allow to The HAVING clause can work with
Functions work with aggregate functions. aggregate functions.
The GROUP BY clause comes after the The GROUP BY clause comes
GROUP BY
WHERE clause. before the HAVING clause.
2. List out and explain the different types of JDBC drivers.
Ans - JDBC drivers are client-side adapters (installed on the client machine, not on the
server) that convert requests from Java programs to a protocol that the DBMS can
understand. JDBC drivers are the software components which implements interfaces in JDBC
APIs to enable java application to interact with the database. Now we will learn how many
JDBC driver types does Sun defines? There are four types of JDBC drivers defined by Sun
Microsystem that are mentioned below:
1. Type-1 driver or JDBC-ODBC bridge driver
2. Type-2 driver or Native-API driver
3. Type-3 driver or Network Protocol driver
4. Type-4 driver or Thin driver
Advantages
This driver software is built-in with JDK so no need to install separately.
It is a database independent driver.
Disadvantages
As a common driver is used in order to interact with different databases, the data transferred
through this driver is not so secured.
The ODBC bridge driver is needed to be installed in individual client machines.
Type-1 driver isn’t written in java, that’s why it isn’t a portable driver.
Advantage
Native-API driver gives better performance than JDBC-ODBC bridge driver.
Disadvantages
Driver needs to be installed separately in individual client machines
The Vendor client library needs to be installed on client machine.
Type-2 driver isn’t written in java, that’s why it isn’t a portable driver
It is a database dependent driver.
Advantages
Type-3 drivers are fully written in Java, hence they are portable drivers.
No client side library is required because of application server that can perform many tasks
like auditing, load balancing, logging etc.
Switch facility to switch over from one database to another database.
Disadvantages
Network support is required on client machine.
Maintenance of Network Protocol driver becomes costly because it requires database-
specific coding to be done in the middle tier.
Advantages
Does not require any native library and Middleware server, so no client-side or server-side
installation.
It is fully written in Java language, hence they are portable drivers.
Disadvantage
If the database varies, then the driver will carry because it is database dependent.
Ans - In SQL, the SELECT, ALTER, and UPDATE statements are fundamental commands used for
retrieving, modifying, and updating data or table structures in a database. Here’s a breakdown of
each statement with examples.
1. SELECT Statement
The SELECT statement is used to retrieve data from one or more tables in a database. It allows users
to specify which columns to display and can include conditions to filter results.
Syntax:
FROM table_name
WHERE condition;
Example:
FROM Employees
- **Explanation**: This query retrieves the `FirstName` and `LastName` of employees from the
`Employees` table who work in the Sales department.
- **GROUP BY**: Groups rows with similar values (often used with aggregate functions).
- **JOIN**: Combines rows from two or more tables based on related columns.
2. ALTER Statement
The ALTER statement is used to modify the structure of an existing table. It can be used to add,
delete, or modify columns, and also to change constraints on the table.
Syntax:
Example:
-- Drop a column
ALTER TABLE Employees
- **Explanation**:
- The first command adds a new column `DateOfBirth` of type `DATE` to the `Employees` table.
- The second command changes the `LastName` column to a data type of `VARCHAR(50)`.
- The third command removes the `MiddleName` column from the `Employees` table.
3. UPDATE Statement
The **UPDATE** statement is used to modify existing data within a table. This statement allows you
to change values in one or more rows, based on specified conditions.
Syntax:
UPDATE table_name
WHERE condition;
Example:
UPDATE Employees
- **Explanation**: This query updates the `Salary` field by increasing it by 10% for employees in the
Sales department.
Ans: A trigger is a set of SQL statements that automatically executes (or “fires”) in response to
specific events on a table or view in a database. Triggers are commonly used to enforce business
rules, maintain audit trails, and automate tasks like data validation or integrity checks.
Characteristics of Triggers
- **Before or After Events**: Triggers can be set to execute either *before* or *after* the triggering
event occurs.
Types of Triggers
1. **Before Triggers**: Execute before the triggering event (e.g., before inserting or updating a row).
2. **After Triggers**: Execute after the triggering event (e.g., after inserting or deleting a row).
3. **Instead of Triggers** (specific to certain databases like SQL Server): Execute in place of a
triggering event, typically on views to simulate an updateable view.
Syntax of a Trigger:
BEGIN
END;
Explanation of Syntax
- **BEFORE/AFTER/INSTEAD OF**: Specifies when the trigger should be fired relative to the event.
- **INSERT, UPDATE, DELETE**: Defines the type of event that will activate the trigger.
- **FOR EACH ROW**: Ensures the trigger fires for each affected row in the table.
- **BEGIN ... END**: Encloses the trigger’s logic (the SQL statements to execute).
Example of a Trigger
**Scenario**: Suppose we want to automatically log any salary updates in the `Employees` table into
a `Salary_Audit` table, recording the employee's ID, old salary, new salary, and the timestamp of the
update.
Employee_ID INT,
BEGIN
END;
- **Explanation**:
- **FOR EACH ROW** means the trigger applies to each row affected by the update.
- The `INSERT INTO Salary_Audit` statement logs the employee's ID, old salary (`OLD.Salary`), new
salary (`NEW.Salary`), and the timestamp into the `Salary_Audit` table.
5. Define referential integrity constraint. Explain the importance of referential integrity
constraint. How is this constraint implemented in SQL.
Ans: A referential integrity constraint is a rule in a relational database that ensures consistency
between related tables by maintaining valid relationships between them. It requires that a foreign
key in one table must either match a primary key in another table or be null. This constraint helps
prevent orphaned records and keeps related data in sync across tables.
- Ensures that there are no orphaned records in the database. For example, if an order references a
customer ID, referential integrity guarantees that this customer exists in the customers table.
- By enforcing consistent relationships, referential integrity constraints reduce the need for manual
checks, making database management easier and reducing potential errors.
In SQL, referential integrity is typically implemented using **foreign keys**. A foreign key in a child
table references the primary key of a parent table. This setup enforces referential integrity by
ensuring that the values in the foreign key column(s) match values in the parent table’s primary key
column(s).
Syntax:
**Explanation of Constraints**:
- **FOREIGN KEY**: Specifies the column in `ChildTable` (`ParentID`) that references the `ParentID`
column in `ParentTable`.
- **ON DELETE CASCADE**: If a record in the `ParentTable` is deleted, all related records in
`ChildTable` will also be deleted. This prevents orphaned records.
- **ON UPDATE CASCADE**: If the `ParentID` in the `ParentTable` is updated, all corresponding
`ParentID` values in the `ChildTable` will also be updated to match.
- **Purpose**: Here, the `CustomerID` column in the `Orders` table is a foreign key that references
the `CustomerID` column in the `Customers` table.
- **ON DELETE CASCADE**: If a customer is deleted from the `Customers` table, all orders related to
that customer in the `Orders` table will automatically be deleted.
6. Demonstrate insertion, deletion and modification anomalies. Why are they considered bad?
Illustrate with example.
Ans: Insertion, Deletion, and Modification Anomalies are issues that arise in poorly designed
databases, often due to redundancy and a lack of normalization. These anomalies create
inconsistencies and make data management error-prone, leading to a lack of data integrity.
1. Insertion Anomaly
Definition: An insertion anomaly occurs when adding a new record is difficult or impossible due to
the table’s design. This issue usually arises when certain fields are dependent on other fields, even if
they’re not yet relevant.
Example**:
Suppose we have a table called `Employee_Project` that stores information about employees and the
projects they are working on.
|-------------|---------------|------------|-----------------|
- **Problem**: If a new employee joins but has not yet been assigned to any project, we cannot add
their details without leaving `Project_ID` and `Project_Name` fields empty or using nulls.
- **Why It’s Bad**: This results in a partial record, which can lead to data inconsistencies and unused
space. Proper database design should allow us to record employees independently of projects, likely
by using separate `Employee` and `Project` tables.
2. Deletion Anomaly
Definition**: A deletion anomaly occurs when deleting a record unintentionally removes additional,
valuable data. This is common when data from two different entities is stored in the same table.
Example**:
|-------------|---------------|------------|-----------------|
- **Problem**: If we delete the entry for `Project_ID = 1` because the Database Design project is
completed, we also lose all information about `Employee_ID = 101` (Alice), even though we may
want to keep her employment records.
- **Why It’s Bad**: Valuable employee information is lost due to poor table structure. Separate
tables for employees and projects would allow us to delete a project without affecting the
employee’s records.
3. Modification Anomaly
Definition**: A modification anomaly occurs when changing data in one place requires changes in
multiple rows, which can lead to inconsistencies if updates are not made consistently.
Example**:
|-------------|---------------|------------|-----------------|
- **Why It’s Bad**: This increases the chance of data inconsistency, where some rows reflect the
new project name and others still show the old name. Proper design, such as a separate `Projects`
table, would allow us to update the project name in only one place.
1. **Data Redundancy**: These anomalies often result from redundant data, leading to wasted
storage and an increased chance of inconsistency.
2. **Data Integrity Issues**: Anomalies create situations where the data can become inconsistent or
incorrect.
1. COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
Syntax :
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
Syntax
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function returns
the average of all non-Null values.
Syntax
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function determines the
largest value of all selected values of a column.
Syntax
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function determines the
smallest value of all selected values of a column.
Syntax
(ii) Find the names of employees who work on all projects controlled by department number4.
SELECT E.Name
FROM EMP E
WHERE NOT EXISTS (
SELECT P.Pnumber
FROM PROJECT P
WHERE P.Dnum = 4
AND NOT EXISTS (
SELECT W.ESSN
FROM WORKS_ON W
WHERE W.ESSN = E.SSN
AND W.Pno = P.Pnumber
)
);
Explanation: This query uses a double NOT EXISTS to find employees (E.Name) who work on all
projects controlled by department number 4. The inner subquery checks that there is no project
under department 4 that the employee is not working on.
iii) Retrieve the SSN of all employees who either in department no :4 or directly supervise an
employee who work in department number :4
SELECT DISTINCT E.SSN
FROM EMP E
WHERE E.Dno = 4
OR E.SSN IN (
SELECT E2.SuperSSN
FROM EMP E2
WHERE E2.Dno = 4
);
Explanation: This query finds employees (E.SSN) who either work in department number 4 (E.Dno =
4) or directly supervise an employee who works in department 4 (E2.SuperSSN = E.SSN). The IN
clause ensures that supervisors of employees in department 4 are included.
Ans: In a relational database, update anomalies occur when there are inconsistencies in the data due
to the way that data is stored and modified. These anomalies typically arise in poorly designed
database schemas, particularly those that do not follow the principles of normalization. Update
anomalies can lead to data integrity issues, redundancy, and difficulties in maintaining data.
1. Insertion Anomaly
2. Deletion Anomaly
3. Modification Anomaly
1. Insertion Anomaly:
An insertion anomaly occurs when certain attributes cannot be inserted into the database without
the presence of other attributes. This can happen in scenarios where data is interdependent.
Example:**
In this table, if we want to insert a new course, say **History**, but do not have a student enrolled
in it yet, we can't add the course without also adding a **StudentID**, **StudentName**, and
**Instructor**. This is problematic if the course is not yet associated with any student.
Issue: - If a new **History** course needs to be added but there are no students yet enrolled, it
cannot be represented in this table without creating a misleading record for a student who is not
actually taking that course.
2. Deletion Anomaly
A **deletion anomaly** occurs when the deletion of a certain piece of data inadvertently results in
the loss of additional data that is not intended to be deleted. This often happens when multiple
types of information are stored in a single row.
Example:**
If we delete the record for **Jane Doe**, who is taking Math with Dr. Smith, we also lose the
information that **Dr. Smith** teaches the **Math** course. This data is lost even though we may
want to keep the record of the instructor.
Issue**:
3. Modification Anomaly
A **modification anomaly** occurs when changes to a piece of data require multiple rows to be
updated, leading to potential inconsistencies if all rows are not updated properly.
Example:**
Now suppose **Dr. Smith** changes his name to **Dr. Johnson**. If we fail to update all
occurrences of **Dr. Smith** in the table, we might end up with some records showing the old name
and some with the new name, leading to data inconsistency.
Issue**:
- If you forget to update the name for **Jane Doe**, the data now has two different names
associated with the same instructor, which creates confusion and inconsistency in the database.
Ans - SQLJ is a set of programming extensions for embedding SQL (Structured Query Language)
statements directly within Java code. It allows developers to combine Java and SQL in a more
structured and readable manner, compared to the traditional approach of using JDBC (Java Database
Connectivity). SQLJ provides a simpler, more declarative syntax for embedding SQL into Java
programs, similar to how embedded SQL works with other languages like C or COBOL.
Differences:
1. SQL Type:
SQLJ: Primarily supports static SQL, which means SQL queries are predefined and embedded in the
Java code at compile time.
JDBC: Allows dynamic SQL, meaning SQL queries are constructed and executed at runtime.
2. Type Safety:
SQLJ: Offers strong type safety. It checks if the SQL query matches the
expected data types in Java during the compile phase, preventing type-related
issues.
JDBC: Is less type-safe, since type checking occurs at runtime. The developer
needs to ensure the correct mapping between SQL result types and Java types,
which may lead to runtime exceptions if mismatches occur.
3. Code Complexity:
SQLJ: Provides cleaner and more readable code by allowing SQL queries to be embedded directly in
Java using #sql syntax.
JDBC: Tends to be more verbose and complex, requiring explicit handling of SQL queries as strings,
setting parameters, and managing ResultSet objects.
4. Performance:
SQLJ: Generally provides better performance for static SQL, as queries are precompiled, and
execution plans can be optimized by the database engine in advance.
5. Development Process:
SQLJ: Requires an additional precompilation step. SQLJ code must be processed by a translator that
converts SQLJ code into standard Java code.
6. Error Handling:
SQLJ: Since SQL is statically checked at compile time, many SQL-related errors can be caught before
runtime.
JDBC: Since SQL is executed at runtime, errors are encountered during query execution, making error
handling more complex.
12. Write a short note on granting and revoking of privileges in SQL. Bring out the different clauses
of SELECT-FROM-WHERE-GROUP-HAVING with an example for each.
13. Demonstrate working of Assertion & Triggers in database? Explain with an example.
Ans - 1. Assertions in a Database:
Assertions are database constraints used to enforce conditions or rules across multiple tables or
records. Unlike primary keys, foreign keys, or unique constraints, which are tied to a single table,
assertions are typically used for more complex, inter-table constraints. They ensure that certain
conditions are always true for the data in the database.
Example:
Suppose you have two tables: `EMPLOYEES` (storing employee data) and `DEPARTMENTS` (storing
department data). You want to ensure that the total salary of employees in a department never
exceeds a set budget for that department.
CREATE ASSERTION salary_budget_check
CHECK (
NOT EXISTS (
SELECT D.DNum
FROM DEPARTMENTS D
WHERE (SELECT SUM(E.Salary)
FROM EMPLOYEES E
WHERE E.Dno = D.DNum) > D.Budget
)
);
This assertion ensures that the sum of employee salaries in any department does not exceed that
department’s budget.
Limitations:
- Assertions are not commonly supported by all databases (e.g., many popular DBMSs like MySQL do
not support assertions directly).
- Assertions can be expensive to enforce because the database system has to evaluate the condition
every time relevant data is modified.
- They are non-standard and database-dependent, meaning that using them might not work in all
databases or could lead to performance issues.
2. Triggers in a Database:
Triggers are special procedures or database scripts that automatically execute (or "fire") in response to
certain events in the database, such as `INSERT`, `UPDATE`, or `DELETE` operations on a particular
table. Triggers are a powerful tool to automate certain processes and enforce business rules or data
integrity.
How Triggers Work:
- Triggers are associated with a specific table or view.
- They are activated automatically before or after a specific type of data modification (like INSERT,
UPDATE, or DELETE) occurs on that table.
- Triggers can be defined to execute **BEFORE** or **AFTER** the event happens, which allows you
to either enforce constraints or apply changes as soon as the event is triggered.
Types of Triggers:
1. BEFORE Trigger: Executes before the triggering event (e.g., before an insert).
2. AFTER Trigger: Executes after the triggering event has completed.
3. INSTEAD OF Trigger: Used for views, allowing the trigger to substitute or override the standard
operation (e.g., for complex views that are not inherently updatable).
Syntax:
CREATE TRIGGER trigger_name
{ BEFORE | AFTER | INSTEAD OF } { INSERT | UPDATE | DELETE }
ON table_name
[ FOR EACH ROW ]
BEGIN
-- Trigger actions (SQL statements)
END;
- trigger_name: The unique name of the trigger.
- { BEFORE | AFTER | INSTEAD OF }: Specifies whether the trigger should run before, after, or instead of
the actual operation.
- { INSERT | UPDATE | DELETE }: Specifies the event that fires the trigger.
- table_name: The table to which the trigger is bound.
- FOR EACH ROW: Specifies that the trigger should be fired once for each row affected by the event
(common for row-level triggers).
Example:
Suppose you have a `SALES` table and you want to automatically update the `TOTAL_SALES` column in
the `EMPLOYEES` table every time a new sale is recorded.
CREATE TRIGGER update_total_sales
AFTER INSERT ON SALES
FOR EACH ROW
BEGIN
UPDATE EMPLOYEES
SET TOTAL_SALES = TOTAL_SALES + :NEW.amount
WHERE EMPLOYEES.id = :NEW.employee_id;
END;
- This trigger fires AFTER an `INSERT` on the `SALES` table and updates the `TOTAL_SALES` of the
employee involved in the sale. The `:NEW` keyword refers to the newly inserted row in the `SALES`
table.
Let’s consider a simple example where we create a stored procedure to insert a new employee
record into an `Employees` table.