0% found this document useful (0 votes)
14 views

Study

The document provides details about a laboratory manual for a database management systems course. It includes experiments on creating ER diagrams, using DDL commands to define schemas, using DML commands to manipulate data, and SQL clauses. The experiments cover key concepts for working with databases.

Uploaded by

Abhay Sharma
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)
14 views

Study

The document provides details about a laboratory manual for a database management systems course. It includes experiments on creating ER diagrams, using DDL commands to define schemas, using DML commands to manipulate data, and SQL clauses. The experiments cover key concepts for working with databases.

Uploaded by

Abhay Sharma
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/ 22

Database Management Systems

Laboratory Manual

Subject Code: CO-202

DELHI TECHNOLOGICAL UNIVERSITY


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SUBMITTED TO: SUBMITTED BY:


Dr. Pawan Singh Mehra Madhav Gupta
(2K21/CO/262)
2K21/CO/262 CO202 Laboratory Manual
2K21/CO/262 CO202 Laboratory Manual

INDEX

S Topic Date
No.

1 To draw an ER diagram. 24/1/2023

2 To Study DDL Commands in SQL. 7/2/2023

3 To Study DML Commands and constraints in SQL. 21/1/2023

4 To implement the set operations and clauses like where, order 28/01/2023
by, group by, having operations.

5 To implement various inbuilt functions and views. 28/03/2023

6 Implement the nested queries and various operations on joins 28/03/2023

7 Introduction to PL/SQL 08/03/2023

8 Write a Program to reverse a number by taking input from 11/04/2023


user
2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 1
24/01/2023
AIM
To draw an ER diagram.

THEORY
ER (Entity-Relationship) diagrams are graphical representations used to model and
design databases. They are used to visualize and organize the data that will be stored
in the database. ER diagrams illustrate the relationships between entities, which are
objects or concepts that hold data in the database.
In an ER diagram, entities are represented by rectangles, and relationships between
entities are represented by diamonds. The relationships between entities can be one-
to-one, one-to-many, or many-to-many.
Creating an ER diagram involves identifying the entities and attributes that will be
stored in the database and then representing them in a visual format. The attributes
are the characteristics or properties of an entity, and they are represented inside the
rectangle representing the entity.
To draw an ER diagram, we follow these steps:
1. Identify entities: These are the objects or concepts you want to represent in the
database.
2. Define relationships: Determine the relationships between entities, such as one-
to-one, one-to-many, or many-to-many.
3. Create a table for each entity: List all attributes or characteristics of each entity.
4. Draw a rectangle for each entity and label it with the entity name and its
attributes.
5. Draw a diamond for each relationship and label it with the relationship name
and cardinality.
6. Connect the entities and relationships with lines and arrows to indicate the
direction of the relationships according to the rules to represent the type of
relationships.
2K21/CO/262 CO202 Laboratory Manual

Q1. National Hockey league (ER Diagram)


1. NHL has many teams and each team has name, city, coach, captain and set of players.
2. Each player belongs to only one team and had skill level, position and set of records.
3. Team Captain is also a Player
4. Game is played between two teams (host and guest) and have date and a score.

Q2. UPS Database

UPS prides itself on having up-to-date information on the processing and current location of each
shipped item. To do this, UPS relies on a company-wide information system. Shipped items are the
heart of the UPS product tracking information system. Shipped items can be characterized by item
number (unique), weight, dimensions, insurance amount, destination, and final delivery date.
Shipped items are received into the UPS system at a single retail center. Retail centers are
characterized by their type, uniqueID, and address. Shipped items make their way to their destination
via one or more standard UPS transportation events (i.e., flights, truck deliveries).
These transportation events are characterized by a unique scheduleNumber, a type (e.g, flight, truck),
and a deliveryRoute. Please create an Entity Relationship diagram that captures this information
about the UPS system. Be certain to indicate identifiers and cardinality constraints.
2K21/CO/262 CO202 Laboratory Manual

