DBMS_SQL
DBMS_SQL
Create a data model for the given business scensrio and prepare schema using
appropriate SQL structures . Insert data to check validate the following integrity
constraints
1.Entity integrity
2.Row integrity
3.Referential integrity
Table created.
Table created.
1 row created.
SQL> insert into employee values(02,'Aditi','f',666,'06-jun-06');
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> insert into assigned_to values(2,1);
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
EMPNO PROJNO
---------- ----------
1 1
2 1
4 2
3 1
5 4
2 2
5 3
4 5
3 2
9 rows selected.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------
Term Work 2:
a. Write SQL statement to obtain the empID# of all employees working on Project =1.
c. WSQL to get the details of the employee who is working on 'DBMS' project
e. WSQL to get the details of the employee who is working on either project 1 or 2.
;
SQL> select e.* from employee e,assigned_to at
where e.empno=at.empno
and (projno=1 AND projno=2);
-----------------------------------------------------------------------------------
------------------
T3:Modify the schema to store the information about fine to be paid by employees
create table employee_fine
(
empNo int not null,
fine Number ,
constraints VIOLATION_PK_CONSTRAINTS
primary key(empNo,fine),
foreign key(empNo)
references employee
);
Table created.
T4 :Modify the schema to store the details of dependents for all employees if
exixts.
1.List all employees who have 2 dependents
2.List all the employees who have their mother as dependent
Table created.
1.
select e.empName
from employee e
Join dependents d on e.empNo = d.empNo
group by e.empNo,e.empName
having count(d.empNo) = 2;
EMPNAME
-------------------------
Ananya
2.
select e.empName from employee e,dependents d
2 where e.empNo = d.empNo
3 and d.deprelation='Mother';
EMPNAME
-------------------------
Ananya
T5 .
T6.
***********************************************************************************
***************
T7.WSQL statement to display name and DOB of all employees who are on the bench
-----------------------------------------------------------------------------------
-----
T8 . WSQL statement to display the name of all employees working on all projects
***********************************************************************************
********
T9 . Display name of all eployees working on atleast all of the project that
employee 1 is working
select empName
from employee
where empNo
in(select empNo from assigned_to where projNo
in(select projNo from assigned_to where empNo=1));
-----------------------------------------------------------------------------------
--------
T10 .Display the details of senior most 3 employees.
SELECT *
FROM (SELECT * FROM employee ORDER BY dob)
WHERE ROWNUM <= 3;
-----------------------------------------------------------------------------------
-----
T11.Find for each employee is penalty incurred
select e.empNo,sum(ef.fine)
from employee e
join employee_fine ef on e.empNo = ef.empNo
group by ef.empNo;
EMPNO SUM(EF.FINE)
---------- ------------
1 490
2 145
3 855
4 340
===================================================================================
======
===================================================================================
======
T13.Study of Order by clause
a)Order By
SQL>select * from employee
order by dob asc;
6 rows selected.
b)Alter
SQL>Alter table employee
add email char(30);
Table altered.
SQL>
SQL> select * from employee;
5 Mani m 7 15-APR-68
6 rows selected.
MIN(SALARY)
-----------
14500
SQL> select Max(salary) from employee;
b) Max()
MAX(SALARY)
-----------
55000
c) Sum()
SQL> select sum(salary) from employee;
SUM(SALARY)
-----------
187500
d) Avg()
SQL> select Avg(salary) from employee;
AVG(SALARY)
-----------
31250
e) variance()
SQL> select variance(salary) from employee;
VARIANCE(SALARY)
----------------
229975000
f)stddev()
SQL> select stddev(salary) from employee;
STDDEV(SALARY)
--------------
15164.9266
===================================================================================
========
T15.Study of clauses
a) between
b) LIKE
SQL>select *
from tenant
where name LIKE 'G%';
c) ALL
select
d)ANY
select projectName
from project
where projNo = ANY
(
select projNo
from assigned_to
where empNo = 2 OR empNo=4);
SQL>
PROJECTNAME
--------------------
DBMS
AIML
IOT
e) IN
'SQL> select * from assigned_to
where ProjNo in 1;
EMPNO PROJNO
---------- ----------
1 1
2 1
3 1
4 1
5 1
f) EXISTS
SQL> select name
from tenant
where exists
(
select *
from holdProperty
where Tenant.TID = HoldProperty.TID);
NAME
---------------------------------------------
David
Virat
Rohit
Smriti
Gagan
Akshata Gaonkar
6 rows selected.
g)RowNum
SQL> select *
from employee
where rownum<=3;
h)count()
COUNT(EMPNO)
------------
6
i) Distinct
===================================================================================
========
T16.Study of date related functions
CURRENTDA
---------
05-NOV-24
DATEPLUS3
---------
05-FEB-25
SQL> SELECT NEXT_DAY(SYSDATE, 'FRIDAY') AS NextFriday FROM dual;
NEXTFRIDA
---------
08-NOV-24
ENDOFMONT
---------
30-NOV-24
STARTOFMO
---------
01-NOV-24
===================================================================================
========
T17.Study of views
a)with check option
create view emp1 as
select empNo , dob,salary
from employee
where dob>='1-Jan-68';
View created.
6 rows selected.
View created.
6 rows selected.
View dropped.
View dropped.
===================================================================================
========T18 . Study of a)Copying table b)Synonym
a)
SQL> create table emp_details
AS select * from employee;
Table created.
6 rows selected.
b)synonym
SQL> CREATE SYNONYM emp_syn FOR EMPLOYEE;
Synonym created.
6 rows selected.
===================================================================================
========
SQL> declare
projectCount integer;
status char(20);
begin
insert into project values(10,'CN','IRU');
select count(projNo) into projectcount from project;
if projectCount>3 then
status := 'Very few projects';
else
status:='suffiecient project';
END if;
delete from project where projNo=10;
SQL> declare
rowNo integer;
pName char(20);
projectCount integer;
begin
rowNo:=1;
select count(projNo) into projectCount from project;
while rowNo<=4 loop
select projectName into pName from project
where projNo=rowNo;
DBMS_output.put_line('Project Name = '||pName);
rowNo := rowNo+1;
End loop;
end;
===================================================================================
========
T20.Study of TRIGGERS
Trigger created.
EMPNO NEWSALARY
---------- ----------
4 55000
Trigger created.
1 row updated.