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

CS-502-DBMS Lab Manual

This document outlines 7 programs related to database management using SQL. The programs cover topics like deleting duplicate rows, displaying alternate rows, updating and deleting rows, and using PL/SQL for CRUD operations. The document provides the SQL code to implement each program on sample tables along with expected output.

Uploaded by

ac8995802
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)
13 views

CS-502-DBMS Lab Manual

This document outlines 7 programs related to database management using SQL. The programs cover topics like deleting duplicate rows, displaying alternate rows, updating and deleting rows, and using PL/SQL for CRUD operations. The document provides the SQL code to implement each program on sample tables along with expected output.

Uploaded by

ac8995802
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/ 20

Laboratory Manual

DATABASE MANAGEMENT SYSTEM

CS-503

2021-2022

Aditya College Of Engineering And Technology

Department of Computer Science & Engineering


Practical File
[B.Tech.]

Subject :- ………………………………………………………..……………

Subject code :- ………………………………………………………..……………

Student Name :- ………………………………………………………..……………

Enrollment No. :- ………………………………………………………..……………

Semester :- ………………………………………………………..……………

Faculty Signature Internal Assessment HOD

|
INDEX
S.No. Experiment Page No. Signature

|
Program => 1
Delete duplicate row from the table.

CREATE TABLE contacts (


id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(25) NOT NULL,
email VARCHAR(210) NOT NULL,
age VARCHAR(22) NOT NULL
);
Insert the values:-
INSERT INTO contacts (first_name,last_name,email,age)
VALUES ('Kavin','Peterson','[email protected]','21'),
('Nick','Jonas','[email protected]','18'),
('Peter','Heaven','[email protected]','23'),
('Michal','Jackson','[email protected]','22'),
('Sean','Bean','[email protected]','23'),
('Tom ','Baker','[email protected]','20'),
('Ben','Barnes','[email protected]','17'),
('Mischa ','Barton','[email protected]','18'),
('Sean','Bean','[email protected]','16'),
('Eliza','Bennett','[email protected]','25'),
('Michal','Krane','[email protected]','25'),
('Peter','Heaven','[email protected]','20'),
('Brian','Blessed','[email protected]','20');
('Kavin','Peterson','[email protected]','30'),

SELECT * FROM contacts ORDER BY email;


OUTPUT=>
 SELECT email, COUNT(email)
FROM contacts GROUP BY email
HAVING COUNT (email) > 1;

Delete duplicate rows with the DELETE JOIN statement

DELETE t1 FROM contacts t1


INNERJOIN contacts t2
WHERE
t1.id < t2.id AND
t1.email = t2.email;

 SELECT * FROM contacts;


OUTPUT=>
Program => 2
Display the alternate row from table.