Q3. Library database

A library service wants to create a database to store details of its libraries, books and borrowers. Details include
the following:

A book has a unique ISBN number, a title and one or more authors. The library service may own several
copies of a given book, each of which is located in one of the service’s libraries. A given library contains
many books, and in order to distinguish different copies of the same book a library assigns a different copy-
number to each of its copies of a given book; the price that was paid for each copy is also recorded. Every
library has a unique name and is either a main library or a branch library. A main library may have zero or
more branch libraries and every branch library is a branch of exactly one main library. A borrower has a
name and a unique ID code. A borrower can have many books on loan, but each copy of a book can only be
on loan to one borrower. A borrower could borrow the same book on several occasions, but it is assumed
that each such loan will take place on a different date.
2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 2
7/02/2023
AIM
To Study DDL Commands in SQL.

THEORY
DDL (Data Definition Language) Commands are used to define the Schema of the Database:
1. CREATE: Creates a new table
Syntax: "CREATE TABLE [table name] (column1 data type, column2 data type, ...);"
2. ALTER: Modifies an existing table
Syntax: "ALTER TABLE [table name] [modification command];"
3. DROP: Deletes an existing table
Syntax: "DROP TABLE [table name];"
4. TRUNCATE: Empties a table, including all data and resetting the auto-increment counter
Syntax: "TRUNCATE TABLE [table name];"

IMPLEMENTATION
Table-Name: Student
Attributes: Name, Branch, Semester, Roll_No, Grade

1) Query to create table

2) Query to drop the table

3) Query to add column “Address” in the table


2K21/CO/262 CO202 Laboratory Manual
4) Query to delete data in the table

5) Query to change name of the table “Student” to “University_Student


2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 3
21/02/2023
AIM
To Study DML Commands and constraints in SQL.

THEORY
DML (Data Manipulation Language) Commands are used to define the Schema of the Database:
1. INSERT: Insert data into a table
Syntax: "INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);"
2. UPDATE: Updates existing data within a table
Syntax: "UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;"
3. DELETE: Deletes record(s) from a database table
Syntax: "DELETE FROM table_name WHERE condition;”
4. SELECT: Retrieve data from a database
Syntax: "SELECT column1, column2, ...
FROM table_name;”

IMPLEMENTATION
Table-Name: Voter
Attributes: F_Name, L_Name, City, DOB, Age, Voter_ID

i. Query to insert the data of one more voter

ii. Query to update L_Name= “Aggarwal” where Voter_ID=31


2K21/CO/262 CO202 Laboratory Manual

iii. Query to delete the details of Voter Ritu

iv. Display the details of Voter who belong to Delhi


2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 4

AIM 28/02/2023
To implement the set operations and clauses like where, order by, group by, having
operations.

THEORY
SET OPERATIONS:
1. UNION: Combines the result sets of two or more SELECT statements into a single result set, removing
duplicate rows.
Syntax:
SELECT column1, column2, ... FROM table1
UNION
SELECT column1, column2, ... FROM table2;

2. UNION ALL: Combines the result sets of two or more SELECT statements into a single result set,
including all duplicate rows.
Syntax:
SELECT column1, column2, ... FROM table1
UNION ALL
SELECT column1, column2, ... FROM table2;

3. INTERSECT: Returns only the rows that appear in both result sets of two SELECT statements.
Syntax:
SELECT column1, column2, ... FROM table1
UNION INTERSECT
SELECT column1, column2, ... FROM table2;

4. MINUS: Returns only the rows that appear in the result set of the first SELECT statement but not in the
result set of the second SELECT statement.
Syntax:
SELECT column1, column2, ... FROM table1
MINUS
SELECT column1, column2, ... FROM table2;

