0% found this document useful (0 votes)
12 views36 pages

BCS403 -DBMS Lab Manual_updated

The document is a lab manual for the Database Management System Laboratory course at CMR Institute of Technology, outlining course objectives, outcomes, and a series of experiments for students to complete. It includes specific SQL programming tasks, database design principles, and practical applications using both relational and NoSQL databases. The manual emphasizes hands-on learning through problem-based and active learning pedagogies.

Uploaded by

shm23csds
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)
12 views36 pages

BCS403 -DBMS Lab Manual_updated

The document is a lab manual for the Database Management System Laboratory course at CMR Institute of Technology, outlining course objectives, outcomes, and a series of experiments for students to complete. It includes specific SQL programming tasks, database design principles, and practical applications using both relational and NoSQL databases. The manual emphasizes hands-on learning through problem-based and active learning pedagogies.

Uploaded by

shm23csds
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/ 36

CMR Institute of Technology, Bengaluru

Department of Computer Science and Engineering


BCS403 - Database Management System Laboratory

Lab Manual
Database Management System Laboratory

Semester: IV

Course Code: BCS403

Teaching Hours/Week - 2

CIE Marks 50

SEE Marks 50

Credits 04

1 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Course Objectives (Defined by the university):

CLO 1. To Provide a strong foundation in database concepts, technology, and practice.


CLO 2. To Practice SQL programming through a variety of database problems.
CLO 3. To Understand the relational database design principles
CLO 4. To Demonstrate the use of concurrency and transactions in database.
CLO 5. To Design and build database applications for real world problems.
CLO 6. To become familiar with database storage structures and access techniques.

Note: A two-hour tutorial is suggested for each laboratory session.

Pedagogy: For the above experiments the following pedagogy can be considered. Problem-based
learning, Active learning, MOOC, Chalk & Talk
PART A – List of problems for which students should develop programs and execute in the
Laboratory.
Course outcomes (Course Skill Set):
At the end of the course, the student will be able to:
CO 1. Describe the basic elements of a relational database management system
CO 2. Design entity relationship for the given scenario.
CO 3. Apply various Structured Query Language (SQL) statements for database manipulation.
CO 4. Analyse various normalization forms for the given application.
CO 5. Develop database applications for the given real world problem
CO 6. Understand the concepts related to NoSQL databases.

2 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

List of Problems/Experiments

Experiments

List of problems for which students should develop the program and execute it in the laboratory

1 Create a table called Employee & execute the following.


Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
1. Create a user and grant all permissions to the user.
2. Insert the any three records in the employee table contains attributes
EMPNO,ENAME JOB, MANAGER_NO, SAL, COMMISSION and use rollback.
Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.

2 Create a table called Employee that contain attributes EMPNO, ENAME, JOB,
MGR,SAL & execute the following.
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job
4. Rename the column of Employ table using alter command.
5. Delete the employee whose Emp no is 105.

3 Queries using aggregate functions (COUNT, AVG, MIN, MAX,SUM),Group by,


Orderby.
Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.

4 Create a row level trigger for the customers table that would fire for INSERT or
UPDATE

3 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
or DELETE operations performed on the CUSTOMERS table. This trigger will
display the
salary difference between the old & new Salary.
CUSTOMERS(ID , NAME , AGE , ADDRESS, SALARY)

5 Create cursor for Employee table & extract the values from the table. Declare the
variables
,Open the cursor & extrct the values from the cursor. Close the cursor.
Employee(E_id, E_name, Age, Salary)

6 Write a PL/SQL block of code using parameterized Cursor, that will merge the
data Available in the newly created table N_RollCall with the data available in the table
O_RollCall. If the data in the first table already exist in the second table then that
data should be skipped.

7 Install an Open Source NoSQL Data base MangoDB & perform basic
CRUD(Create, Read, Update & Delete) operations.
Execute MangoDB basic Queries using CRUD operations.

4 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Experiment 1: Create a table called Employee & execute the following.


Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
1. Create a user and grant all permissions to the user.
2. Insert the any three records in the employee table contains attributes
EMPNO,ENAME JOB, MANAGER_NO, SAL, COMMISSION and use rollback.
Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.
Code:
1. CREATE USER 'sharma'@'localhost' IDENTIFIED BY 'tiger';

