0% found this document useful (0 votes)
22 views19 pages

DBMS File CS 502

Uploaded by

sahudeepesh029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views19 pages

DBMS File CS 502

Uploaded by

sahudeepesh029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Experiment No : 1

Objective:- Learn and apply the concept of Data Definition Language (DDL)
commands in RDBMS, Data Manipulation Language (DML) and Data
Control Language (DCL).
1. Data Definition Language (DDL)
DDL commands define the structure of a database and its objects, such as tables,
indexes, and views. They focus on creating, altering, and deleting these elements.
Common DDL commands include:
● CREATE: Creates a new table, database, index, or other database object.

● ALTER: Modifies an existing database object, such as adding or dropping a


column in a table.

● DROP: Deletes a table, database, index, or other database object.

● TRUNCATE: Removes all rows from a table, resetting it to an empty state.

2. Data Manipulation Language (DML)


DML commands are used to manipulate data stored in tables. These commands allow
for the insertion, update, and deletion of data. Common DML commands include:
● INSERT: Adds new rows to a table.
● UPDATE: Modifies existing data in a table.

● DELETE: Removes rows from a table.

● SELECT (often classified separately as Query Language or SQL, but commonly


grouped with DML): Retrieves data from one or more tables.

3. Data Control Language (DCL)


DCL commands manage user permissions and control access to database objects.
They ensure data security by granting or revoking permissions. Common DCL
commands include:
● GRANT: Gives specific privileges to users or roles.

● REVOKE: Removes specific privileges from users or roles.

Applying the Concepts


To apply these concepts:
● Use DDL commands when setting up the database structure, like defining tables
and constraints.
● Use DML commands when inserting, updating, or deleting data within the tables.
● Use DCL commands to control who can access and modify data or database
objects.
Experiment No : 2
Objective:- Simple queries: selection, projection, sorting on a simple table,
Small-large number of attributes ,Distinct output values ,Renaming
attributes , Computed attributes Simple-complex conditions (AND, OR,
NOT).

1. Selection
Selection involves retrieving specific rows from the table based on a condition.

2. Projection
Projection is used to retrieve specific columns from a table

3. Sorting
Sorting arranges the result set in a specific order, using ORDER BY.
4. Small-Large Number of Attributes
You can retrieve either a few or all attributes in the table.

5. Distinct Output Values


To get unique values in a column, use DISTINCT.

6. Renaming Attributes
Use AS to rename columns in the output.

7. Complex Conditions with AND:

8. Complex Conditions with OR:

9. Complex Conditions with NOT:


Experiment No : 3
Objective:- Partial Matching operators (LIKE, %, _, *, ?) ,ASC-DESC ordering
combinations Checking for Nulls

● LIKE: Used to find a pattern within text columns.


% (wildcard for zero or more characters): Matches any sequence of characters
(including no characters).

_ (single-character wildcard): Matches exactly one character.

Combining % and _:

2. ASC and DESC Ordering


You can use ORDER BY to sort the result set, and specify ASC (ascending) or DESC
(descending) order. Multiple columns can be sorted in different directions in the same
query.

3. Checking for Nulls


The IS NULL and IS NOT NULL operators check for null (missing) values in columns.
● IS NULL: Checks if a column has a null value.

● IS NOT NULL: Checks if a column has a non-null value.


Experiment No : 4
Objective: - Multi-table queries (JOIN OPERATIONS), Simple joins (no
INNER JOIN) Aliasing tables – Full/Partial name qualification, Inner-joins
(two and more (different) tables), Inner-recursive- joins (joining to itself),
Outer-joins (restrictions as part of the WHERE and ON clauses) ,Using
where & having clauses.

1. Simple Joins (Aliasing Tables and Basic Inner Join)


Simple joins use conditions to relate columns from multiple tables. We use aliases to
make table references shorter.

2. Inner Join with Multiple Tables


An inner join with multiple tables is useful when querying information from three or
more tables.

3. Self-Join (Recursive Join)


A self-join is a join operation where a table joins to itself. Let’s assume we want to find
employees who report to a manager. We assume:
● ManagerID column in Employees references the ID of another employee who is
the manager.

4. Outer Joins (LEFT JOIN, RIGHT JOIN)


Outer joins include rows from one table even when there’s no match in the other table.
Use these for scenarios like finding employees who aren’t assigned to a department or
employees without projects.
● LEFT JOIN: Retrieves all employees and their departments. Employees with no
department show NULL for department-related fields.
● RIGHT JOIN: Retrieves all departments and employees in each department,
even if no employee is assigned.

5. Full Outer Join


Full outer joins combine all rows from both tables, with unmatched rows showing NULL
in columns for the other table. Not all RDBMS support FULL OUTER JOIN.
Experiment No : 5
Objective: - Nested queries: In, Not In Exists, Not Exists Dynamic relations
(as part of SELECT, FROM, and WHERE clauses).
1. IN and NOT IN
IN and NOT IN are used to check if a value exists in a set of values returned by a
subquery.
Example: IN (Retrieve employees who are assigned to a project.)

