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

DBMS Lab Report

This document is a lab report submitted to Tribhuvan University by Shishir Dhakal, detailing various aspects of Database Management Systems. It includes multiple lab reports covering topics such as DDL and DML commands, aggregate functions, views, joins, and stored procedures. The report serves as part of the requirements for a Bachelor's degree in Computer Application.

Uploaded by

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

DBMS Lab Report

This document is a lab report submitted to Tribhuvan University by Shishir Dhakal, detailing various aspects of Database Management Systems. It includes multiple lab reports covering topics such as DDL and DML commands, aggregate functions, views, joins, and stored procedures. The report serves as part of the requirements for a Bachelor's degree in Computer Application.

Uploaded by

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

TRIBHUVAN UNIVERSITY

FACULTY OF HUMANITIES AND SOCIAL SCIENCE

A Lab Report of
Database Management System

Submitted to
Department of Computer Application
Southwestern State College, Basundhara, Kathmandu

Submitted By
Shishir Dhakal

In partial fulfillment of the requirements for the Bachelors in


Computer Application
11 March 2024
Table of Contents
Table of Contents....................................................................................................ii

Lab Report 1:...........................................................................................................5

Lab Report 2:...........................................................................................................6


DDL command and table creation...........................................................................................6

Lab Report 3:...........................................................................................................7


DDL Commands and DML Commands..................................................................................7

1) Insert..............................................................................................................................7

2) Delete.............................................................................................................................7

3) Update...........................................................................................................................8

4) Create.............................................................................................................................8

5) Drop...............................................................................................................................8

6) Truncate.........................................................................................................................8

Lab Report 4............................................................................................................9


Data Control Language Commands:.......................................................................................9

1) Grant..............................................................................................................................9

2) Revoke...........................................................................................................................9

Transaction Control Language Commands............................................................................9

1) Setpoint..........................................................................................................................9

2) Rollback.......................................................................................................................10

Lab Report 5..........................................................................................................10


Aggregate Functions:...............................................................................................................10

1) AVG()..........................................................................................................................10

2) COUNT().....................................................................................................................10
3) MAX().........................................................................................................................11

4) MIN()...........................................................................................................................11

5) SUM()..........................................................................................................................11

Lab Report 6..........................................................................................................11


Commands:..............................................................................................................................11

1) Use of OR....................................................................................................................11

2) Use of AND.................................................................................................................12

3) Use of IN.....................................................................................................................12

4) Use of BETWEEN......................................................................................................12

5) Use of ORDER BY.....................................................................................................12

6) Use of Group By..........................................................................................................13

Lab Report 7..........................................................................................................13


VIEW in Database:..................................................................................................................13

1) Creating View:............................................................................................................13

2) Insert from View.........................................................................................................14

3) Delete from View........................................................................................................14

4) Update from View.......................................................................................................14

Lab Report 8..........................................................................................................15


Types of Join:...........................................................................................................................15

1) Inner Join:....................................................................................................................15

2) Right Outer Join:.........................................................................................................15

3) Left Outer Join:...........................................................................................................16

4) Full Outer Join:............................................................................................................16

Lab Report 9 and 10..............................................................................................17


Store Procedure.......................................................................................................................17

1) Create, Alter, Drop and Execute Stored Procedure:....................................................17

2) Passing Data to Stored Procedure:..............................................................................17

3) Returning Data from Stored Procedure:......................................................................18

4) Procedure Output Parameter using the return statement:............................................18

5) Creating and Disabling Triggers:................................................................................18

6) Multi-Row Enabled Trigger:.......................................................................................19

7) Creating and Dropping Index:.....................................................................................19


Lab Report 1:
ER Diagram Scenario: Students are attending to different course in college. Each Course Has
different subject which are teach by different lecturer (one subject can be teach by only one
lecturer). One student can be teach by many students. Course is taken by many lecturers.
Students have their own property (id,name, address(address can be street,city,pin,door),age,
hobby, birth date etc). Course contain course id and course name, subject have subject_id and
name and lecturer poses their properties as lecturer_id,name,address. Note: Assume your
own attributes and data types further as per your requirement. Design an ER diagram for the
above scenario.
ER Diagram:

5
Lab Report 2:
DDL command and table creation
Course Table:

