Study
Study
Laboratory Manual
INDEX
S Topic Date
No.
4 To implement the set operations and clauses like where, order 28/01/2023
by, group by, having operations.
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
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
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
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
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:
UNION
2. UNION ALL:
UNION ALL
3. INTERSECT:
INTERSECT
MINUS
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.
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.
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
We have to view that only shows the name and salary of faculty in the Computer Science department.
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.
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:
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;
OUTPUT