GRANT ALL PRIVILEGES ON *.* TO 'sharma'@'localhost';

Creation of Table:

create table employee(empno int, ename varchar(10),job varchar(10),manager_no int,sal int,


commission int);

2. insert into employee(empno,ename,job,manager_no,sal,commission) values


(1,"Ram","Developer",123,6000,20);
5 | Page Department of CSE
CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
3. insert into employee(empno,ename,job,manager_no,sal,commission) values
(2,"John","Tester",456,7000,10);

insert into employee(empno,ename,job,manager_no,sal,commission) values


(3,"Mahesh","Consultant",789,8000,15);

Select *from employee;

Working of Rollback;

Step 1: Start Transaction


Insert the values
Apply savepoint

Step 2: Insert again values

6 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Step 3: Apply Rollback

7 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
4. alter table employee add primary key(ename);

alter table employee modify column empno int not null;/alter table employee modify
empno int not null;

5. insert into employee values(2,"Gokul",null,421,9800,34);

8 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Experiment 2: Create a table called Employee that contain attributes EMPNO, ENAME, JOB,
MGR,SAL & alter table employee modify column empno int not null;execute the following.
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job
4. Rename the column of the Employee table using alter command.
5. Delete the employee whose Emp no is 105.

Code:
Creation of Table: create table Emp(empno int, name varchar(30),job varchar(30),mgr
int, sal int);

1. ALTER TABLE Emp ADD COLUMN COMMISSION DECIMAL(10, 2);