CLAUSES:
1. WHERE: The WHERE clause is used to filter the rows returned by a SELECT statement. It allows you to
specify a condition that must be met for a row to be included in the result set. The WHERE clause can
use comparison operators (e.g., =, <>, <, >, <=, >=), logical operators (e.g., AND, OR, NOT), and
parentheses to create complex conditions.
Syntax: SELECT column1, column2, ... FROM table_name
WHERE condition;
2. ORDER BY: The ORDER BY clause is used to sort the result set of a SELECT statement. It allows you to
specify one or more columns to sort by, as well as the order (ascending or descending) for each column.
By default, the ORDER BY clause sorts the result set in ascending order.
Syntax: SELECT column1, column2, ... FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
2K21/CO/262 CO202 Laboratory Manual
3. GROUP BY: The GROUP BY clause is used to group the rows returned by a SELECT statement based
on one or more columns. It is often used in combination with aggregate functions (e.g., SUM,
COUNT, AVG) to calculate summary information for each group.
Syntax: SELECT column1, column2, ..., aggregate_function(column_name)
FROM table_name
GROUP BY column1, column2, ...;

4. HAVING: The HAVING clause is used to filter the groups returned by a GROUP BY clause. It allows you
to specify a condition that must be met for a group to be included in the result set. The HAVING clause
works like the WHERE clause, but operates on groups rather than individual rows.
Syntax: SELECT column1, column2, ... FROM table_name
GROUP BY column1, column2, ...
HAVING condition;

IMPLEMENTATION
SET OPERATIONS:
Table: Vendors, Customers
Attributes: Name, Address

1. UNION:

SELECT name, address FROM customers

UNION

SELECT name, address FROM vendors;

2. UNION ALL:

SELECT name, address FROM customers

UNION ALL

SELECT name, address FROM vendors;

3. INTERSECT:

SELECT name, address FROM customers

INTERSECT

SELECT name, address FROM vendors;


2K21/CO/262 CO202 Laboratory Manual
4. MINUS:

SELECT name, address FROM customers

MINUS

SELECT name, address FROM vendors

CLAUSES:
Table: Employees
Attributes: id, name, department, salary

1. WHERE:
SELECT * FROM employees
WHERE department = 'Marketing';

2. ORDER BY:
SELECT * FROM employees
ORDER BY salary DESC;

3. GROUP BY:
SELECT department, SUM(salary) as total_salary FROM employees
GROUP BY department;

4. HAVING:
SELECT department, SUM(salary) as total_salary FROM employees
GROUP BY department
HAVING total_salary > 120000;
2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 5
28/03/2023
AIM
To implement various inbuilt functions and views.

THEORY (Inbuilt Functions)


SINGLE ROW FUNCTIONS:
ASCII(char)- Returns the ASCII code value of the leftmost character of the input string.
LENGTH(str)- Returns the length of the input string.
LTRIM(str)- Returns the input string with all leading spaces removed.
LPAD(str, len, pad)- Returns the input string left-padded with the specified pad character to the specified
length.
RPAD(str, len, pad)- Returns the input string right-padded with the specified pad character to the
specified length.
VSIZE(expr) - Returns the number of bytes required to store the input expression in the database.
POWER(num1, num2) - Returns num1 raised to the power of num2.

MULTI ROW FUNCTIONS:


MAX(expr)- Returns the maximum value of the input expression over all rows.
MIN(expr)- Returns the minimum value of the input expression over all rows.

STRING/CHARACTER FUNCTIONS:
LOWER(str) - Returns the input string with all characters converted to lowercase.
INITCAP(str) - Returns the input string with the first character of each word capitalized and all
other characters converted to lowercase.
UPPER(str) - Returns the input string with all characters converted to uppercase.
SUBSTR(str, start, length) - Returns a substring of the input string starting at the specified position and with
the specified length.

NUMERICAL FUNCTIONS:
ABS(num) - Returns the absolute value of the input number.
POWER(num, exp) - Returns num raised to the power of exp.
ROUND(num, [decimal_places]) - Returns the input number rounded to the specified number of
decimal places (0 if not specified).
LEAST(num1, num2, ...) - Returns the smallest value among the input numbers.
RESULT - Pseudo column that can be used to refer to the result of the current
r row in a SELECT statement.

