Dbms Lab All Tasks
Dbms Lab All Tasks
Output:
2. Add a column commission to the EMP table. Commission should be numeric with null values allowed.
Query:
SQL>Alter table empadd(commission number(4));
Output:
Table altered.
Query:
SQL>Alter table empmodify(job varchar2(15));
Output:
Table altered.
5. Add constraint to the emp table that is empno as primary key and deptno as foreign key.
Query:
SQL>alter table emp add constraint emp_id_pk primary key(empno);
SQL>alter table dept add constraint pk primary key(deptno);
Output:
Table altered
SQL>Alter table emp add constraint emp_deptno_fk foreign key(deptno) references dept(deptno);
6. Add constraints to the emp table to check the empno value while entering i.eempno>100. Salary value by
default is 5000, otherwise it should accept the values from the user.
Query:
SQL>alter table emp add check (empno>100);
SQL>alter table emp modify sal default 5000;
Output:
7. Add column DOB to the emp table Add and drop a column DOJ to the emp table.
Query:
SQL>alter table emp add(dob date);
SQL>alter table emp add(doj date);
SQL>alter table emp drop(doj);
Output:
TASK – 2
DML COMMANDS (Insert, Select, Update, Delete)
1.Insert 5 records into dept table.Insert few rows and truncate those from emp1 table and also drop it. .
Query:
SQL>Insert into dept values(&deptno,’&dname’,’&loc’);
SQL>create table emp1 as select * from emp;
SQL>insert into emp1 values(7000, ’King’, ‘Pres’, 10, 20,10000,500, ’12-Jan-92’);
SQL>insert into emp1 values(7010, ‘Jack’, ‘VP’, 10, 30, 9000, 300, ’19-Jul-92’);
SQL>Truncate table emp1;
SQL>Drop table emp1;
Output:
2. Insert 11 records into the emp table.
Query:
SQL>insert into empvalues(&no, ’&name’, ’&job’, &mgr, &deptno, &sal, &comm, ‘&dob’);
Note: Repeat execution of this statement for 11 times for 11 record insertions
Output:
3. Update the emp table to set the default commission of all employees to Rs.1000 /- who are working as
managers.
Query:
SQL>update emp set commission=1000 where job like ‘%Manager%’;
Output:
TASK – 3
SQL Operators
1. List the records in the emp table order by salary in descending order.
Query:
SQL>select * from emp order by saldesc;
Output:
3. Display deptno from the table employee avoiding the duplicate values.
Query:
SQL>select distinct deptno from emp;
Output:
4. List all employee names, salary and 15% rise in salary.Label the column as New Sal.
Query:
SQL>select ename, sal, (sal*1.15) “New Sal” from emp;
Output:
8. Display all the details of the records with 5 character names with ‘S’ as starting character.
Query:
SQL>select * from employees where lengty(last_name)=5 and last_name like ‘s%’;
TASK – 4
SQL Aggregate Functions, Group By clause, Having clause
1. Count the total records in the emp table.
Query: select count(*) from emp;
Output:
3. Determine the maximum and minimum salary of the employees and rename the columns max_salalary and
min_salary.
Query: select max(sal) “max_salary”, min(sal) “min_salary” from emp;
Output:
TASK - 5
Exercise on SQL Functions
1. Display the employee name concatenated with empno.
Query:
select concat(empno, concat(' ', ename)) from emp;
Output:
2. Display half of employee name in upper case and half in lower case.
Query:
Select upper(substr(ename,0,length(ename)/2))||lower(substr(ename,(length(ename)/2)+1,length(ename)))
“Name” from emp;
Output:
7. Display the rounded date in the year format, month format, day format in the employee.
Query:
select round(dob, 'dd'), round(dob, 'month'), round(dob, 'year') from emp;
Output:
8.Display the commissions earned by employees. If they do not earn commission, display it as “No Commission”.
Query:
select employee_id,last_name,nvl(to_char(commission_pct),’No Commission’)”commission” from employees;
TASK – 6
Nested Queries
1. Find the third highest salary of the employees.
Query:
select max(sal) from emp where sal<(select max(sal) from emp where sal<(select max(sal) from emp));
Output:
2. Display all the employee names and salary whose salary is greater than the minimum salary and job title starts
with 'M'.
Query:
select ename, sal from emp where sal>(select min(sal) from emp) and job like 'M%';
Output:
3. Write a Query to display information about employees who earn more than any employee in department 30.
Query:
select empno, ename, sal, deptno from emp where sal>any (select sal from emp where deptno=30);
Output:
4. Display the employees who have the same job as Jones and whose salary>=Fords.
Query:
select empno, ename, sal, job from emp where job= (select job from emp where ename='Jones') and sal>= (select
sal from emp where ename='Fords');
Output:
5. List out the employee names who get the salary> maximum salary of dept with deptno 20,30.
Query:
select ename from emp where sal>(select max(sal) from emp where deptno in(20,30));
Output:
7. Create a table employee with the same structure as the table emp and insert rows into the table using select
clauses.
Query:
SQL>create table employee as (select * from emp);
Output:
8. Create a manager table from the emp table which should hold details only about managers.
Query:
SQL>create table manager as (select * from emp where job like ‘%Manager%’);
TASK – 7
Joins, Set Operators
1. Display all the employees and departments implementing left outer join.
Query: select e.empno, e.ename, d.deptno, d.dname from emp e left outer join dept d on(e.deptno=d.deptno);
Output:
2. Display the employee name and department name in which they are working implementing a full outer join.
Query: select e.ename,d.dname from emp e full outer join dept d on(e.deptno=d.deptno);
Output:
3. Write a Query to display the employee name and manager’s name and salary for all employees.
Query: select e.ename, m.ename “MGR”, m.sal “MGRSAL” from emp e, emp m where e.mgr=m.empno;
Output:
4. Write a Query to Output the name, job, employee number, department name, location for each department even
if there are no employees.
Query: select e.empno, e.ename, e.job, d.dname, d.loc from emp e join dept d on(e.deptno=d.deptno);
Output:
TASK – 8
Views
1.Create a view that displays the employee id, name and salary of employees who belong to
10th department.
Query:
Create view emp_view as select employee_id,last_name,salary from employees where department_id=10;
2.Create a view with read only option that displays the employee name and their department name
Query:
create view emp_dept as select employee_id,last_name,department_id from employees with read onlyh
constraint emp_dept_readonly;
Drop a view.
Query: drop view my_view;
Output:
TASK – 9
Practices on DCL Commands
1. SQL>Create user test identified by pswd;
Output:
2.SQL> Grant create session, create table, create sequence, create view to test;
Output:
3. SQL>Create role manager;
SQL>Grant create table, create view to manager;
SQL>Grant manager to test;
Output:
SQL>select employee_id,last_name,job_id from employees where last_name between 'N' and 'P';
Output:
Default indexes.
SQL>select index_name,table_name from user_indexes where table_name='EMP3';
Output:
4. Displaying all
the indexes.
SQL>Select index_name, table_name from user_indexes;
Output:
4.SQL>select table_name, index_name, column_name from user_ind_columns where table_name='EMPLOYEES';
Output:
5. Dropping an index:
SQL> drop index emp_index;
Output:
SEQUENCE
1. SQL>create sequence my_seq start with 10 increment by 10 maxvalue 100 nocache;
Output:
2.
SQL>select my_seq.nextval from dual;
Output:
Output:
TASK - 9
1. Write a PL/SQL code to retrieve the employee name, join date and designation from employee database of an
employee whose number is input by the user.
Program:
/*Employee details*/
DECLARE
v_name varchar2(25);
v_joindate date;
v_dsgn employees.job_id%type;
BEGIN
END;
Output:
Program:
/*Calculate Tax*/
DECLARE
v_sal number(8);
v_tax number(8,3);
v_name varchar2(25);
BEGIN
if v_sal<10000 then
v_tax:=v_sal*0.1;
else
v_tax:=v_sal*0.3;
END IF;
DBMS_OUTPUT.PUT_LINE('Name:'||v_name||' Salary:'||v_sal||'Tax:'||v_tax);
END;
Output:
3. Write a PL/SQL program to display top 10 employee details based on salary using cursors.
Program:
/*Top 10 salary earning employee details*/
DECLARE
cursor c_emp_cursor is select employee_id, last_name, salary from employees order by salary desc;
v_rec c_emp_cursor%rowtype;
v_i number(3):=0;
BEGIN
open c_emp_cursor;
loop
v_i:=v_i+1;
fetch c_emp_cursor into v_rec;
exit when v_i>10;
DBMS_OUTPUT.PUT_LINE(v_rec.employee_id||' '||v_rec.last_name||' '||v_rec.salary);
END LOOP;
close c_emp_cursor;
END;
/
Output:
4. Write a PL/SQL program to update the commission values for all employees with salary less than 5000 by
adding 1000 to existing employees.
Program:
/*Updation*/
declare
v_emp c_emp%rowtype;
v_temp number(7,2);
v_temp1 number;
BEGIN
open c_emp;
loop
v_temp1:=v_emp.commission_pct;
v_temp:=(v_emp.salary*v_emp.commission_pct)+1000;
v_temp:=v_temp/v_emp.salary;
if(v_emp.salary<5000) then
end if;
end loop;
END;
Output:
TASK – 11
1. Write a trigger on the employee table which shows the old values and new values of ename after any
updations on ename on Employee table.
Program:
create or replace trigger t_emp_name after update of last_name on salary_table FOR EACH ROW
begin
END;
Output:
2. Write a PL/SQL procedure for inserting, deleting and updating in employee table.
Program:
is
BEGIN
case p_case
when 1 then
DBMS_OUTPUT.PUT_LINE('Insertion...');
when 2 then
DBMS_OUTPUT.PUT_LINE('Deletion...');
when 3 then
DBMS_OUTPUT.PUT_LINE('Updation...');
END;
DECLARE
v_id employees.employee_id%type:=&id;
v_sal employees.salary%type:=&sal;
v_case number:=&case1or2or3;
begin
proc_dml(v_id,v_sal,v_case);
END;
Output:
3. Write a PL/SQL function that accepts department number and returns the total salary of the department.
Program:
v_total number;
BEGIN
return v_total;
END;
/
DECLARE
v_dept number:=&department_id;
v_total number;
BEGIN
v_total:=func_dept(v_dept);
END;
Output:
Task-12
Program:
declare
v_id number(6):=&employee_id;
v_sal employees.salary%type;
v_name employees.last_name%type;
v_job employees.job_id%type;
begin
select last_name, salary into v_name, v_sal from employees where employee_id=v_id;
EXCEPTION
end;
Output:
DECLARE
v_dept number:=&department_id;
e_nodept exception;
BEGIN
IF SQL%notfound then
raise e_nodept;
ELSE
END IF;
EXCEPTION
END;
Output:
a. Package specification.
Program:
END pack_dml;
Output:
b. Package body for the insert, retrieve, update and delete operations on student table.
Program:
v_name varchar2(20);
v_total number;
BEGIN
case choice
when 1 then
DBMS_OUTPUT.PUT_LINE('Insertion...');
when 2 then
DBMS_OUTPUT.PUT_LINE('Deletion...');
when 3 then
DBMS_OUTPUT.PUT_LINE('Updation...');
when 4 then
end case;
END proc_dml;
END pack_dml;
BEGIN
pack_dml.proc_dml(&StudentID,&choice1or2or3or4);
END;
Output: