05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 ■ DATABASE TABLES 411<br />

ops$tkyte@ORA10G> create or replace type address_type<br />

2 as object<br />

3 ( city varchar2(30),<br />

4 street varchar2(30),<br />

5 state varchar2(2),<br />

6 zip number<br />

7 )<br />

8 /<br />

Type created.<br />

ops$tkyte@ORA10G> create or replace type person_type<br />

2 as object<br />

3 ( name varchar2(30),<br />

4 dob date,<br />

5 home_address address_type,<br />

6 work_address address_type<br />

7 )<br />

8 /<br />

Type created.<br />

ops$tkyte@ORA10G> create table people of person_type<br />

2 /<br />

Table created.<br />

ops$tkyte@ORA10G> desc people<br />

Name Null? Type<br />

---------------------------------------- -------- ----------------------------<br />

NAME<br />

VARCHAR2(30)<br />

DOB<br />

DATE<br />

HOME_ADDRESS<br />

ADDRESS_TYPE<br />

WORK_ADDRESS<br />

ADDRESS_TYPE<br />

That’s all there is to it. We create some type definitions, <strong>and</strong> then we can create tables of<br />

that type. The table appears to have four columns representing the four attributes of the<br />

PERSON_TYPE we created. We are at the point where we can now perform DML on the object<br />

table to create <strong>and</strong> query data:<br />

ops$tkyte@ORA10G> insert into people values ( 'Tom', '15-mar-1965',<br />

2 address_type( 'Reston', '123 Main Street', 'Va', '45678' ),<br />

3 address_type( 'Redwood', '1 <strong>Oracle</strong> Way', 'Ca', '23456' ) );<br />

1 row created.<br />

ops$tkyte@ORA10G> select * from people;<br />

NAME DOB HOME_ADDRESS(CITY, S WORK_ADDRESS(CITY, S<br />

----- --------- -------------------- --------------------<br />

Tom 15-MAR-65 ADDRESS_TYPE('Reston ADDRESS_TYPE('Redwoo

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!