HU21CSEN0100752 DBMS LAB RECORD-compressed
HU21CSEN0100752 DBMS LAB RECORD-compressed
Deemed to be University
(Estd. u/s of UGC Act, 1950)
Hyderabad Campus
i
GITAM
Deemed to be University
(Estd. u/s of UGC Act, 1950)
Hyderabad Campus
CERTIFICATE
Faculty I/c.
Date: Head of the Department
ii
INDEX
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:
ExecuIon:
Command:
ExecuIon:
ExecuIon:
Command:
ExecuIon:
v
Command:
ExecuIon:
Command:
ExecuIon:
vi
Command:
ExecuIon:
Command:
ExecuIon:
vii
Command:
ExecuIon:
Command:
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);
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
1. to retrieve all the informaIon from the employee table in the descending order of employee_names
19
4. to display the maximum salary, minimum salary, average salary of all employees
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
10. delete two rows from employee table and dont commit
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.
25
DBMS LAB ASSIGNMENT – 6
M SREE NEEHARIKA REEDY
Id:hu21csen0100752
1. create a user with your 'rollno' and use password as gitam
query:
2. grant all privileges to 'rollno' and connect to 'rollno' with appropriate password
query:
26
query :
SHOW TABLES;
4. create a table login with adributes id, password with appropriate datatypes and constraints
query:
KEY (id),
query:
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
33
6. Full outer join
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;
VALUES
BEGIN
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;
ELSE
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 //
FROM employee
ELSE
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 //
BEGIN
FROM employee
ELSE
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 //
BEGIN
42
SELECT *
FROM employee
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
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)
);
-- Use the SELECT statement to retrieve the employee salary for the specified employee ID
SELECT employee_salary
FROM employee
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)
);
DELIMITER //
BEGIN
FROM employee;
OPEN cur;
FETCH_LOOP: LOOP
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
OPEN emp_cursor;
FETCH_LOOP: LOOP
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;
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 //
BEGIN
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