1. Database Structure
Create or replace type CategoryType as object( categoryid int, categoryname varchar2(50) ) not final
Create a table of CategoryType
Create or replace type ProductCategory as Table of CategoryType
Create an inheritable (specified by "not final" construct) object Product
Create or replace type Product as object( productid int, productname varchar2(100), price number(7,2) ) not final
Create type ProductSize
Create or replace type ProductSize as object( sizecode varchar2(20), sizevalue varchar2(20) )
Create collection array for colors
create or replace type ColorArray as varray(20) of varchar2(20)
Create a type ClotheType that inherits from Product
Create or replace type ClotheType under Products( clothesize productsize, clothecolor colorarray, clothecategory productcategory, member function total_price return number ) --define the body for the member function total_price create or replace type body ClotheType as member function total_price return number is begin return (price + price * 0.1); end; end;
Finally create a table of the type ClotheType
create or replace table Clothes of clothetype nested table clothecategory store as prod_cat
4. Inserting rows into the Clothes table
Now we insert rows into the Clothes table.
insert into clothes values(3,'V-neck tshirts with full sleeve',250, productsize('M','Medium'), colorarray('White','Blue','Black'), productcategory(categorytype('1','T-Shirts'), categorytype('2','Men''s Wears') ) ); insert into clothes values(2,'Round-neck tshirts',450, productsize('L','Large'),colorarray('Red','Black','Green'), productcategory(categorytype('1','T-Shirts'), categorytype('2','Men''s Wears') ) ); insert into clothes values(1,'Both side design tshirts',600, productsize('S','Small'),colorarray('Red','Blue','Black'), productcategory(categorytype('1','T-Shirts'), categorytype('2','Men''s Wears') ) );5. Querying the table Clothes
a. Normal query to select the columns productid, productname, pirce and productsize's columns sizecode and sizevalue
select c.productid,c.productname,c.price, c.clothesize.sizecode,c.clothesize.sizevalue from clothes c
b. Querying the categories in a product from the nested table clothecategory
select c.productid,c.productname,p.categoryid,p.categoryname from clothes c,table(c.clothecategory) p where c.productid=1; select c.productid,c.productname,p.categoryid,p.categoryname from clothes c,table(c.clothecategory) p where c.productid=2;
c. Querying the colors associated with a product
select c.productid, c.productname,p.* from clothes c, table(c.clothecolor) p where c.productid=2;
d. Querying the total price by calling the member function total_price
select p.productid, p.productname,p.price,p.total_price() "Total Price" from clothes p;
6.Conclusion