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

CS3481 - LAB Exercises

The document outlines a series of lab exercises for a Database Management Systems course, covering SQL commands, table creation, constraints, foreign keys, aggregate functions, joins, triggers, views, and user-defined functions. Each exercise includes specific tasks such as creating tables, inserting records, and executing queries to manipulate and retrieve data. The exercises aim to provide hands-on experience with various database operations and SQL syntax.

Uploaded by

antoniyajeswin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

CS3481 - LAB Exercises

The document outlines a series of lab exercises for a Database Management Systems course, covering SQL commands, table creation, constraints, foreign keys, aggregate functions, joins, triggers, views, and user-defined functions. Each exercise includes specific tasks such as creating tables, inserting records, and executing queries to manipulate and retrieve data. The exercises aim to provide hands-on experience with various database operations and SQL syntax.

Uploaded by

antoniyajeswin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CS3481 - DATABASE MANAGEMENT SYSTEMS

LAB EXERCISES

EX 1:
create a database table, add constraints (primary key, unique, check, Not null), insert rows,
update and delete rows using SQL DDL and DML commands.

Q1: Create a table called EMP with the following structure.


Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Allow NULL for all columns except ename and job.

Q2: Add a column experience to the emp table. experience numeric null is allowed.
Q3: Modify the column width of the job field of emp table.
Q4: Create dept table with the following structure.
Name Type
------------ ---------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
Deptno as the primary key
Q5: Create the emp1 table with ename and empno, add constraints to check whether the
empno value while entering is greater than 100 (i.e) empno >100.
Q6:Drop a column experience from the emp table
Q7: Truncate the emp table
Q8: Insert more records into the employee table.
Q9: Insert a single record into dept table.
Q10: Update the emp table to set the salary of all employees to Rs 20,000
Q11: Delete only those who are working as “Lecturer”
EX 2: FOREIGN KEY CONSTRAINT AND REFERENTIAL INTEGRITY

AIM:
To Create a set of tables, add foreign key constraints and incorporate referential integrity

1) Create a dept table(dno,dname)


Create a student table (rollno,name, marks,dno)
Set referential integrity for the same

2. a. Customer (Cust_id, Cust_name, Addr, ph_no,pan_no)


b. Loan (Loan_id, Amount. Interest, Cust_Id)

3. Create the following table with the mapping given below.


a. stu_details(reg_no, stu_name, DOB, address, city)
b. mark_details(reg_no, mark1, mark2, mark3, total)

4. Create the following tables


CUSTOMER(custno, custname, city, phone)
ITEM(itemno, itemname, itemprice, quantity)
INVOICE(invno, invdate, custno)
INVITEM(invno, itemno, quantity)

5. Consider the following relational schema for a Product Sales


database application
Product (Prodid, Prodesc, Price, Stock)
Purchase (Purid, Proid, qty, supplierName)
Sales (Saleid, Proid, qty, custname)
EX 3: WHERE CLAUSE AND AGGREGATE FUNCTIONS IN SQL

1. Create a table for employee with empid varchar(10), empname varchar(20)


2. Add a column to this table as DateOfJoining
3. Insert records into this table using Insert Command
4. Create a pseudo table employee with the same structure as the table emp and insert
rows into the table using select clauses.
5. List the records in the emp table in ascending order of empid
6. Display only those employees whose empid is 30.
7. Display all the details of the records whose employee name does not starts with “A”
8. Display the employees whose salary ranges from 15000 to 30000.
9. Calculate the total and average salary amount of the emp table.
10. Count the total records in the emp table
11. Determine the maximum and minimum salary and rename the column as
max_salary and min_salary.
12. Find the experience of the employee in months(Find the difference between the current
date and the DOJ) and display it for all the employees.
13. Find how many job titles are available in the employee table.
14. What is the difference between maximum and minimum salaries of employees in the
organization?
15. Add a default constraint to the salary and the set the default value as Rs.5000.
16. Find the total amount of salary spent by each department using GROUP BY clause.
17. Display the department that has total salary paid for its employees more than 25000
18. Display the ENAME as first name from the employees
EX 4: DATABASE QUERYING – SIMPLE QUERIES, NESTED QUERIES, SUB
QUERIES AND JOINS

1. Createthe following tables


emp (empno,empname ,job,deptno,salary )
Department(dname,dno,id)
Grades()rollno,gpa)
Student(sname,rollno,regno)
a) Display all employee names and salary whose salary is greater than
minimum salary of the company and job title starts with ‘M’
b) Issue a query to find all the employees who work in the same job as Arjun
c) Issue a query to display information about employees who earn more than
any employee in dept 1.
d) Display the employee details, departments that the departments are same
in both the emp and dept.
e) Display the Student name and grade by implementing a left outer join.
f) Display the Student name, register no, and result by implementing a right
outer join.
g) Display the Student name register no by implementing a full outer join.

2. Create the following tables and insert rows:


Product (maker, model, type)
PC(mode1, speed, ram, hd, rd, price)
Laptop(mode1, speed, ram, hd, screen, price)
Printer(mode1,color,type,price)
a) Find the average speed of PC’s.
b) Find the average speed of laptops costing over Rs. 2000.
c) Find the average price of PC made by a manufacturer A
d) Find for each different speed the average price of PC
e) Find for each manufacturer, the average screen size of its laptops.
f) Find the manufacturers that make at least three different models of PC
g) Find , for each speed of PC above 800, the average price