2. insert into Emp values (101, 'John Doe', 'Manager', 100, 5000, 1000.28),(102,
'Jane Smith', 'Developer', 101, 4000, 500.10),(103, 'Alice Johnson', 'Analyst', 102,
3500, 120.12),(104, 'Bob Brown', 'Assistant', 103, 3000, 200.05),(105, 'Emily
Davis', 'Clerk', 102, 2500, 300.07);

9 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

3. UPDATE Emp SET job = 'Senior Developer' WHERE empno = 102;

4. ALTER TABLE Emp change mgr supervisor_no int;

10 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

5. DELETE FROM Emp WHERE empno = 105;

11 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Experiment 3: Queries using aggregate functions (COUNT, AVG, MIN, MAX,SUM),Group by,
Orderby.
Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
Code:
1. CREATE TABLE Employee1( E_id INT,E_name VARCHAR(50),Age INT, Salary int);

INSERT INTO Employee1 VALUES (1, 'John Doe', 30, 50000),(2, 'Jane Smith', 25,
45000),(3, 'Alice Johnson', 35, 60000),(4, 'Bob Brown', 28, 48000),(5, 'Emily Davis', 32,
55000);

12 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

2. SELECT COUNT(E_name) AS TotalEmployees FROM Employee1;

3. SELECT MAX(Age) AS MaxAge FROM Employee1;

4. SELECT MIN(Age) AS MinAge FROM Employee1;

5. SELECT E_name, Salary FROM Employee1 ORDER BY Salary ASC;

13 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

6. INSERT INTO Employee1 VALUES (6, 'Robert Downy', 22, 55000);


SELECT Salary, COUNT(*) AS EmployeeCount FROM Employee1 GROUP BY
Salary;

14 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Experiment 4: Create a row level trigger for the customers table that would fire for INSERT or
UPDATE
or DELETE operations performed on the CUSTOMERS table. This trigger will
display the
salary difference between the old & new Salary.
CUSTOMERS(ID , NAME , AGE , ADDRESS, SALARY)
Code:
Creation of Table:
CREATE TABLE customers (ID INT,NAME VARCHAR(50),AGE INT,ADDRESS
VARCHAR(100),SALARY INT);

CREATE TABLE salary_log (customer_id INT,customer_name


VARCHAR(50),old_salary INT, new_salary INT, salary_dif INT);

Step to delete existing trigger:


DROP TRIGGER IF EXISTS salary_difference_trigger;

Step to view existing triggers:

15 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
SELECT TRIGGER_NAME, EVENT_OBJECT_TABLE, ACTION_STATEMENT,
ACTION_TIMING
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA = 'your_database_name';

Trigger for displaying the salary_difference as per the question:


DELIMITER //

CREATE TRIGGER salary_difference


AFTER INSERT ON customers
FOR EACH ROW
BEGIN
DECLARE old_salary INT DEFAULT 20000;
DECLARE new_salary INT;
DECLARE salary_diff INT;

SET new_salary = NEW.SALARY;


SET salary_diff = new_salary - old_salary;

INSERT INTO salary_log (customer_id, customer_name, new_salary,


old_salary,salary_diff)
VALUES (NEW.ID, NEW.NAME, new_salary, old_salary, salary_diff);
END//

DELIMITER ;

16 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

INSERT INTO customers VALUES (1,'Himesh', 25, 'Delhi', 40000);


INSERT INTO customers VALUES (2,'Ramesh', 30, 'Bangalore', 30000);
INSERT INTO customers VALUES (3,'Mahesh', 40, 'Uttarakhand', 50000);

select * from salary_log;

Experiment 5: Create cursor for Employee table & extract the values from the table. Declare the
variables. Open the cursor & extract the values from the cursor. Close the cursor.

17 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
Employee(E_id, E_name, Age, Salary)
Code:
Creation of Table:
create table Employee(E_id int,E_name varchar(30),Age int, Salary int);
Insertion:
INSERT INTO Employee VALUES (1, 'John Doe', 30, 50000),(2, 'Jane Smith', 25, 45000),(3, 'Alice
Johnson', 35, 60000),(4, 'Bob Brown', 28, 48000),(5, 'Emily Davis', 32, 55000);
Creation Cursor
DELIMITER //
CREATE PROCEDURE fetch_employee_data()
BEGIN
DECLARE emp_id INT;
DECLARE emp_name VARCHAR(100);
DECLARE emp_age INT;
DECLARE emp_salary INT;
DECLARE emp_cursor CURSOR FOR
SELECT E_id, E_name, Age, Salary FROM Employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN emp_cursor;
emp_loop: LOOP
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;
IF done THEN
LEAVE emp_loop;
END IF;
SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ',
emp_age, ', Salary: ', emp_salary);
END LOOP emp_loop;
CLOSE emp_cursor;
END //
DELIMITER ;

18 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

CALL fetch_employee_data();

19 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

20 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Experiment 6: Write a PL/SQL block of code using parameterized Cursor, that will merge the
data Available in the newly created table N_RollCall with the data available in the table
O_RollCall. If the data in the first table already exist in the second table then that
data should be skipped.

Code:
CREATE TABLE O_RollCall (id INT,name VARCHAR(100));
INSERT INTO O_RollCall (id, name) VALUES(1, 'John'),(2, 'Alice'),(3, 'Bob');
CREATE TABLE N_RollCall (id INT,name VARCHAR(100));

PL/SQL Code:

DELIMITER //

CREATE PROCEDURE MergeRollCallData()


BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE o_id INT;
DECLARE o_name VARCHAR(100);
DECLARE n_count INT;

DECLARE o_cursor CURSOR FOR


SELECT id, name FROM O_RollCall;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN o_cursor;

read_loop: LOOP
FETCH o_cursor INTO o_id, o_name;
IF done THEN
LEAVE read_loop;
END IF;

21 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
SELECT COUNT(*) INTO n_count FROM N_RollCall WHERE id = o_id;
IF n_count = 0 THEN
INSERT INTO N_RollCall (id, name) VALUES (o_id, o_name);
END IF;
END LOOP;

CLOSE o_cursor;
END //

DELIMITER ;

22 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

CALL MergeRollCallData();

23 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Experiment 7:.Install an Open Source NoSQL Data base MangoDB & perform basic
CRUD(Create, Read, Update & Delete) operations.
Execute MangoDB basic Queries using CRUD operations.

Code:
How to Connect Mongo database:

Mongosh

Basic Operations:

Creating Database:

use databasename;

24 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
Existing Database:

Show dbs;

Creating Collection: a collection is a grouping of MongoDB documents. It is the


equivalent of a table in a relational database.

db.createCollection(“cmrit”);

Create Operation:

For inserting one: db.collectionname.insertOne({name:”joy”,age:20});

For inserting many:


db.Students.insertMany([
{ name: "Mack", age: 29 },
{ name: "Alice", age: 25 },
{ name: "Bob", age: 35 }
]);

25 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Read Operation:

db.Students.find();

To find One element:


db.Students.findOne({age:30});

Update Operation:
db.Students.updateOne({name:'John'},{$set: {age:40}});

26 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

db.Students.updateMany(
{ name: "John" },
{ $set: { age: 35 } }
);

The above command will update the "age" field to 35 for all documents in the
"users" collection where the "name" field is "John"

db.Students.updateMany(

27 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
{ name: { $in: ["John", "Alice"] } },
{ $set: { age: 45 } }
);

Deleting Operation:
db.Students.deleteOne({ name: "Alice" });

To delete multiple elements:

To delete Collection:
db.cmrit.drop();

To delete database:

28 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Content Beyond Syllabus Program

Program 1
About the Program/Problem Statement

29 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
In this program we are creating order database with 3 entities as “Salesman”, “Customer”,
“Orders”. The objective of this program is to retrieve the records from the database related to the
customers with grade above particular city average, find and display name and number of all
salesman who had more than one customer, display all the salesman and indicate those who have
and don't have customer in their cities.

Consider the following schema for Order Database:


SALESMAN(Salesman_id, Name, City, Commission)
CUSTOMER(Customer_id, Cust_Name, City, Grade,
Salesman_id) ORDERS(Ord_No, Purchase_Amt,
Ord_Date, Customer_id, Salesman_id)

Keywords/Key Concepts

Group by clause, Having clause, Aggregate Functions (Count, Avg, Max), Nested Query (Single
and Multiple row Subquery concepts ), Union

Questions
1. Count the customers with grades above Bangalore’s average.

2. Find the name and numbers of all salesman who had more than one
customer.

3. List all the salesman and indicate those who have and don’t have
customers in their cities (UseUNION operation.)

Program 2
About the Program/Problem Statement

In this program we are creating order database with 3 entities as “Salesman”, “Customer”,
“Orders”. The objective of this program is to design a view that finds salesman who has the

30 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
customer with higher order of a day & to remove a record from a table to understand the effect
of on delete cascade method.

Consider the following schema for Order Database:


SALESMAN(Salesman_id, Name, City, Commission)
CUSTOMER(Customer_id, Cust_Name, City, Grade,
Salesman_id) ORDERS(Ord_No, Purchase_Amt,
Ord_Date, Customer_id, Salesman_id)

Keywords/Key Concepts

View, Delete operation

Questions

1. Create a view that finds the salesman who has the customer with the
highest order of a day.

2. Demonstrate the DELETE operation by removing salesman with id


1000. All his orders mustalso be deleted.

---------------------------------------------------------------------------------------------------------------------

Key points to remember while Table Creation:

ON DELETE CASCADE: SQL Server deletes the rows in the child table that is corresponding to the
row deleted from the parent table.

ON DELETE SET NULL: SQL Server sets the rows in the child table to NULL if the corresponding
rows in the parent table are deleted.

--for Customer table foreign key-> Salesman_id, need to mention on delete set null.

--for Orders table foreign key-> Salesman_id, and Customer_id need to mention on delete
cascade.

31 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

In the 5th query question, it is asked to delete the Salesman_id 1000 from Salesman table and also
to ensure that all the orders of Salesman_id 1000 is also deleted. So, we have to mention on delete
cascade for Orders table for both the foreign keys.

In the 5th query question it is not asked to delete anything from the customer table. So, just to
ensure that referential integrity constraint (foreign key reference) is preserved in customer table,
on delete set null is used.

Table Creation

• Create table SALESMAN (Salesman_id int primary key, Name varchar (40), City varchar
(40), Commission varchar (40));

• Create table CUSTOMER (Customer_id int primary key, Cust_Name varchar (40), City
varchar (40), Grade int, Salesman_id int, Salesman_id references
SALESMAN(Salesman_id) on delete set null);

• Create table ORDERS (Ord_No int primary key, Purchase_Amt float, Ord_Date date,
Customer_id int, Salesman_id int, Customer_id references CUSTOMER(Customer_id) on
delete cascade, Salesman_id references SALESMAN(Salesman_id) on delete cascade);

Key points to remember while Insertion:

Always while inserting the values in the tables, refer the query questions and check whether any
hint for values to be inserted is given or not.

1. The 1st query question is “Count the customers with grades above Bangalore’s average”.
So, to calculate the average grade, city value should be inserted as “Bangalore” for more
than one row in the customer table.
2. The 2nd query question is “Find the name and numbers of all salesman who had more than
one customer”. So, same Salesman_id should be inserted for more than one row in

32 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
minimum in the customer table and the corresponding customer_ids for those rows
should be different.
Tables with inserted values:

select * from salesman;

select * from customer;

select * from orders;

33 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

Program 1 Solution

1. Count the customers with grades above Bangalore’s average.


SELE
CT
COUN
T(*)
FROM
CUST
OMER
WHERE GRADE > (SELECT AVG(GRADE)
FROM CUSTOMER
WHERE CITY='BANGALORE');

2. Find the name and numbers of all salesman who had more than one
customer.

Method 1
SELECT
SALESMAN_ID,
NAMEFROM
SALESMAN
WHERE SALESMAN_ID IN (SELECT SALESMAN_ID
FROM
CUSTOMER
C GROUP
BY
SALESMAN
_ID
HAVING
COUNT(*)>1
);

Method 2

34 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory
SELECT
S.SALESMAN_ID,
NAME FROM
SALESMAN S,
CUSTOMER C
WHERE
S.SALESMAN_ID=C.SALESM
AN_ID
GROUP BY S.SALESMAN_ID,
NAME HAVING COUNT(*)>1;

3. List all the salesman and indicate those who have and don’t have
customers in their cities (UseUNION operation.)

(SELECT DISTINCT S.SALESMAN_ID, S.NAME,


C.CUST_NAME
FROM CUSTOMER C, SALESMAN S
WHERE C.SALESMAN_ID=S.SALESMAN_ID AND C.CITY=S.CITY)
UNION
(SELECT DISTINCT S.SALESMAN_ID, S.NAME,
C.CUST_NAME
FROM CUSTOMER C, SALESMAN S
WHERE C.SALESMAN_ID=S.SALESMAN_ID AND C.CITY != S.CITY)
UNION
(SELECT DISTINCT S.SALESMAN_ID, S.NAME, “NO
CUSTOMER”
FROM SALESMAN S
WHERE SALESMAN_ID NOT IN (SELECT SALESMAN_ID
FROM CUSTOMER));

Program 2 Solution

1. Create a view that finds the salesman who has the customer with the
highest order of a day.

35 | Page Department of CSE


CMR Institute of Technology, Bengaluru
Department of Computer Science and Engineering
BCS403 - Database Management System Laboratory

CREATE VIEW HIGH_ORDER


AS (SELECT SALESMAN_ID,
ORD_DATE
FROM ORDERS
WHERE (ORD_DATE, PURCHASE_AMT) IN (SELECT ORD_DATE,
MAX(PURCHASE_AMT)
FROM ORDERS
GROUP BY ORD_DATE));

To retrieve the records from created View→ (Result of Ques.4)

SELECT * FROM HIGH_ORDER;

2. Demonstrate the DELETE operation by removing salesman


with id 1000. All his orders mustalso be deleted.

DELETE FROM SALESMAN WHERE SALESMAN_ID=1000;

Check effect of delete operation on the tables→ (Result of Ques.5)

SELECT * FROM
SALESMAN;
SELECT * FROM
ORDERS;
SELECT * FROM
CUSTOMER;

36 | Page Department of CSE

You might also like