OTHER NUMERICAL FUNCTIONS:


FLOOR(num) - Returns the largest integer less than or equal to the input number.
SQRT(num) - Returns the square root of the input number.
EXP(num) - Returns e raised to the power of the input number.
MOD(num1, num2) - Returns the remainder of num1 divided by num2.
TRUNC(num,[decimal_places])- Returns the input number truncated to the specified number of decimal
places (0 if not specified).
CEIL(num) - Returns the smallest integer greater than or equal to the input number.
GREATEST(num1, num2, ...) - Returns the largest value among the input numbers.
2K21/CO/262 CO202 Laboratory Manual

IMPLEMENTATION (Inbuilt Functions)


-- ASCII
SELECT ASCII('A') AS ascii_code; -- Returns 65
-- LENGTH
SELECT LENGTH('Hello world!') AS str_length; -- Returns 12
-- LTRIM
SELECT LTRIM(' Hello world!') AS trimmed_str; -- Returns 'Hello world!'
-- LPAD
SELECT LPAD('Hello', 10, '-') AS padded_str; -- Returns '--Hello'
-- RPAD
SELECT RPAD('Hello', 10, '-') AS padded_str; -- Returns 'Hello-----'
-- VSIZE
SELECT VSIZE('Hello world!') AS byte_size; -- Returns 12
-- POWER
SELECT POWER(2, 3) AS power_result; -- Returns 8
-- MAX
SELECT MAX(salary) AS max_salary FROM employees; -- Returns the maximum salary of all employees
-- MIN
SELECT MIN(salary) AS min_salary FROM employees; -- Returns the minimum salary of all employees
-- LOWER
SELECT LOWER('Hello World!') AS lower_str; -- Returns 'hello world!'
-- INITCAP
SELECT INITCAP('hello world!') AS initcap_str; -- Returns 'Hello World!'
-- UPPER
SELECT UPPER('Hello World!') AS upper_str; -- Returns 'HELLO WORLD!'
-- SUBSTR
SELECT SUBSTR('Hello world!', 1, 5) AS substr_str; -- Returns 'Hello'
-- ABS
SELECT ABS(-10) AS abs_num; -- Returns 10
-- POWER
SELECT POWER(2, 3) AS power_result; -- Returns 8
-- ROUND
SELECT ROUND(3.14159, 2) AS rounded_num; -- Returns 3.14
-- LEAST
SELECT LEAST(10, 5, 20, 15) AS least_num; -- Returns 5
-- RESULT
SELECT salary * bonus_rate AS bonus FROM employees; -- Returns the bonus amount for each employee
based on their salary and bonus rate
-- FLOOR
SELECT FLOOR(3.7) AS floor_num; -- Returns 3
-- SQRT
SELECT SQRT(16) AS sqrt_num; -- Returns 4
-- EXP
SELECT EXP(1) AS exp_num; -- Returns 2.71828...
-- MOD
SELECT MOD(10, 3) AS mod_result; -- Returns 1
-- TRUNC
SELECT TRUNC(3.14159, 2) AS trunc_num; -- Returns 3.14
-- CEIL
SELECT CEIL(3.1) AS ceil_num; -- Returns 4
-- GREATEST
SELECT GREATEST(10, 5, 20, 15) AS greatest_num; -- Returns 20
2K21/CO/262 CO202 Laboratory Manual

THEORY (Views)
A view is a virtual table based on the result set of a SQL statement. Views are not actual tables, but
instead they are stored as pre-compiled SQL statements that can be accessed like tables. Views are
useful in many ways, including simplifying complex queries, providing a layer of security, and
separating the presentation of data from its underlying structure.
Syntax: " CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;"
In this syntax, view_name is the name of the view being created, column1, column2, ... are the columns to
include in the view, table_name is the name of the table or tables to use in the view, and condition is any
additional conditions to apply to the view.
2K21/CO/262 CO202 Laboratory Manual

