You are on page 1of 6

PRACTICAL NO: 2

SQL> create or replace type state61 as object


2 (
3 st_code number(5),
4 st_name varchar2(40),
5 st_district varchar2(50),
6 st_pincode number(7)
7 )
8 /

Type created.
SQL> create or replace type contact_detail61 as object
2 (
3 residence_no number(10),
4 office_no number(10),
5 email varchar2(30),
6 fax number(10),
7 mobile number(10)
8 )
9 /

Type created.

SQL> create or replace type address61 as object
2 (
3 road_no varchar2(7),
4 road_name varchar2(40),
5 landmark varchar2(40),
6 state state61,
7 contact contact_detail61
8* )
9 /

Type created.

SQL> create or replace type staff61 as object(
2 staff_id number(6),
3 staff_name varchar2(40),
4 staff_address address61,
5 staff_deptno number(3),
6 staff_sal number(6),
7 staff_other varchar2(40),
8 dob date,
9 member function getAge return number)
10 /

Type created.

SQL> create or replace type body staff61 as member function getAge return
number as
2 begin
3 return trunc(months_between(sysdate,dob)/12);
4 end getAge;
5 end;
6 /

Type body created.

SQL> create or replace type staffTableType as table of staff61
2 /

Type created.







SQL> create or replace type dept61 as object
2 (
3 dept_id number(3),
4 location varchar2(30),
5 dept_name varchar2(20),
6 emp staffTableType
7 )
8 /

Type created.

SQL> create table dpt_refernce of dept61 nested table emp store as
NTrelation
2 /

Table created.

SQL> insert into dpt_refernce
values(1,'Mumbai','Sales',staffTableType(staff61(1,'EklavyaSingh',addr
ess61('A-1','L.T. road','Status
Hotel',state61(1,'Maharashtra','Mumbai',400092),contact_detail61(289
94177,28182729,'eklavyasingh@yahoo.com',28994177,9818967345)),1,10000,'
HOD','17-aug-1984')));

1 row created.

SQL> insert into dpt_refernce
values(1,'Mumbai','Sales',staffTableType(staff61(2,'SanjayShukla',addr
ess61('C-1','yari
road','',state61(1,'Maharashtra','Mumbai',400069),contact_detail61(26321115
,263317
39,'sanjayshukla@rediffmail.com',26321116,918967345)),1,6000,'HOD','04-
nov-1984')));

1 row created.

SQL> insert into dpt_refernce
values(2,'Mumbai','Accounts',staffTableType(staff61(3,'Sharmila Dave',
address61('C-1','M.G. road','Sanjeevani
Hospital',state61(1,'Maharashtra','Mumbai',400078),contact_d
etail61(28331112,28987058,'sam_dave@hotmail.com',28982430,9833196734)),
2,2000,'clerk','28-sep-1984')
));

1 row created.

SQL> insert into dpt_refernce
values(3,'Mumbai','Purchase',staffTableType(staff61(4,'AnshuDubey',add
ress61('E-2','B.S.road','Vikas
Kendra',state61(2,'Goa','Panji',419832),contact_detail61(26831112,268
97058,'anshudubey@gmail.com',26897059,9820636448)),3,7000,'','09-sep-
1984')));

1 row created.

SQL> insert into dpt_refernce
values(3,'Mumbai','Purchase',staffTableType(staff61(5,'ParthJoshi',add
ress61('E-2','Nehru road','HDFC
Bank',state61(1,'Maharashtra','Vileparle',400056),contact_detail61(2
6149172,26157058,'parthjoshi@yahoo.co.in',26897059,9820739488)),3,9000,'','
29-sep-1985')));

1 row created.







a) Display staff ID and department name of all employees.

SQL> select p.dept_name, q.staff_id from dpt_refernce p,table(p.emp) q;

DEPT_NAME STAFF_ID
-------------------- ----------
Sales 1
Sales 2
Accounts 3
Purchase 4
Purchase 5

b) How many workers are in particular department.

SQL> select p.dept_id,p.dept_name,count(q.staff_id) as
number_of_employees from dpt_refernce p,table
(p.emp) q where p.dept_name='Purchase' group by dept_id,dept_name;

DEPT_ID DEPT_NAME NUMBER_OF_EMPLOYEES
---------- -------------------- -------------------
3 Purchase 2

c) Find department name for particular staff name

SQL> select p.dept_id,p.dept_name from dpt_refernce p,table(p.emp) q
where q.staff_name='EklavyaSing
h';

DEPT_ID DEPT_NAME
---------- --------------------
1 Sales




d) Display department-wise report

SQL> select p.dept_id,p.dept_name,count(q.staff_id) as
number_of_employees from dpt_refernce p,table
(p.emp) q group by p.dept_id,p.dept_name ;

DEPT_ID DEPT_NAME NUMBER_OF_EMPLOYEES
---------- -------------------- -------------------
1 Sales 2
2 Accounts 1
3 Purchase 2

e) Display age and birth date of particular employee

SQL> select q.dob,q.getAge() as Age from dpt_refernce p,table(p.emp) q
where q.staff_name='EklavyaSi
ngh';

DOB AGE
--------- ----------
17-AUG-84 28

You might also like