Example: NOT IN (Retrieve employees who are not assigned to any project.)

2. EXISTS and NOT EXISTS


EXISTS and NOT EXISTS are used to check if a subquery returns any rows. They are
often more efficient than IN for complex queries, especially when working with large
datasets.

Example: EXISTS (Retrieve employees who belong to a department that has a project.)
Example: NOT EXISTS (Retrieve departments that have no employees.)

3. Dynamic Relations in SELECT, FROM, and WHERE Clauses


Dynamic relations involve using subqueries within SELECT, FROM, or WHERE clauses to
generate data dynamically based on the results of other queries.
Example: Dynamic Subquery in SELECT
Retrieve each employee's name along with the number of projects they are assigned to.

Example: Dynamic Subquery in FROM


Retrieve the department with the highest number of employees.

Example: Dynamic Subquery in WHERE


Retrieve projects where employees have a salary above the average salary.
Experiment No : 6
Objective:- Set Oriented Operations :Union, Difference, Intersection, Division

1. Union
The UNION operation combines rows from two tables, removing duplicates, and returns
all unique rows from both queries. Let’s say we want a combined list of all unique
departments and all unique project names.

2. Difference
The EXCEPT (or MINUS in some SQL dialects) operation returns rows from the first
query that are not in the second query. For example, if we want to find employees who
are not assigned to any project, we can use EXCEPT.
3. Intersection
The INTERSECT operation returns rows that are common to both tables. For instance,
we can find employees who are also assigned to at least one project.

4. Division
The DIVISION operation is useful when you want to find entities related to all entities in
another set. This is often simulated using JOIN, GROUP BY, and HAVING clauses in SQL.

Result: (Since no employee is assigned to all projects in the example, this query would
return no results in this case.)

Experiment No : 8
Objective:- PL/SQL Programming II :Creating stored procedures, functions,
Trigger
CURSOR SYNTAX
DECLARE test CURSOR FOR
Select *from employee_salary;
Open cursor_name;
FETCH [ NEXT [ FROM ] ] cursor_name INTO variable_list;
Close cursor_name;

PROCEDURE
Experiment No : 9
Objective:- Create your optimize database in a team for any given problem

Problem Statement
Design a database for a task management application for a company. The application
should manage teams, employees, projects, tasks, and task assignments. The goal is
to track which employees are working on which tasks, the tasks’ statuses, and the
projects they belong to.
Step-by-Step Plan to Create the Optimized Database
Step 1: Requirements Gathering & Analysis
● Identify the key entities in the system: Teams, Employees, Projects, Tasks, Task
Assignments, Task Statuses.
● Determine relationships between these entities.
● Identify the main queries that the application will use, as these will influence the
design.
Example questions to ask:
● How are tasks assigned to employees?
● Can multiple employees work on a single task?
● Can employees belong to multiple teams?
● What are the various statuses for a task (e.g., Pending, In Progress, Completed)?
Step 2: Create an ER Diagram
The Database Architect creates an ER Diagram that outlines the entities and their
relationships. A preliminary design could look like this:
1. Teams - Represents different teams within the company.
2. Employees - Each employee has a role and is associated with one or more
teams.
3. Projects - Projects that are managed by different teams.
4. Tasks - Each project has multiple tasks associated with it.
5. Task Assignments - Links employees to tasks to track who is working on what.
6. Task Statuses - Defines the status of each task (e.g., Pending, In Progress,
Completed).
Step 3: Define Database Schema
The Database Architect and Developer work together to create the schema. Here’s a
sample schema for each table:

Step 4: Normalize the Schema

The Data Analyst and Database Architect work together to ensure the schema is
normalized, which involves:
1. Removing duplicate data (1NF - First Normal Form).
2. Ensuring relationships through foreign keys (2NF - Second Normal Form).
3. Removing transitive dependencies to avoid redundancy (3NF - Third Normal
Form).
In this case:
● All columns are atomic.
● Foreign keys ensure data is related but not duplicated.
● Separate tables for Task Statuses and Task Assignments help avoid redundant
data.
Step 5: Indexing and Query Optimization

The DBA focuses on adding indexes to speed up frequent queries.


1. Primary Key Indexes: MySQL automatically creates indexes on primary keys.
2. Foreign Key Indexes: Adding indexes on foreign keys can improve join
performance.

The DBA and Data Analyst should also identify potential performance bottlenecks and
create optimized views or materialized views for complex reports.

Step 6: Security & Access Control

Step 7: Testing and Load Testing

The QA Engineer tests the database schema for correctness by running sample queries
and verifying results. Load testing is also performed to see how the database handles
concurrent connections and large datasets.
Example testing queries:
Step 8: Documentation and Handover
The team documents the database structure, optimizations, and access control
policies. This documentation should include:
● ER Diagram showing the database structure and relationships.
● Schema definition and any specific optimizations.
● Permissions and access controls.
● Maintenance Plan including backup, recovery, and indexing strategies.

You might also like