CS 502 - DBMS - LABORATORY MANUAL - Final - 1659942271 - 1694237975
CS 502 - DBMS - LABORATORY MANUAL - Final - 1659942271 - 1694237975
LABORATORY MANUAL
V SEM (CSE)
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
CERTIFICATE
Bhopal for V Semester of the Computer Science and Engineering Department during the year 2021-22.
Signature of
Faculty In-Charge
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
2022-23
List of Experiments
6. Write a query to display the 3rd, 4th and 9th row from the table
Aim: To acquire knowledge about delete command and perform delete operation
Theory:
MySQL provides the facility to delete duplicate rows with the help of the delete operation
along with the join command.
The following statement deletes duplicate rows and keeps the highest id -
Syntax:
Delete t1 from contacts t1 inner join contacts t2 where t1.id < t2.id and t1.email = t2.email;
Query: -
Viva Questions:
1. Explain the procedure to delete the duplicate rows from a table.
2. Explain count () and count (*).
3. What is ROWID? Can ROWID be used as a primary key while creating a table?
4. Explain the purpose of the “group by” clause.
5. What is the purpose of the “having” clause?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -2-
EXPT. No. -2: Write a query to display alternate rows of the table
Aim: To acquire knowledge about select command and perform display operation
Theory:
ROWID
1. ROWID is nothing but the physical memory location on which that data/row is stored.
ROWID returns the address of the row.
2. ROWID uniquely identifies a row in the database.
3. ROWID is a combination of data object number, data block in a data file, the position of
row, and a data file in which row resides.
4. ROWID is a 16-digit hexadecimal number whose data type is also ROWID or UROWID.
5. The fastest way to access a single row is ROWID.
6. ROWID is a unique identifier of the ROW.
ROWNUM
1. ROWNUM is a magical column in oracle which assigns the sequence number to the rows
retrieved in the table.
2. To limit the values in the table user can use the ROWNUM pseudo column.
3. ROWNUM is nothing but a logical sequence number given to the rows fetched from the
table.
4. ROWNUM is a logical number assigned temporarily to the physical location of the row.
5. User can limit the values in the table using ROWNUM.
6. ROWNUM is also a unique temporary sequence number assigned to that row.
select * from emp where ROWID in (select decode (mod (ROWNUM, 2), 0, ROWID) from
emp);
Or
select * from dept where ROWID in (select decode (mod (ROWNUM, 2), 0, ROWID) from
dept);
Query: -
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -3-
Viva Questions:
1. Explain the three components of ROWID.
2. What is meant by “decode” function?
3. What is the procedure to count the number of records using the ROWNUM identifier?
4. What is meant by multi-row operators?
5. Explain the use of the ROWNUM identifier.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -4-
EXPT. No. -3. Write a query to delete alternate rows from the table
Aim: To acquire knowledge about delete command and perform delete operation on
alternate rows
Theory:
Delete: Delete clause is used to remove rows from the table based on the condition.
Syntax:
Delete from table name where <condition>.
Delete from dept where ROWID IN (Delete decode (mod (ROWNUM, 2), 0, ROWID) from
dept);
Query: -
Viva Questions:
1. Describe the delete and truncate command.
2. Explain the procedure to delete a column from the table.
3. Explain the drop and delete command.
4. What is the procedure to recover the deleted rows from a table?
5. Explain the command to delete multiple columns from the table.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -5-
EXPT. No. -4. Write a query to update multiple rows by using a single update statement
Aim: To acquire knowledge about update command and perform the update operation
Theory:
The update clause is used to update records in the table. Using case functions, multiple
rows can be updated.
Syntax
update table_name set column_name=new_value, column2=value where <condition>
CASE Function: The CASE function lets user to evaluate conditions and return a value when
the first condition is met (like an IF-THEN-ELSE statement).
CASE expression
WHEN condition1 THEN result1; WHEN condition2 THEN result2; WHEN condition N THEN
result N; ELSE result;
END;
Display all the details where dept is either sales or research.
select * from emp where dname = any (select dname from emp where dname = ‘sales’ or
dname = ‘research’);
select * from emp where dname = any (select dname from emp where dname like
(‘sales’,’research’));
Query: -
Viva Questions:
1. What is the use of an update command?
2. What is the CASE function in MySQL?
3. Explain the procedure to update multiple rows using a single update statement.
4. Explain the various DML commands.
5. How does the nested CASE statement work in MySQL?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -6-
EXPT. No. -5. Write a query to find the third-highest and third-lowest salary paid
Aim: To understand the concept of aggregate functions
Theory:
Similar to TOP, MySQL also supports the keyword LIMIT, which provides pagination
capability.
Nth highest salary in MySQL using LIMIT clause -
Select salary from employee order by salary DESC LIMIT n-1,1
Explanation:
The benefit of this approach is that it is faster than the correlated query approach but it is
vendor dependent. This solution will work only in the MySQL database.
select max (sal) from emp where sal< (select max (sal) from emp where sal< (select max
(sal) from emp));
Display from Nth Row
select * from dept where ROWID NOT IN (select ROWID from dept where ROWNUM<=
(select count (*)-&N from dept));
Query: -
Viva Questions:
1. What is the use of the keyword LIMIT?
2. Explain the importance of order by clause in MySQL query.
3. What is meant by the terms ASC and DSC?
4. Explain the various aggregate functions used in MySQL.
5. What is the use of the clause ‘where’?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -7-
EXPT. No. -6. Write a query to display the 3rd, 4th and 9th row from the table
Aim: To understand the concept of different clauses as well as apply them to display the
specific rows from a table
Theory:
Following three clauses can be used to display specific rows from the table-
WHERE: This allows a user to request information from database objects with certain
characteristics. For instance, a user can request the name of customers who live in California
or a user can list only products of a certain category.
LIMIT: This allows a user to limit the number of rows from which information can be
retrieved. For instance, a user can request the information from only the first three rows of
a particular table.
DISTINCT: This allows a user to request information from only one of the identical rows.
For instance, in a Login table, a user can request login Names with specification of no
duplicate names, thus limiting the response to one record for each member.
Syntax:
select * from (select ROWNUM r, e.* from dept d) where r = 3;
Query: -
Viva Questions:
1. What is the use of the clause DISTINCT?
2. Describe the use of the keyword “in”.
3. Explain the importance of the clause “having”.
4. Explain the use of the clause “limit”.
5. Describe the importance of the clause “having”.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -8-
EXPT. No. -7. Write a query to display the names of employee’s whose names start with j,
k, l or m
Viva Questions:
1. What is the use of wildcard in MySQL?
2. Describe the use of the keyword ESCAPE.
3. What is the use of underscore wildcard?
4. Explain percentage wildcard character.
5. What is the use of the keyword “NOT LIKE”?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -10-
EXPT. No. -8. Write a query to show all the employees who were hired during the first half
of the month
Aim: To understand the concept of the date feature and apply it to display results based on
the user query
Theory:
Table 8.1: Date Functions ()
Viva Questions:
1. Which function is used to format the date?
2. What is the procedure to extract day and month from date?
3. Write a query to find all the employees hired on a specific day.
4. Explain the two formats of the date in MySQL.
5. What is the range of date data type?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -11-
EXPT. No. -9. Write a query to display three records in the first row, two records in the
second row and one record in the third row in a single SQL statement
Aim: To understand the concept of select command with different clauses
Theory:
The SQL select TOP statement is used to retrieve records from one or more tables in a
database and limit the number of records returned based on a fixed value or percentage.
Parameters or Arguments TOP (top_value):
It will return the top number of rows in the result set based on top_value. For example, TOP
(10) would return the top 10 rows from the full result set.
PERCENT:
If PERCENT is specified, then the top rows will be based on a percentage of the total result
set (as specified by the top_value). For example, TOP (10) PERCENT would return the top 10%
of the full result set.
Expressions:
The columns or calculations that the user wishes to retrieve
Tables:
For the tables that the user wishes to retrieve records from, there must be at least one table
listed in the from clause.
WHERE conditions:
The conditions must be met for the records to be selected.
ORDER BY expression:
It is used in the select TOP statement so that the user can arrange the result in a particular
order and target those records that the user wishes to return.
Syntax:
select TOP (top_value)
[PERCENT] expressions
from
tables [where conditions]
[ORDER BY expression [ ASC | DESC]];
Query: -
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -12-
Viva Questions:
1. Describe the clause “GROUP BY” in MySQL.
2. Explain the clause “ORDER BY” in MySQL.
3. What is the use of the keyword ‘limit’ in MySQL?
4. Explain the significance of the inner query.
5. Under what circumstances the clause TOP is used with the insert statement?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -13-
EXPT. No. -10. Write a SQL statement for ROLLBACK, COMMIT and SAVE-POINT
Aim: To understand the concept of ROLLBACK, COMMIT and SAVEPOINT
Theory:
ROLLBACK/ SAVEPOINT/COMMIT
All these statements fall in the category of Transaction Control Statements.
ROLLBACK: The ROLLBACK command is the transactional command used to undo the
transactions that have not already been saved to the database. This command can only be
used to undo transactions since the last COMMIT or ROLLBACK command was issued.
MySQL->ROLLBACK TO SAVEPOINT_NAME;
SAVEPOINT: A SAVEPOINT is a point in a transaction when the user can roll the transaction
back to a certain point without rolling back the entire transaction.
MySQL->SAVEPOINT SAVEPOINT_NAME;
COMMIT: The COMMIT command is the transactional command used to save changes
invoked by a transaction to the database. The COMMIT command saves all the transactions
to the database since the last COMMIT or ROLLBACK command.
MySQL->COMMIT;
Query: -
Viva Questions:
1. What is the procedure to ROLLBACK the changes in a transaction?
2. Explain the use of the command COMMIT.
3. Describe the significance of SAVEPOINT with its uses in DBMS.
4. Explain the use of SAVEPOINT in a transaction.
5. Explain the process to roll back DDL commands.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -14-
EXPT. No. -11. Write a PL/SQL to select, insert, update and delete statements
Aim: To understand the concept of PL/SQL to perform select, insert, update and delete
operations
Theory:
PL/SQL is a procedural extension of SQL, making it extremely simple to write procedural code
that includes SQL as if it was a single language. The basic unit in PL/SQL is a block, all PL/SQL
programs are made up of blocks, which can be nested within each other. Typically, each block
performs a logical action in the program.
Query: -
Viva Questions:
Viva Questions:
1. Explain the concept of the parameters IN and OUT.
2. What are external procedures and when are they used?
3. Explain the use of stored procedure.
4. Describe the basic syntax of stored procedures in PL/SQL.
5. What is the use of a stored procedure?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -16-
EXPT. No. -13. Write a query to display the name and hiring date of all employees using a
cursor
Aim: To understand the concept of cursor
Theory:
Cursor: A cursor is a variable that runs through the tuples of some relation. This relation can
be a stored table, or it can be the answer to some query. By fetching into the cursor each
tuple of the relation, a user can write a program to read and process the value of each such
tuple. If the relation is stored, a user can also update or delete the tuple at the current cursor
position.
Query: -
Viva Questions:
1. What are the main features of a cursor?
2. Explain different types of cursors used in PL/SQL.
3. What are the drawbacks of using an implicit cursor?
4. Explain the attributes of an explicit cursor.
5. Describe the procedure to display the tuple using a cursor.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -17-
EXPT. No. -14. Write a query to display details of the first 5 highly paid employees using a
cursor
Aim: To display the limited data with conditions with the help of cursor
Theory:
To handle a result set inside a stored procedure, a cursor is required. A cursor allows a user
to iterate a set of rows returned by a query and process each row individually. MySQL cursor
is read-only, non-scrollable and asensitive.
Read-only: User cannot update data in the underlying table through the cursor.
Non-scrollable: User can only fetch rows in the order determined by the SELECT statement.
Users cannot fetch rows in the reversed order. In addition, users cannot skip rows or jump
to a specific row in the result set.
There are two types of cursors i.e., Asensitive cursor and Insensitive cursor. An asensitive
cursor points to the actual data and due to this reason, they perform faster, whereas an
insensitive cursor uses a temporary copy of the data. However, any change that is made to
the data from other connections will affect the data that is being used by an asensitive cursor,
therefore, it is safer if a user does not update the data that is being used by an asensitive
cursor.
Query: -
Viva Questions:
Viva Questions:
1. Explain triggers along with their types.
2. What is the use of “instead of” in the DML trigger?
3. Describe the use of “After trigger”.
4. Explain the three basic parts of a trigger.
5. What is a triggering event?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -19-
EXPT. No. -16. Write a query for database trigger which acts just like a primary key and
does not allow duplicate records
Aim: To understand the advanced concepts of a trigger
Theory: Before Insert Trigger: As the name implies, this trigger is invoked before an insert
statement is executed. A user cannot create a BEFORE trigger on a view.
Query: -
Viva Questions:
1. Explain DDL triggers along with their types.
2. Describe the role of REPLACE TRIGGER.
3. Explain the use of “logon trigger”.
4. Why does the trigger fire multiple times in a single login?
5. What is the procedure to use an if-else statement while declaring the trigger?
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -20-
EXPT. No. -17. Write a query to create a database trigger which performs the action of an
on delete cascade
Aim: To understand the concept of on delete cascade trigger
Theory: A Trigger that contains statements that cause invoking of other triggers is known as
cascading triggers. The orders of execution of statements in case of cascading triggers are -
• Execute all BEFORE statement triggers that apply to the current statement.
• Loop for each row affected statement.
• Execute all BEFORE row triggers that apply to the current statement in the loop.
• Lock and change row, perform integrity constraints check, release the lock.
• Execute all AFTER row triggers that apply to the current statement.
• Execute all AFTER statement triggers that apply to the current statement.
After Delete Trigger: As the name implies, this trigger is invoked after a delete occurs or
after a delete operation is implemented.
Query: -
Viva Questions:
1. Explain the various types of constraints.
2. What is the function of ‘on delete cascade’?
3. What kind of triggers are stored in a database?
4. Explain the execution of cascading triggers.
5. Describe ‘on delete cascade’ and ‘on update cascade’.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
DATABASE MANAGEMENT SYSTEMS LABORATORY -21-
EXPT. No. -18. Write a query for database trigger which should not delete any record from
the employee table if the day is Sunday
Aim: To understand the concept of special cases in database trigger
Theory:
Before Delete Trigger: As the name implies, this trigger is invoked before a delete occurs, or
before the deletion statement is implemented. It is also used to delete data from another
table.
Query: -
Viva Questions:
1. What is the procedure to invoke a trigger on user requirements?
2. Explain the significance of ‘before delete trigger’.
3. What is the use of TO_CHAR()?
4. Describe the process to delete a trigger.
5. Explain the purpose of using ‘before delete trigger’.
CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING