0% found this document useful (0 votes)
70 views53 pages

HU21CSEN0100752 DBMS LAB RECORD-compressed

The query should be: CREATE USER 'hu21csen0100752' IDENTIFIED BY 'gitam'; 2. grant select privilege on employee table to the user created in step 1 query: GRANT SELECT ON employee TO 'hu21csen0100752'; 3. connect as the user created in step 1 and try to select from employee table 4. revoke select privilege on employee table from the user created in step 1 query: REVOKE SELECT ON employee FROM 'hu21csen0100752'; 5. grant insert, update, delete privileges on employee table to the user created in step 1 query: GRANT INSERT,
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)
70 views53 pages

HU21CSEN0100752 DBMS LAB RECORD-compressed

The query should be: CREATE USER 'hu21csen0100752' IDENTIFIED BY 'gitam'; 2. grant select privilege on employee table to the user created in step 1 query: GRANT SELECT ON employee TO 'hu21csen0100752'; 3. connect as the user created in step 1 and try to select from employee table 4. revoke select privilege on employee table from the user created in step 1 query: REVOKE SELECT ON employee FROM 'hu21csen0100752'; 5. grant insert, update, delete privileges on employee table to the user created in step 1 query: GRANT INSERT,
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/ 53

GITAM

Deemed to be University
(Estd. u/s of UGC Act, 1950)
Hyderabad Campus

Laboratory Record Book

Name of the Student: M SREE NEEHARIKA REEDY


Pin No: HU21CSEN0100752
Department: Computer Science Engineering
Laboratory: DBMS LABORATORY Section: CSE (H) SET-3

i
GITAM
Deemed to be University
(Estd. u/s of UGC Act, 1950)
Hyderabad Campus

CERTIFICATE

Certified that this is the bonafide record of practical work done by


Mr./Ms. M SREE NEEHARIKA REDDY with Reg.
HU21CSEN0100752 of B. Tech Computer Science Engineering
branch in DATABASE MANAGEMENT SYSTEM Laboratory of
Department of during the academic year 2023-2024.

Faculty I/c.
Date: Head of the Department

ii
INDEX

S.No Topic Date Page No Marks Remarks


1 SQL Commands 25-07-2023 1-4

2 ER DIAGRAM 01-08-2023 5-13


3 SQL Queries Using Alter 08-08-2023 14-17
Command
4 Views SQL Queries 22-08-2023 18-21

5 View, save point, Commit 19-09-2023 22-26


&Rollback (WEEK-5)
6 Privileges (WEEK-6) 19-09-2023 27-29
7 Joins And Sets (WEEK-7) 20-09-2023 30-34
8 PL/SQL (WEEK – 8) 26-09-2023 35- 38
9 TRIGGERS AND CURSORS (WEEK-9) 26-09-2023 39 - 42

iii
DBMS LAB ASSIGNMENT -1
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
write the SQL queries for the following
1.Create an employee table with (id, name, salary, dob, deptno) a8ributes. consider id as the primary key

Command:

CREATE TABLE employee (


empid int PRIMARY KEY,
empname varchar(10),
salary int,
dob varchar(15),
deptno int
);

ExecuIon:

2. insert 5 rows of data into employee table

Command:

INSERT INTO employee VALUES (111,'Neeharika', 100000, '15-June-2003',10);


INSERT INTO employee VALUES (112,'Renuka',30000,'19-July-2002',10);
INSERT INTO employee VALUES (113,'Madhu', 55000, '16-June-2000',20);
INSERT INTO employee VALUES (114,'Sunshine', 47000, '17-June-2001',30);
INSERT INTO employee VALUES (115,'Revathi', null, '18-June-2000',30);

ExecuIon:

3. retrieve all the rows from the employee table


iv
Command:

select * from employee;

ExecuIon:

4. update the employee name for empid 111 to 'Blake'

Command:

UPDATE employee SET empname='Blake' where empid='111';

ExecuIon:

5. delete the employees having null values for salary

v
Command:

DELETE FROM employee WHERE salary is null;

ExecuIon:

6. retrieve the employees having name of 5 characters length

Command:

SELECT * FROM employee WHERE length(empname)=5;

ExecuIon:

7. retrieve the employee names starGng with S and ending with e

vi
Command:

SELECT * FROM employee WHERE empname like 'S%e';

ExecuIon:

8. retrieve employee data from deptnos 10 and 20;

Command:

SELECT * FROM employee WHERE deptno='10' or deptno='20';

ExecuIon:

9. retrieve the employees whose salary is not null

vii
Command:

SELECT * FROM employee WHERE salary is not null;

