CS-502-DBMS Lab Manual
CS-502-DBMS Lab Manual
CS-503
2021-2022
Subject :- ………………………………………………………..……………
Semester :- ………………………………………………………..……………
|
INDEX
S.No. Experiment Page No. Signature
|
Program => 1
Delete duplicate row from the table.
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-
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
);
Program => 4
Update multiple rows in using single update statement.
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.
Program => 5
Show all employees who were hired the first half of the month
Search the Records
Program => 6
Display the ename, which is start with j, k, l or m.
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';
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.
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'