You are on page 1of 8

PRACTICAL NO: 1

SQL> Create or replace type AddrType1 as object(


2 Pincode number(5),
3 Street char(20),
4 City varchar2(50),
5 state varchar2(40),
6 no number(4) )
7 /

Type created.

SQL> create or replace type BranchType as object(
2 address AddrType1,
3 phone1 integer,
4* phone2 integer )
5 /

Type created.

SQL> create or replace type BranchTableType as table of BranchType;
2 /

Type created.

SQL> create or replace type AuthorType as object(
2 name varchar2(50),
3* addr AddrType1 )
4 /

Type created.



SQL> create table authors of AuthorType;

Table created.

SQL> create or replace type AuthorListType as varray(10) of ref AuthorType
2 /

Type created.

SQL> create or replace type PublisherType as object(
2 name varchar2(50),
3* addr AddrType1, branches BranchTableType)
4 /

Type created.

SQL> create table Publishers of PublisherType NESTED TABLE branches
STORE as branchtable
2 /

Table created.

SQL> create table books(
2 title varchar2(50),
3 year date,
4 published_by ref PublisherType,
5* authors AuthorListType)
6 /

Table created.

SQL> insert into Authors
values('Ravi',AddrType1(5002,'sstreet','pune','mha',04));

1 row created.

SQL> insert into Authors
values('Ravi',AddrType1(7007,'sstreet','mumbai','mha',1007));

1 row created.

SQL> insert into Authors
values('Sanjay',AddrType1(7008,'nstreet','nasik','mha',08));

1 row created.

SQL> insert into Authors
values('Jharna',AddrType1(7003,'dstreet','mumbai','mha',1003));

1 row created.

SQL> insert into Authors
values('Paul',AddrType1(7008,'sstreet','mumbai','mha',1007));

1 row created.

SQL> insert into Authors
values('Eena',AddrType1(7006,'nstreet','mumbai','mha',1006));

1 row created.

SQL> insert into Authors
values('Rama',AddrType1(8002,'dstreet','pune','mha',1003));

1 row created.

SQL> insert into Authors
values('Jharna',AddrType1(8003,'dstreet','pune','mha',1003));

1 row created.

SQL> insert into Publishers
values('Priya',AddrType1(4002,'rstreet','mumbai','mha',03),BranchTableTy
pe(BranchType(AddrType1(5002,'fstreet','mumbai','mha',03),23406,69896)));

1 row created.

SQL> insert into Publishers
values('eklavya',AddrType1(7007,'sstreet','mumbai','mha',1007),BranchTab
leType(BranchType(AddrType1(7007,'sstreet','mumbai','mha',1007),4543545,
8676775)));

1 row created.

SQL> insert into Publishers
values('john',AddrType1(7008,'sstreet','mumbai','mha',1007),BranchTableTy
pe(BranchType(AddrType1(1002,'sstreet','nasik','mha',1007),456767,7675757
)));

1 row created.

SQL> insert into Publishers
values('eklavya',AddrType1(7002,'sstreet','pune','mha',1007),BranchTable
Type(BranchType(AddrType1(1002,'sstreet','pune','mha',1007),4543545,8676
775)));

1 row created.

SQL> insert into Publishers
values('william',AddrType1(6002,'sstreet','nasik','mha',1007),BranchTabl
eType(BranchType(AddrType1(6002,'sstreet','nasik','mha',1007),4543545,867
6775)));

1 row created.

SQL> insert into books select 'DSP','28-may-
1983',ref(pub),AuthorListType(ref(aut)) from Publishers
pub,Authors aut where pub.name='john' and aut.name='Eena';

1 row created.

SQL> insert into books select 'compiler','09-jan-
1890',ref(pub),AuthorListType(ref(aut)) from Publi
shers pub,Authors aut where pub.name='william' and aut.name='Jharna';

2 rows created.

SQL> insert into books select 'c','25-may-
1983',ref(pub),AuthorListType(ref(aut)) from Publishers pu
b,Authors aut where pub.name='Priya' and aut.name='Ravi';

2 rows created.

a) List all of the authors that have the same address as their publisher:

SQL> select a.name from authors a, publishers p where a.addr = p.addr;

NAME
--------------------------------------------------
Ravi
Paul









b) List all of the authors that have the same pin code as their publisher:

SQL> select a.name from authors a, publishers p where a.addr.pincode =
p.addr.pincode;

NAME
--------------------------------------------------
Ravi
Sanjay
Paul

c) List all books that have 2 or more authors:

SQL> select * from books b where 1<(select count(*) from table(b.authors));

no rows selected

d) List the title of the book that has the most authors:

SQL> Select title from books b, table(b.authors) group by title having
count(*)=(select max(count(*)
) from books b, table(b.authors) group by title)
2 /

TITLE
--------------------------------------------------
c
compiler







e) List the name of the publisher that has the most branches:

SQL> Select p.name from publishers p, table(p.branches) group by p.name
having count(*)>=all (select
count(*) from publishers p, table(p.branches) group by name);

NAME
--------------------------------------------------
eklavya

f) Name of authors who have not published a book:

SQL> select a.name from authors a where not exists(select b.title from books
b, table(select authors
from books b1 where b.title = b1.title) a2 where a.name = name);

no rows selected

g) Move all the branches that belong to the publisher 'tata' to the publisher
joshi'

SQL> insert into table(select branches from publishers where
name='william') select b.* from publishers p, table(p.branches) b where name
= 'eklavya';

2 rows created.









h) List all authors who have published more than one book:

SQL> select a.name from authors a, books b, table(b.authors) v where
v.column_value = ref(a) group b
y a.name having count(*) > 1;

NAME
--------------------------------------------------
Jharna
Ravi

i) List all books (title) where the same author appears more than once on the
list of authors (assuming that an integrity constraint requiring that the name
of an author is unique in a list of authors has not been specified).

SQL> select title from authors a, books b, table(b.authors) v where
v.column_value = ref(a) group by
title having count(*) > 1;

TITLE
--------------------------------------------------
c
compiler

You might also like