ExecuIon:

10. delete the employees having deptno 20;

Command:

DELETE FROM employee WHERE deptno='20';

ExecuIon:

viii
DBMS LAB ASSIGNMENT -2
M SREE NEEHARIKA REEDY
Id:hu21csen0100752

ix
s)

x
xi
xii
xiii
DBMS LAB ASSIGNMENT - 3
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
1.Create a rela*on with name student with following fields
sid, name, age, address, courseid, coursefee with suitable domains, sid-primary key
Ans:
CREATE TABLE Student (sid INTEGER PRIMARY KEY,name VARCHAR(20),age INTEGER,address
VARCHAR(20),courseid INTEGER,coursefee INTEGER);

2. alter the table structure to add the column dob


Ans:
ALTER TABLE Student ADD dob varchar(15);

3. alter the table structure to drop the column address


Ans:
ALTER TABLE Student DROP COLUMN address;

14
4. alter the table structure to add not null constraint on dob, default constraint on coursefee with
default value of 5000,
Ans:
ALTER TABLE Student MODIFY COLUMN dob varchar(15) NOT NULL;
ALTER TABLE Student ALTER coursefee SET DEFAULT '5000';

5. alter the table structure to add user defined constraint on coursefee to allow the values between 3000
and 15000
Ans:
ALTER TABLE Student ADD CONSTRAINT check_coursefee CHECK (coursefee >= 3000 and
coursefee<=15000);

15
6. create the table course with the aEributes courseid, name, instructorname with suitable domains and
primary key.
Ans:
CREATE TABLE Course (courseid INTEGER PRIMARY KEY, name varchar(15), instructorname varchar(25));

7. alter the student table structure to add foreign key referencing to course table
Ans:
ALTER TABLE Student ADD FOREIGN KEY (courseid) REFERENCES Course(courseid);

16
8. alter the table student structure to disable primary key
Ans:
ALTER TABLE Student DROP PRIMARY KEY;

9. alter the student table structure to modify the datatype of coursefee to take real values.
Ans:
ALTER TABLE Student MODIFY COLUMN coursefee REAL;

17
10. alter the table structure of course to rename the column instructorname to facultyname.
Ans:
ALTER TABLE Course RENAME COLUMN instructorname to facultyname;

18
DBMS LAB ASSIGNMENT - 4

M SREE NEEHARIKA REEDY


Id:hu21csen0100752
Write SQL queries for the following

1. to retrieve all the informaIon from the employee table in the descending order of employee_names

2. to retrieve all the employees informaIon in the ascending order of salaries

3. to retrieve the department wise sum of salaries

19
4. to display the maximum salary, minimum salary, average salary of all employees

5. to display the number of employees in each department

6. to display the department wise average salaries

7. to display the difference of average salary and his own earnings for each employee, give the alias name as sal_diff

20
8. to display the department wise minimum salaries for department numbers 10, 20.

9. to display the number of months between independence day and your date of birth

10. to display the experience of each employee calculated as difference of date of joining and current date, give
alias name as employee_experience.

21
DBMS LAB ASSIGNMENT – 5
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
1. create a view with name max_sal_view from employee table as departmentwise maximum
salaries

2. create an updatable view salrange_view from employee table having salaries between 10000 and 50000

22
3. insert a row into salrange_view and check that row inserted in employee table or not.

4. create a view salrange_view1 from employee table having salaries between 10000 and
50000 with check opIon

5. insert a row into salrange_view1 with salaries 40000 and 60000. what did you observe.
6. delete max_sal_view

23
7. create a savepoint with name SPA

8. insert four rows into employee table and commit the changes

9. create a savepoint with name SPB

10. delete two rows from employee table and dont commit

11. rollback to SPB, what did you observe

24
ObservaIon:

Once we run the rollback to SPB statement , the table got reverted to the status before SPB savepoint is created i.e,
the two deleted rows from the Employees table got reverted and can be seen in the Employees table now.

12. write short notes on commit, rollback and savepoint

COMMIT − to save the changes. ROLLBACK − to roll back the


changes.SAVEPOINT − creates points within the groups of transactions in which to
ROLLBACK.

25
DBMS LAB ASSIGNMENT – 6
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
1. create a user with your 'rollno' and use password as gitam

query:

CREATE USER 'neeharika0752'@'localhost' IDENTIFIED BY 'gitam';

2. grant all privileges to 'rollno' and connect to 'rollno' with appropriate password

query:

GRANT ALL PRIVILEGES ON *.* TO 'neeharika0752'@'localhost'; FLUSH