CREATE TABLE IF NOT EXISTS `empdata` ( `id` int(11) NOT NULL


AUTO_INCREMENT, `name` char(25) NOT NULL, `email` varchar(100) NOT NULL,
`phone` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM
AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

 INSERT INTO `empdata` (`id`, `name`, `email`, `phone`) VALUES (1, 'Anjali',
'[email protected]', 878433948), (2, 'Priska', '[email protected]', 493905490), (3,
'Abhi', '[email protected]', 403022139), (4, 'Joya', '[email protected]', 342345329),
(5, 'Ammy', '[email protected]', 239848342), (6, 'Lussi', '[email protected]',
490290331);

MOD() method
MySQL MOD() method returns the remainder of a number divided by another number. So for
getting alternate rows, we can divide the ID with 2 and displays only those having remainder 1.
SELECT * FROM empdata GROUP BY id having mod(id,2)=1;
Output of the above statement
+----+--------+--------------------+-----------+
| id | name | email | phone |
+----+--------+--------------------+-----------+
| 1 | Anjali | [email protected] | 878433948 |
| 3 | Abhi | [email protected] | 403022139 |
| 5 | Ammy | [email protected] | 239848342 |

+----+--------+--------------------+-----------+
The above statement returns only ODD rows. If you want to get even rows, write the statement
as-

SELECT * FROM empdata GROUP BY id having mod(id,2)=0;


Output of the above statement
+----+--------+--------------------+-----------+
| id | name | email | phone |
+----+--------+--------------------+-----------+
| 2 | Priska | [email protected] | 493905490 |
| 4 | Joya | [email protected] | 342345329 |
| 6 | Lussi | [email protected] | 490290331 |
Program => 3
Delete alternate row from table.

 CREATE TABLE IF NOT EXISTS `empdata` ( `id` int(11) NOT NULL


AUTO_INCREMENT, `name` char(25) NOT NULL, `email` varchar(100) NOT NULL,
`phone` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM
AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

 INSERT INTO `empdata` (`id`, `name`, `email`, `phone`) VALUES (1, 'Anjali',
'[email protected]', 878433948), (2, 'Priska', '[email protected]', 493905490), (3,
'Abhi', '[email protected]', 403022139), (4, 'Joya', '[email protected]', 342345329),
(5, 'Ammy', '[email protected]', 239848342), (6, 'Lussi', '[email protected]',
490290331);

MOD() method
MySQL MOD() method returns the remainder of a number divided by another number. So for
getting alternate rows, we can divide the ID with 2 and fetch only those having remainder 1 and
delete them.
DELETE FROM empdata
WHERE id IN(
SELECT *
FROM (
SELECT id FROM empdata GROUP BY id having mod(id,2)=1
)temp
);

SELECT * FROM `empdata`;


Output of the above statement
+----+--------+--------------------+-----------+
| id | name | email | phone |
+----+--------+--------------------+-----------+
| 4 | Joya | [email protected] | 342345329 |
| 2 | Priska | [email protected] | 493905490 |
| 6 | Lussi | [email protected] | 490290331 |
The above statement deletes only ODD rows. If you want to delete even rows, write the
statement as-

DELETE FROM empdata


WHERE id IN(
SELECT *
FROM (
SELECT id FROM empdata GROUP BY id having mod(id,2)=0
)temp
);

SELECT * FROM `empdata`;


Output of the above statement
+----+--------+--------------------+-----------+
| id | name | email | phone |
+----+--------+--------------------+-----------+
| 5 | Ammy | [email protected] | 239848342 |
| 3 | Abhi | [email protected] | 403022139 |
| 1 | Anjali | [email protected] | 878433948 |

Program => 4
Update multiple rows in using single update statement.

 CREATE TABLE BANDS(BAND_NAME VARCHAR(20),


PERFORMING_COST INT,NUMBER_OF_MEMBERS INT);

 INSERT INTO BANDS VALUES('INDIAN OCEAN',10000,5);


 INSERT INTO BANDS VALUES('BTS',20000,6);
 INSERT INTO BANDS VALUES('METALLICA',30000,10);
 INSERT INTO BANDS VALUES('BEATLES',40000,4);
 INSERT INTO BANDS VALUES('EAGLES',50000,4);

SELECT * FROM BANDS;

Update all records of the table BANDS satisfying only a single condition. The condition here
is that the value of the column NUMBER_OF_MEMBERS should be less than 5. If the
condition is satisfied then the value of the column PERFORMING_COST doubles itself. Use
the keyword UPDATE and WHERE to achieve this.

UPDATE BANDS SET PERFORMING_COST =


2*PERFORMING_COST WHERE NUMBER_OF_MEMBERS<=5;
 SELECT * FROM BANDS;

Program => 5
Show all employees who were hired the first half of the month

create Employee Table with Date format

 CREATE DATABASE WORKERS ;


 USE WORKERS;

Create Employee table

 CREATE TABLE EMPLOYEE(EID INT ,ENAME VARCHAR(20),DATE_OF_HIRED


DATE);

Insert records into table:

 INSERT INTO EMPLOYEE VALUES(101,’REETA’,’2000-07-10’);


 INSERT INTO EMPLOYEE VALUES(102,’UPASHANA’,’2000-07-12’);
 INSERT INTO EMPLOYEE VALUES(103,’ANSHUL’,’2000-07-07’);
 INSERT INTO EMPLOYEE VALUES(104,’SHIVANI’,’2000-07-15’);
 INSERT INTO EMPLOYEE VALUES(105,’BRAJENDRA’,’2000-07-20’);


Search the Records

 SELECT *FROM Emprec WHERE TO_NUMBER(TO_CHAR(Date_of_hired, 'DD'))


<= 15 ;

Program => 6
Display the ename, which is start with j, k, l or m.

CREATE TABLE contacts (


id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(25) NOT NULL,
email VARCHAR(210) NOT NULL,
age VARCHAR(22) NOT NULL
);
Insert the values:-
INSERT INTO contacts (first_name,last_name,email,age)
VALUES ('Kavin','Peterson','[email protected]','21'),
('Nick','Jonas','[email protected]','18'),
('Peter','Heaven','[email protected]','23'),
('Michal','Jackson','[email protected]','22'),
('Sean','Bean','[email protected]','23'),
('Tom ','Baker','[email protected]','20'),
('Ben','Barnes','[email protected]','17'),
('Mischa ','Barton','[email protected]','18'),
('Sean','Bean','[email protected]','16'),
('Eliza','Bennett','[email protected]','25'),
('Michal','Krane','[email protected]','25'),
('Peter','Heaven','[email protected]','20'),
('Brian','Blessed','[email protected]','20');
('Kavin','Peterson','[email protected]','30'),

SELECT * FROM contacts ORDER BY email;


OUTPUT=>
SELECT first_name "Name",
LENGTH(first_name) "Length"
FROM contacts
WHERE first_name LIKE 'J%'
OR first_name LIKE 'K%'
OR first_name LIKE 'L%'
OR first_name LIKE 'M%'
ORDER BY first_name ;
Program => 7
Write a pl/sql for select, insert, update and delete statements.

DECLARE
l_emp_name VARCHAR2(250);
l_emp_no NUMBER;
l_salary NUMBER;
l_manager VARCHAR2(250);
BEGIN
INSERT INTO emp(emp_name,emp_no,salary,manager)
VALUES(‘BBB’,1000,25000,’AAA’);
INSERT INTO emp(emp_name,emp_no,salary,manager)
VALUES('XXX',1001,10000,’BBB);
INSERT INTO emp(emp_name,emp_no,salary,managed
VALUES(‘YYY',1002,10000,'BBB');
INSERT INTO emp(emp_name,emp_no,salary,manager)
VALUES(‘ZZZ',1003,7500,'BBB'):
COMMIT;
Dbms_output.put_line(‘Values Inserted');
UPDATE EMP
SET salary=15000
WHERE emp_name='XXX';
COMMIT;
Dbms_output.put_line(‘Values Updated');
DELETE emp WHERE emp_name='ZZZ';
COMMIT:
Dbms_output.put_line('Values Deleted );
SELECT emp_name,emp_no,salary,manager INTO l_emp_name,l_emp_no,l_salary,l_manager
FROM emp WHERE emp_name='XXX';

Dbms_output.put line(‘Employee Detail’);


Dbms_output.put_line(‘Employee Name:‘||l_emp_name);
Dbms_output.put_line(‘Employee Number:‘||l_emp_no);
Dbms_output.put_line(‘Employee Salary:‘||l_salary);
Dbms_output.put line(‘Emplovee Manager Name:‘||l_manager):
END;
/
OUTPUT

Values Inserted
Values Updated
Values Deleted
Employee Detail
Employee Name:XXX
Employee Number:1001
Employee Salary:15000
Employee Manager Name:BBB
Program => 8
Display name, hire date of all employees using cursors.

declare
cursor employees_cur is
select employee_id, first_name, last_name, job_title , hire_date
from employees natural join jobs;

emp_st_date date;
begin
dbms_output.put_line( rpad('Employee Name',30) || rpad('Job Title',35)|| 'Starting Date');
dbms_output.put_line('-----------------------------------------------------------------------------------------
');
for employee_rec in employees_cur
loop
-- find out most recent end_date in job_history
select max(end_date) + 1 into emp_st_date
from job_history
where employee_id = employee_rec.employee_id;
if emp_st_date is null then
emp_st_date := employee_rec.hire_date;
end if;
dbms_output.put_line(rpad((employee_rec.first_name||' '||employee_rec.first_name),30) ||
rpad(employee_rec.job_title,35)
|| to_char(emp_st_date,'dd-mon-yyyy'));
end loop;
end;

OUTPUT=>
Program => 09
Create a data base trigger, which performs the action of the on delete cascade.

 CREATE TABLE Employee (


emp_id int(10) NOT NULL,
name varchar(40) NOT NULL,
birthdate date NOT NULL,
gender varchar(10) NOT NULL,
hire_date date NOT NULL,
PRIMARY KEY (emp_id)
);
 INSERT INTO Employee (emp_id, name, birthdate, gender, hire_date) VALUES
(101, 'Bryan', '1988-08-12', 'M', '2015-08-26'),
(102, 'Joseph', '1978-05-12', 'M', '2014-10-21'),
(103, 'Mike', '1984-10-13', 'M', '2017-10-28'),
(104, 'Daren', '1979-04-11', 'M', '2006-11-01'),
(105, 'Marie', '1990-02-11', 'F', '2018-10-12');

 CREATE TABLE Payment (


payment_id int(10) PRIMARY KEY NOT NULL,
emp_id int(10) NOT NULL,
amount float NOT NULL,
payment_date date NOT NULL,
FOREIGN KEY (emp_id) REFERENCES Employee (emp_id) ON DELETE CASCADE
);
 INSERT INTO Payment (payment_id, emp_id, amount, payment_date) VALUES
(301, 101, 1200, '2015-09-15'),
(302, 101, 1200, '2015-09-30'),
(303, 101, 1500, '2015-10-15'),
(304, 101, 1500, '2015-10-30'),
(305, 102, 1800, '2015-09-15'),
(306, 102, 1800, '2015-09-30');

mysql> DELETE FROM Employee WHERE emp_id = 102;


Program => 10
Write a data base trigger, which should not delete from emp table if the day is Sunday.

CREATE OR REPLACE TRIGGER EMPNO_CHECK


BEFORE DELETE ON emp
BEGIN
if to_char(sysdate,'dAy')='SUNDAY' then
raise_application_error(-20001,'TO DAY IS SUNDAY ');
end if;
END;
Output=>
ERROR at line 1:
*ORA-20500: THE PRI KEY RULE IS VOILATED
ORA-06512: at "sEMPNO_CHECK ", line 4
ORA-04088: error during execution of trigger ' EMPNO_CHECK '
Program => 11
Write a data base trigger, which acts just like primary key and does not allow duplicate
values.
CREATE OR REPLACE TRIGGER PRIKEY BEFORE INSERT ON EMP
FOR EACH ROW
DECLARE
A NUMBER;
BEGIN
SELECT COUNT(*) INTO A FROM EMP WHERE EMPNO=:NEW.EMPNO;
IF A >=1 THEN
RAISE_APPLICATION_eRROR(-20500,'THE PRI KEY RULE IS
VOILATED');
ELSIF A=0 THEN
PRINT('RECORD IS INSERTED');
END IF;
END;

SQL> INSERT INTO EMP(EMPNO,DEPTNO) VALUES(7788,20);


INSERT INTO EMP(EMPNO,DEPTNO) VALUES(7788,20)

Output=>
ERROR at line 1:
*ORA-20500: THE PRI KEY RULE IS VOILATED
ORA-06512: at "GEETHA.PRIKEY", line 6
ORA-04088: error during execution of trigger 'GEETHA.PRIKEY'

SQL> INSERT INTO EMP(EMPNO,DEPTNO) VALUES(77,20);

You might also like