IMPLEMENTATION (Inbuilt Functions)


TABLE: Faculty

We have to view that only shows the name and salary of faculty in the Computer Science department.

This gives the following output:


2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 6
28/03/2023
AIM
Implement the nested queries and various operations on joins
THEORY (Nested Queries)
Nested queries are also known as subqueries, which are used to retrieve data from one table based
on the conditions specified in another table. In SQL, a subquery is a query that is nested within
another query and is enclosed in parentheses. The subquery is executed first, and the result of the
subquery is then used in the main query to perform further filtering or calculations.

IMPLEMENTATION (Nested Queries)


Table: customers, orders

NESTED QUERY
SELECT name
FROM customers
WHERE id IN (
SELECT customer_id
FROM orders
WHERE product_name = 'Mobile Phone'
);
2K21/CO/262 CO202 Laboratory Manual

THEORY (Joins)
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them. The SQL Joins clause is used to combine records from two or more tables in a
database. A JOIN is a means for combining fields from two tables by using values common to each.

INNER JOIN: returns only the matching rows from both tables
Syntax: "SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column".

LEFT JOIN: returns all the rows from the left table and matching rows from the
right table
Syntax: "SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column".

RIGHT JOIN: returns all the rows from the right table and matching rows from
the left table
Syntax: "SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column".

FULL OUTER JOIN: returns all the rows from both tables, with NULL values for
non-matching rows
Syntax: "SELECT * FROM table1
FULL OUTER JOIN table2 ON table1.column = table2.column".

IMPLEMENTATION (Joins)
Table: customers, orders

INNER JOIN:
2K21/CO/262 CO202 Laboratory Manual

LEFT JOIN:

RIGHT JOIN:

FULL OUTER JOIN:


2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 6
8/03/2023
AIM
Introduction to PL/SQL

THEORY
PL/SQL stands for “Procedural Language extensions to the Structured Query Language”. PL/SQL adds many
procedural constructs to SQL language to overcome some limitations of SQL. Besides, PL/SQL provides a
more comprehensive programming language solution for building mission-critical applications on Oracle
Databases.

PL/SQL is a highly structured and readable language. Its constructs express the intent of the code clearly.

DECLARE is used to introduce a section of the PL/SQL block where variables, constants, cursors, and other
program elements are declared. These declarations are separated by semicolons and are not executable
statements. They are used to specify the data types and initial values for variables, the parameters and
return types for functions and procedures, and so on.

BEGIN is used to introduce the executable section of the PL/SQL block. The statements written between
BEGIN and END are executed in the order they appear. This section can contain any SQL or PL/SQL
statements, including control structures such as loops, conditional statements, and exceptions.

END marks the end of the PL/SQL block. It is required to terminate the block and must be preceded by a
semicolon (;) to separate it from the last statement in the executable section.

Syntax:
DECLARE

-- variable declarations
BEGIN
-- executable statements
WHILE i <= var LOOP
-- do something
IF i = 15 THEN
-- handle a specific case
ELSIF i = 20 THEN
-- handle another specific case
ELSE
-- do something else
NULL;
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
-- handle any other exceptions
END;
2K21/CO/262 CO202 Laboratory Manual

EXPERIMENT 8
11/04/2023
AIM
Write a Program to reverse a number by taking input from user

IMPLEMENTATION
DECLARE
-- variable declarations
num NUMBER := 0;
rev_num NUMBER := 0;
remainder NUMBER := 0;
BEGIN
-- get user input
num := &Enter_Number;

-- reverse the number


WHILE num > 0 LOOP
remainder := num MOD 10;
rev_num := (rev_num * 10) + remainder;
num := num / 10;
END LOOP;

-- print the reversed number


DBMS_OUTPUT.PUT_LINE('The reversed number is ' || rev_num);
END;

OUTPUT

You might also like