PRIVILEGES; mysql -u neeharika0752 -p

3. retrieve all tables that exists in 'rollno'

26
query :

SHOW TABLES;
4. create a table login with adributes id, password with appropriate datatypes and constraints

query:

CREATE TABLE login ( id varchar(15) not null, PRIMARY

KEY (id),

password VARCHAR(15) NOT NULL


);
5. insert 5 rows into login
27
query:

insert into login values


(‘neeharika0752’,’gitam’),(‘ninad’,’amritha’),(‘sarath’,sasthra),(‘renuka’,jntuh’),(‘ram’,;iiith’);

6. display login data

query:

select * from login;


7. connect back to admin user and revoke select privilege on login table from rollno

query:
28
Revoke select on*.* from ‘neeharika0752’@’localhost’;

FLUSH PRIVILEGES;

8. now retrieve the data from login table. what did you observe?

Observaion:

Since the permission for SELECT command is revoked for user ‘Neeharika0752’ is revoked by the admin, the
user won’t be able to perform SELECT operaions.

29
DBMS LAB ASSIGNMENT – 7
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
Demonstrate the following joins using SQL queries

1. Equi join

30
31
2. Natural join

3. Non-equi join

32
4. Left outer join

5. Right outer join

33
6. Full outer join

Demonstrate the following set operators using SQL queries

1. Union

2. Union All

3. Intersect

4. Minus / Except

34
35
DBMS LAB ASSIGNMENT – 8
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
1. To print the employee details from employee table for a specific employee no given at run
time using local variables

36
CREATE DATABASE local_variables;

USE DATABASE local_variables;

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES

(1, 'neeharika', 50000.0, 'HR'),

(2, 'varnika', 60000.0, 'IT');

CREATE PROCEDURE PrintEmployeeDetails(IN employee_number INT)

BEGIN

DECLARE v_employee_id INT;

DECLARE v_employee_name VARCHAR(255);

DECLARE v_employee_salary DECIMAL(10, 2);

DECLARE v_employee_department VARCHAR(255);

SELECT
employee_id, employee_name,
employee_salary, employee_department
INTO v_employee_id,
v_employee_name,
v_employee_salary,
v_employee_department
FROM
employees
WHERE

employee_id = employee_number;

IF v_employee_id IS NOT NULL THEN

SELECT 'Employee ID: ', v_employee_id;

SELECT 'Employee Name: ', v_employee_name;

SELECT 'Employee Salary: ', v_employee_salary;

SELECT 'Employee Department: ', v_employee_department;

ELSE

SELECT 'Employee not found';

END IF;

END //

DELIMITER ;

37
2. To print the employee details from employee table for a specific employee no given at run time
using record type variable

DELIMITER //

CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)


38
BEGIN

DECLARE emp_name VARCHAR(255);

DECLARE emp_salary DECIMAL(10, 2);

DECLARE emp_department VARCHAR(255);

SELECT employee_name, employee_salary, employee_department

INTO emp_name, emp_salary, emp_department

FROM employee

WHERE employee_id = emp_id;

IF emp_name IS NOT NULL THEN

SELECT 'Employee ID: ', emp_id;

SELECT 'Employee Name: ', emp_name;

SELECT 'Employee Salary: ', emp_salary;

SELECT 'Employee Department: ', emp_department;

ELSE

SELECT 'Employee not found.';

END IF;

END;

//

DELIMITER ;

CALL GetEmployeeDetails(2);

3. To display the employee details from employee table for a specific employee no given at run
time using rowtype record variable

39
DELIMITER //

CREATE PROCEDURE GetEmployeeDetails(IN employee_id_param INT)

BEGIN

DECLARE v_employee_id INT;

DECLARE v_employee_name VARCHAR(255);


40
DECLARE v_employee_salary DECIMAL(10, 2);

DECLARE v_employee_department VARCHAR(255);

SELECT employee_id, employee_name, employee_salary, employee_department

INTO v_employee_id, v_employee_name, v_employee_salary, v_employee_department

FROM employee

WHERE employee_id = employee_id_param;

IF FOUND_ROWS() > 0 THEN

SELECT 'Employee ID:', v_employee_id;

SELECT 'Employee Name:', v_employee_name;

SELECT 'Employee Salary:', v_employee_salary;

SELECT 'Employee Department:', v_employee_department;

ELSE

SELECT 'Employee not found';

END IF;

END //

DELIMITER ;

CALL GetEmployeeDetails(1);

4. To display the employee details from employee table for a specific employee no given at run time
using procedures

41
DELIMITER //

-- Create a procedure to fetch employee details by employee_id

CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)

BEGIN

42
SELECT *

FROM employee

WHERE employee_id = emp_id;

END //

DELIMITER ;

CALL GetEmployeeDetails(4);

5. To display the employee salary from employee table for a specific employee no given at run
time using functions

CREATE DATABASE FuncIontype_variable;

USE FuncIontype_variable;
CREATE TABLE IF NOT EXISTS employee ( employee_id INT
PRIMARY KEY, employee_name VARCHAR(255),
employee_salary DECIMAL(10, 2), employee_department
VARCHAR(255)
);

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (1, 'neeharika', 50000.0, 'HR');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)


43
VALUES (2, 'varnika', 60000.0, 'IT');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (3, 'renuka', 55000.0, 'Finance');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (4, 'poorna', 62000.0, 'IT');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (5, 'krishna', 53000.0, 'MarkeIng');

-- Declare a variable to store the employee ID provided at runIme

SET @employee_id_to_lookup = 3; -- Change this value to the desired employee ID

-- Use the SELECT statement to retrieve the employee salary for the specified employee ID

SELECT employee_salary

FROM employee

WHERE employee_id = @employee_id_to_lookup;

44
DBMS LAB ASSIGNMENT – 9
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
1.To display all the employee details from employee table using implicit cursors

45
CREATE DATABASE implictsql_variable; USE
implictsql_variable;
CREATE TABLE IF NOT EXISTS employee ( employee_id INT PRIMARY KEY, employee_name VARCHAR(255),
employee_salary DECIMAL(10, 2), employee_department VARCHAR(255)

);

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (1, 'neeharika', 50000.0, 'HR');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (2, 'varnika', 60000.0, 'IT');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (3, 'renuka', 55000.0, 'Finance');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (4, 'poorna', 62000.0, 'IT');

INSERT INTO employee (employee_id, employee_name, employee_salary, employee_department)

VALUES (5, 'krishna', 53000.0, 'MarkeIng');

DELIMITER //

CREATE PROCEDURE FetchEmployeeDetails()

BEGIN

DECLARE done INT DEFAULT 0;

DECLARE emp_id INT;

DECLARE emp_name VARCHAR(255);

DECLARE emp_salary DECIMAL(10, 2);

DECLARE emp_department VARCHAR(255);

DECLARE cur CURSOR FOR

SELECT employee_id, employee_name, employee_salary, employee_department

FROM employee;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur;

FETCH_LOOP: LOOP

FETCH cur INTO emp_id, emp_name, emp_salary, emp_department;

IF done = 1 THEN
46
LEAVE FETCH_LOOP;

END IF;

-- Display employee details (you can modify this part as needed) SELECT emp_id, emp_name,
emp_salary, emp_department;

END LOOP;

CLOSE cur;

END;

//

DELIMITER ;

CALL FetchEmployeeDetails();

2. To display all the employee details from employee table using explicit cursors

47
CREATE PROCEDURE DisplayEmployeeDetails()

BEGIN

DECLARE done INT DEFAULT 0;

DECLARE emp_id INT;

DECLARE emp_name VARCHAR(255);

DECLARE emp_salary DECIMAL(10, 2);

DECLARE emp_department VARCHAR(255);

DECLARE emp_cursor CURSOR FOR

SELECT employee_id, employee_name, employee_salary, employee_department FROM employee;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN emp_cursor;

FETCH_LOOP: LOOP

FETCH emp_cursor INTO emp_id, emp_name, emp_salary, emp_department;


IF done = 1 THEN

LEAVE FETCH_LOOP;

END IF;

48
SELECT emp_id AS 'Employee ID', emp_name AS 'Employee Name', emp_salary AS
'Employee Salary', emp_department AS 'Employee Department';

END LOOP;

CLOSE emp_cursor; END //

DELIMITER ;

CALL DisplayEmployeeDetails();

3.To display all the student details from student table using loops(any loop)

49
50
4. Write an update trigger to display the salary change for an employee salary upda<on

DELIMITER //

CREATE TRIGGER salary_change_trigger


AFTER UPDATE ON employee

FOR EACH ROW

BEGIN

IF NEW.employee_salary <> OLD.employee_salary THEN


INSERT INTO salary_change_log (employee_id, employee_name, old_salary, new_salary, change_date)

VALUES (OLD.employee_id, OLD.employee_name, OLD.employee_salary,

NEW.employee_salary, NOW());

END IF;

END;
//

DELIMITER ;
5. Create a log table and log every change of employee table, <ming, opera<on, user name using triggers.

51
52
--THE END--

53

You might also like