Oracle PL/SQL/Object Oriented Database/Object Table
Содержание
- 1 Crate table with object column
- 2 Create a new type and add it to a table
- 3 Create a table based on user-defined object only
- 4 Create a table with nested user defined type as column
- 5 Create a table with user define varray as column type
- 6 Create table based on single data type
- 7 Create table with nested types
- 8 Create table with user defined type as column
- 9 Create type and use it as table column
- 10 Implementation of many to many using object references
- 11 Implementation of multiple inheritance relationship
- 12 Implementation of one to many using object references
- 13 Multilevel aggregation relationships using nested tables
- 14 Nested table
- 15 Object table: a table of type
- 16 One to one using object references
- 17 Use a table alias and the name of the object
- 18 Use user-defined type to combine query logic
- 19 Use user-defined varray type as column type
Crate table with object column
<source lang="sql">
SQL> SQL> SQL> --User-defined types SQL> SQL> CREATE TYPE address_typ AS OBJECT
2 (StreetNo NUMBER(10), 3 StreetName VARCHAR2(100), 4 AptNo NUMBER(5), 5 City VARCHAR2(100), 6 State VARCHAR2(100), 7 ZipCode NUMBER(9), 8 Country VARCHAR2(100)); 9 /
Type created. SQL> SQL> CREATE TABLE people
2 (ID NUMBER(5), 3 FirstName VARCHAR2(100), 4 LastName VARCHAR2(100), 5 Address address_typ);
Table created. SQL> SQL> SQL> INSERT INTO people
2 VALUES(10, 3 "John", 4 "Smith", 5 address_typ(123,"Happy Lane", NULL, 6 "Smalltown","Alaska", 12345,"USA") );
1 row created. SQL> SQL> SQL> SQL> SELECT * FROM people;
ID FIRSTNAME
----------------------------------------------------------------------------------------------------
LASTNAME
ADDRESS(STREETNO, STREETNAME, APTNO, CITY, STATE, ZIPCODE, COUNTRY)
10.00 John
Smith ADDRESS_TYP(123.00, "Happy Lane", NULL, "Smalltown", "Alaska", 12345.00, "USA")
SQL> SQL> drop table people; Table dropped. SQL> SQL>
</source>
Create a new type and add it to a table
<source lang="sql">
SQL> SQL> create table emp
2 ( empno NUMBER(4) constraint E_PK primary key 3 , ename VARCHAR2(8) 4 , init VARCHAR2(5) 5 , job VARCHAR2(8) 6 , mgr NUMBER(4) 7 , bdate DATE 8 , sal NUMBER(6,2) 9 , comm NUMBER(6,2) 10 , deptno NUMBER(2) default 10 11 ) ;
Table created. SQL> insert into emp values(1,"Tom","N", "TRAINER", 13,date "1965-12-17", 800 , NULL, 20); 1 row created. SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20", 1600, 300, 30); 1 row created. SQL> insert into emp values(3,"Wil","TF" , "Tester",6,date "1962-02-22", 1250, 500, 30); 1 row created. SQL> insert into emp values(4,"Jane","JM", "Designer", 9,date "1967-04-02", 2975, NULL, 20); 1 row created. SQL> insert into emp values(5,"Mary","P", "Tester",6,date "1956-09-28", 1250, 1400, 30); 1 row created. SQL> insert into emp values(6,"Black","R", "Designer", 9,date "1963-11-01", 2850, NULL, 30); 1 row created. SQL> insert into emp values(7,"Chris","AB", "Designer", 9,date "1965-06-09", 2450, NULL, 10); 1 row created. SQL> insert into emp values(8,"Smart","SCJ", "TRAINER", 4,date "1959-11-26", 3000, NULL, 20); 1 row created. SQL> insert into emp values(9,"Peter","CC", "Designer",NULL,date "1952-11-17", 5000, NULL, 10); 1 row created. SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28", 1500, 0, 30); 1 row created. SQL> insert into emp values(11,"Ana","AA", "TRAINER", 8,date "1966-12-30", 1100, NULL, 20); 1 row created. SQL> insert into emp values(12,"Jane","R", "Manager", 6,date "1969-12-03", 800 , NULL, 30); 1 row created. SQL> insert into emp values(13,"Fake","MG", "TRAINER", 4,date "1959-02-13", 3000, NULL, 20); 1 row created. SQL> insert into emp values(14,"Mike","TJA","Manager", 7,date "1962-01-23", 1300, NULL, 10); 1 row created. SQL> SQL> SQL> SQL> create table e
2 as 3 select empno, ename, init, mgr, deptno 4 from emp;
SQL> SQL> SQL> create or replace type numberlist_t
2 as varray(4) of varchar2(20); 3 /
Type created. SQL> SQL> column numlist format a60 SQL> SQL> alter table e add (numlist numberlist_t); Table altered. SQL> SQL> describe e
Name Null? Type ----------------------------------------- -------- ---------------------------- EMPNO NUMBER(4) ENAME VARCHAR2(8) INIT VARCHAR2(5) JOB VARCHAR2(8) MGR NUMBER(4) BDATE DATE SAL NUMBER(6,2) COMM NUMBER(6,2) DEPTNO NUMBER(2) NUMLIST NUMBERLIST_T
SQL> SQL> SQL> select empno, numlist from e; SQL> SQL> SQL> drop table e; Table dropped. SQL> drop table emp; Table dropped. SQL>
</source>
Create a table based on user-defined object only
<source lang="sql">
SQL> SQL> create or replace
2 type person as object ( 3 first_name varchar2(100), 4 last_name varchar2(100), 5 dob date, 6 phone varchar2(100), 7 member function get_last_name return varchar2, 8 member function get_phone_number return varchar2 ) 9 not final 10 /
Type created. SQL> SQL> SQL> create or replace
2 type body person as 3 member function get_last_name return varchar2 is 4 begin 5 return self.last_name; 6 end; 7 member function get_phone_number return varchar2 is 8 begin 9 return self.phone; 10 end; 11 end; 12 /
Type body created. SQL> SQL> create table person_table( p person ); Table created. SQL> SQL> SQL> drop table person_table; Table dropped.
</source>
Create a table with nested user defined type as column
<source lang="sql">
SQL> SQL> create type order_item_type as object (
2 line_item_id number(3), 3 product_id number(6), 4 unit_price number(8,2), 5 quantity number(4) 6 ) 7 /
Type created. SQL> SQL> SQL> create type order_item_list_type as table of order_item_type
2 /
Type created. SQL> SQL> SQL> create table orders(
2 order_id number(12) not null, 3 order_date timestamp(6) with local time zone, 4 customer_id number(6), 5 order_items order_item_list_type ) 6 nested table order_items store as order_items_tab 7 /
Table created. SQL> SQL> drop table orders; Table dropped. SQL> drop type order_item_list_type; Type dropped. SQL> drop type order_item_type; Type dropped. SQL>
</source>
Create a table with user define varray as column type
<source lang="sql">
SQL> SQL> create type employee_type as object (
2 employee_id number, 3 first_name varchar2(30), 4 last_name varchar2(30) 5 ); 6 /
Type created. SQL> SQL> create type employee_list_type as varray(50) of employee_type
2 /
Type created. SQL> SQL> SQL> create table departments (
2 department_id number, 3 department_name varchar2(30), 4 manager employee_type, 5 employees employee_list_type ) 6 /
Table created. SQL> SQL> SQL> SQL> drop table departments; Table dropped. SQL> drop type employee_list_type; Type dropped. SQL> drop type employee_type; Type dropped. SQL> SQL>
</source>
Create table based on single data type
<source lang="sql">
SQL> create table myTable of xmltype; Table created. SQL> SQL> insert into myTable values(XMLTYPE("
2 <customer> 3 <name>name value</name> 4 <telephone>123 555-1234</telephone> 5 </customer>")) 6 /
1 row created. SQL> SQL> select * from myTable;
SYS_NC_ROWINFO$
<customer> <name>name value</name> <telephone>123 555-1234</telephone>
1 row selected. SQL> SQL> SQL> drop table myTable; Table dropped.
</source>
Create table with nested types
<source lang="sql">
SQL> SQL> --CREATE [OR REPLACE] TYPE <object table schema> AS TABLE OF (object schema) SQL> --/
SQL> --CREATE TABLE