Student Table:
Lab Report 3:
DDL Commands and DML Commands
1) Insert
Description: INSERT is a DML (Data Manipulation Language) command used to add new
records or rows into a table.
Syntax:
INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary)
VALUES (101, 'John', 'Doe', 50000);
2) Delete
In SQL, the DELETE statement is used to remove rows from a table based on specified
criteria. It's a DML (Data Manipulation Language) command.
Syntax:
DELETE FROM Employees
WHERE EmployeeID = 101;
3) Update
Description: UPDATE is a DML command used to modify existing records in a table based
on specified conditions.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
4) Create
Description: The CREATE command is a DDL (Data Definition Language) command used
to create new database objects such as tables, indexes, views, or schemas.
Syntax:
CREATE OBJECT_TYPE object_name (column1 datatype, column2 datatype, ...);
5) Drop
Description: DROP is a DDL (Data Definition Language) command used to remove an
entire table or database object.
Syntax:
DROP TABLE table_name;
6) Truncate
Description: TRUNCATE is a DDL command used to remove all records from a table,
while keeping the table structure intact.
Syntax:
TRUNCATE TABLE table_name;
Lab Report 4
Data Control Language Commands:
1) Grant
Description: GRANT is a DCL command used to grant specific privileges or permissions to
database users or roles. These privileges may include SELECT, INSERT, UPDATE,
DELETE, or other operations on database objects like tables, views, or procedures.
Syntax:
GRANT privileges ON object TO user;
Example:
GRANT SELECT, INSERT ON Employees TO user1;
2) Revoke
Description: REVOKE is a DCL command used to revoke previously granted privileges
from database users or roles. It removes the specified privileges, restricting access to certain
database objects.
Syntax:
REVOKE privileges ON object FROM user;
Example:
REVOKE INSERT ON Employees FROM user1;

Transaction Control Language Commands


1) Setpoint
Description: SETPOINT is a TCL command used to set a savepoint within a transaction.
Savepoints allow you to mark a specific point within a transaction so that you can later roll
back to that point if needed, without rolling back the entire transaction.
Syntax:
SET SAVEPOINT savepoint_name;
Example:
SET SAVEPOINT before_update;
2) Rollback
Description: ROLLBACK is a TCL command used to roll back a transaction to a specified
savepoint or the beginning of the transaction. It undoes all changes made since the specified
savepoint or the start of the transaction.
Syntax:
ROLLBACK TO SAVEPOINT savepoint_name;
Example:
ROLLBACK TO SAVEPOINT before_update;

Lab Report 5
Aggregate Functions:
1) AVG()
Description: AVG () calculates the average value of a numeric column in a table. It adds up
all the values in the column and divides the sum by the total number of values.
Syntax:
SELECT AVG (column_name) FROM table_name;
Example:
SELECT AVG(salary) FROM employees;
2) COUNT()
Description: COUNT() counts the number of rows that meet a specified condition. It can
count all rows in a table or only those that satisfy certain criteria.
Syntax:
SELECT COUNT(column_name) FROM table_name WHERE condition;
Example:
SELECT COUNT(employee_id) FROM employees WHERE department_id = 1;
3) MAX()
Description: MAX() retrieves the maximum value from a column in a table.
Syntax:
SELECT MAX(column_name) FROM table_name;
Example:
SELECT MAX(salary) FROM employees;
4) MIN()
Description: MIN() retrieves the minimum value from a column in a table.
Syntax:
SELECT MIN(column_name) FROM table_name;
Example:
SELECT MIN(salary) FROM employees;
5) SUM()
Description: SUM() calculates the sum of values in a numeric column in a table.
Syntax:
SELECT SUM(column_name) FROM table_name;
Example:
SELECT SUM(sales_amount) FROM sales;

Lab Report 6
Commands:
1) Use of OR
Description: OR is a logical operator used in the WHERE clause of a SQL query to retrieve
rows that satisfy at least one of the specified conditions.
Syntax:
SELECT * FROM table_name WHERE condition1 OR condition2;
Example:
SELECT * FROM employees WHERE department_id = 1 OR department_id = 2;
2) Use of AND
Description: AND is a logical operator used in the WHERE clause of a SQL query to
retrieve rows that satisfy all of the specified conditions.
Syntax:
SELECT * FROM table_name WHERE condition1 AND condition2;
Example:
SELECT * FROM employees WHERE department_id = 1 AND salary > 50000;
3) Use of IN
Description: IN is a logical operator used in the WHERE clause of a SQL query to retrieve
rows where a specified column value matches any value in a list.
Syntax:
SELECT * FROM table_name WHERE column_name IN (value1, value2, ...);
Example:
SELECT * FROM employees WHERE department_id IN (1, 2, 3);
4) Use of BETWEEN
Description: BETWEEN is a comparison operator used in the WHERE clause of a SQL
query to retrieve rows where a specified column value is within a range of values.
Syntax:
SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2;
Example:
SELECT * FROM employees WHERE salary BETWEEN 50000 AND 100000;
5) Use of ORDER BY
Description: ORDER BY is a clause used in a SELECT statement to sort the result set based
on one or more columns in ascending or descending order.
Syntax:
SELECT * FROM table_name ORDER BY column_name [ASC|DESC];
Example:
SELECT * FROM employees ORDER BY hire_date DESC;
6) Use of Group By
Description: GROUP BY is a clause used in a SELECT statement to group rows that have
the same values into summary rows and to apply aggregate functions such as COUNT, SUM,
AVG, etc., to each group.
Syntax:
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;
Example:
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id;

Lab Report 7
VIEW in Database:
1) Creating View:
Description: A view is a virtual table generated by a query that allows you to retrieve data
from one or more tables or other views. It provides a way to present data in a structured
format without storing it physically in the database.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE VIEW EmployeeView AS
SELECT employee_id, last_name, first_name, department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
2) Insert from View
Description: It's possible to insert data into the underlying tables of a view if certain
conditions are met, such as the view being based on a single table and not containing any
derived columns.
Syntax:
INSERT INTO view_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO EmployeeView (employee_id, last_name, first_name, department_name)
VALUES (101, 'Doe', 'John', 'IT');
3) Delete from View
Description: Deleting data from a view is possible if the view only references one table and
doesn't contain any derived columns or aggregate functions.
Syntax:
DELETE FROM view_name
WHERE condition;
Example:
DELETE FROM EmployeeView WHERE employee_id = 101;
4) Update from View
Description: Updating data through a view is possible if certain conditions are met, such as
the view being based on a single table and not containing any derived columns or aggregate
functions.
Syntax:
UPDATE view_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE EmployeeView SET department_name = 'HR' WHERE employee_id = 101;
Lab Report 8
Types of Join:
1) Inner Join:
Description: Inner join retrieves records from two tables where there is a match based on a
specified condition. It returns only the rows that have matching values in both tables.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
Example:
SELECT employees.employee_id, employees.last_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
2) Right Outer Join:
Description: Right outer join returns all records from the right table (table2) and the
matching records from the left table (table1). If there are no matches, NULL values are
returned for the columns from the left table.
Syntax:
SELECT columns
FROM table1
RIGHT OUTER JOIN table2 ON table1.column_name = table2.column_name;
Example:
SELECT employees.employee_id, employees.last_name, departments.department_name
FROM employees
RIGHT OUTER JOIN departments ON employees.department_id =
departments.department_id;
3) Left Outer Join:
Description: Left outer join returns all records from the left table (table1) and the matching
records from the right table (table2). If there are no matches, NULL values are returned for
the columns from the right table.
Syntax:
SELECT columns
FROM table1
LEFT OUTER JOIN table2 ON table1.column_name = table2.column_name;
Example:
SELECT employees.employee_id, employees.last_name, departments.department_name
FROM employees
LEFT OUTER JOIN departments ON employees.department_id =
departments.department_id;
4) Full Outer Join:
Description: Full outer join returns all records when there is a match in either the left table
(table1) or the right table (table2). If there are no matches, NULL values are returned for the
columns from the table without a match.
Syntax:
SELECT columns
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
Example:
SELECT employees.employee_id, employees.last_name, departments.department_name
FROM employees
FULL OUTER JOIN departments ON employees.department_id =
departments.department_id;
Lab Report 9 and 10
Store Procedure
1) Create, Alter, Drop and Execute Stored Procedure:
Description: Stored procedures are precompiled SQL code that can be executed on demand.
They can accept parameters, perform actions, and return results.
Commands:
Create:
CREATE PROCEDURE procedure_name
AS
-- SQL statements
Alter:
CREATE PROCEDURE procedure_name
AS
-- SQL statements
Drop:
DROP PROCEDURE procedure_name;
Execute:
EXEC procedure_name;
2) Passing Data to Stored Procedure:
Description: Parameters can be passed to a stored procedure to customize its behavior or
provide input data.
Syntax:
CREATE PROCEDURE procedure_name (@parameter1 datatype, @parameter2
datatype, ...)
AS
-- SQL statements using parameters
3) Returning Data from Stored Procedure:
Description: Stored procedures can return result sets using SELECT statements.
Syntax:
CREATE PROCEDURE procedure_name
AS
BEGIN
SELECT column1, column2, ...
FROM table_name
WHERE condition;
END
4) Procedure Output Parameter using the return statement:
Description: The RETURN statement in a stored procedure is used to return a status value to
the calling program or script.
Syntax:
CREATE PROCEDURE procedure_name (@parameter1 datatype, @parameter2 datatype,
@output_parameter datatype OUTPUT)
AS
BEGIN
-- SQL statements
SET @output_parameter = value;
RETURN @output_parameter;
END
5) Creating and Disabling Triggers:
Description: Triggers are special types of stored procedures that are automatically executed
in response to specific events on a table.
Commands:
Create:
CREATE TRIGGER trigger_name
ON table_name
AFTER INSERT, UPDATE, DELETE
AS
-- SQL statements
Disable:
DISABLE TRIGGER trigger_name ON table_name;
6) Multi-Row Enabled Trigger:
Description: Triggers can be configured to handle multiple rows affected by an insert,
update, or delete operation.
Example:
CREATE TRIGGER trigger_name
ON table_name
AFTER INSERT, UPDATE, DELETE
AS
-- SQL statements that handle multiple rows
7) Creating and Dropping Index:
Description: Indexes are database objects used to speed up the retrieval of data from a table
by providing quick access to rows.
Commands:
Create:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Drop:
DROP INDEX index_name;

You might also like