EX 5: VARIOUS JOINS IN SQL


1. Customer (Custid, Custname, Age, phno)
Loan (Loanid, Amount, Custid)

Execute the following using different joins


(a) Display the list of the customerids and total Loan amount taken
(b) Display the CustId and CustName who have taken less than 2 loans

2. SAILOR (SID, NAME, DOB, GENDER)


BOAT (BID, BTYPE, BNAME, COLOR)
BTYPE can take two values (D, S)
a) alter table boat add constraint c1 to check if btype values is s and d
b) Add constraint to check if shift is ‘an’ and ‘fn’.
c) Develop a SQL query to list the details of boats whose type is Super
Deluxe and Color is Red
d) List all sailors who took a trip in the month Jan 2023
e) List all sailors who took a trip on “red” color boat in the month of Jan

3.emp_details(emp_no, emp_name, DOB, address, doj, mobile_no,dept_no,


salary)
dept_details(dept_no, dept_name, location)
a) Select dept_no from dept_details and not in emp_details using both the
tables
b) Display the empnames of those who work in Chennai
c) List the names of all employees who earn more than 50000 in CSE Dept
d) List the Department name and number of employees in each department.
e) List the dept name and number of employees earning more than 30000.

EX 6: USER DEFINED FUNCTIONS AND STORED PROCEDURES


1. Write a PLSQL Program to print Hello Statement.
2. Write a PLSQL program to add 2 numbers.
3. Write a PL SQL program to swap 2 numbers.
4. Create a procedure Sal_Get to get the salary of an employee, passing an empid as an
input parameter ( Create employees table with columns like EmpID, Fname,Lname, Age,
Salary. Insert 5 rows in the table)
5. Create a procedure to get the Annual Income of the employee. Employee is passed as
an input parameter and Annual Income is retrieved as output parameter.
6. Create a function to get the salary of an employee when empid is passed as parameter.
Use the function in a query and return the result.
7. Create a function to display the FirstName and LastName as FirstName,LastName

EX 7 DCL AND TCL COMMANDS


1. Create an employees table with the following columns EmpID,
FirstName,LastName,Salary
2. In the employees table add few rows
3. Use savepoint after inserting the rows
4. Insert few more rows
5. Illustrate rollback command
6. Use commit statement
7. Illustrate grant and revoke DCL commands

EX.NO:8 TRIGGERS FOR INSERT, DELETE, AND UPDATE OPERATIONS

1. Create table sailor(sid,sname,age).Add trigger before insert to allow age greater than 18

CREATE OR REPLACE TRIGGER check_age_before_insert


BEFORE INSERT ON sailor_details
FOR EACH ROW
BEGIN
IF :NEW.age <= 18
THEN RAISE_APPLICATION_ERROR(-20001, 'Age must be greater than 18.');
END IF;
END; /

2. Create table stock(pid ,stock). Add trigger before update to maintain a stock above 5 on
update.
3. Write a Trigger to carry out the following action: on deleting any records from the emp
table ,the same values must be inserted into the log table.

Ex.No. 9 VIEW AND INDEX

1. Create a view containing the employee number and employee name from employee
table.
2. Create an index on employee name and then run the query. Use set timing on to check
for the elapsed time to execute the query.
3. Update the employee name of an employee and record the observations. Check if the
value is reflected in the original table.
EX 10 : SQL QUERIES
1. List of Students in a Particular Department
Write a query to find all students who belong to the "Computer Science" department.
2. List of Professors by Department
Write a query to list all professors and their respective departments.
3. Courses Taught by a Professor
Write a query to find all courses taught by Professor Sarah Lee.
4. Total Credits per Student
Write a query to calculate the total number of credits a student has taken, based on
their enrollments.
5. List of Students Enrolled in a Specific Course
Write a query to find all students who are enrolled in the "Data Structures" course.
6. Average Grade per Course
Write a query to calculate the average grade (A = 4, B = 3, C = 2, etc.) for each course.
7. Students with GPA Greater Than 3.5
Write a query to list all students whose GPA (based on their courses) is greater than
3.5.
8. Top 3 Best Performing Students
Write a query to find the top 3 students with the highest GPA (average grade across all
their courses).
9. Professor's Course Load
Write a query to find the number of courses taught by each professor.
10. List of Upcoming Events
Write a query to list all events that are happening after the current date.
11. Student Enrollment History
Write a query to display the courses each student has enrolled in, along with their
grades.
12. Total Number of Students per Department
Write a query to find the number of students in each department.
13. Students Who Have Failed a Course
Write a query to find all students who have received a grade of "C" or below in any
course.
14. Department-wise Average Credits per Course
Write a query to calculate the average number of credits for each department.
15. Courses Not Taught by Any Professor
Write a query to find all courses that are not assigned to any professor.
16. Find the Professors Who Have Taught More Than One Course
Write a query to list the professors who have taught more than one course.
17. List Events Happening in a Specific Location
Write a query to list all events that are happening in the "Main Hall."
18. Students and Their Professors
Write a query to list all students along with the professors teaching their enrolled
courses.
19. Courses with More Than 50 Students Enrolled
Write a query to find all courses that have more than 50 students enrolled.
20. Departments with the Most Courses
Write a query to find which department offers the most number of courses.

You might also like