Oracle学习笔记之Oracle的对象类型
一、抽象数据类型
1、创建类型
--地址类型
CREATE OR REPLACE TYPE AddressType AS OBJECT
(
Country varchar2(15),
City varchar2(20),
Street varchar2(30)
);
2、类型嵌套
--创建基于前一个类型的新的抽象数据类型:巨星类型
CREATE OR REPLACE TYPE SuperStarType AS OBJECT
(
StarName varchar2(30),
Address AddressType
);
3、基于抽象类型创建关系表
CREATE TABLE SuperStar
(
StarID varchar(10),
Star SuperStarType
);
4、基于抽象类型创建对象表
CREATE TABLE SuperStarObj of SuperStarType;
5、使用构造方法在表中插入记录
INSERT INTO SuperStar VALUES('001',SuperStarType('Zidane',AddressType('France','Paris','People Street NO.1')));
6、查询表中记录
(1)SQL> SELECT * FROM SuperStar;
STARID
----------
STAR(STARNAME, ADDRESS(COUNTRY, CITY, STREET))
--------------------------------------------------------------------------------
001
SUPERSTARTYPE('Zidane', ADDRESSTYPE('France', 'Paris', 'People Street NO.1'))
(2)
SELECT s.StarID,s.Star.StarName,s.Star.Address.Country,s.Star.Address.City,s.Star.Address.Street FROM SuperStar s
STARID
STAR.STARNAME STAR.ADDRESS.CO STAR.ADDRESS.CITY STAR.ADDRESS.STREET
---------- ------------------------------ --------------- -------------------- ---------------------
001
Zidane France Paris People Street NO.1
7、抽象数据类型的继承
(1)创建一个类型
CREATE OR REPLACE TYPE PersonType AS OBJECT
(
PersonName varchar(10),
PersonSex varchar(2),
PersonBirth date
) not final;
(2)派生一个类型
CREATE OR REPLACE TYPE StudentType UNDER PersonType
(
StudentNO int,
StudentScore int
);
(3)查看数据字典
SQL> DESC StudentType
StudentType extends SYS.PERSONTYPE
Name
------------------------------------------------------------------------------
PERSONNAME
PERSONSEX
PERSONBIRTH
STUDENTNO
STUDENTSCORE
(4)创建对象表
CREATE TABLE student OF StudentType;
(5)向对象表中插入数据
INSERT INTO student VALUES('Rose','nv',to_date('1983-05-02','yyyy-mm-dd'),1001,98);
(6) 查询数据
SQL> SELECT * FROM student;
PERSONNAME PE PERSONBIR STUDENTNO STUDENTSCORE
---------- -- --------- ---------- ------------
Rose
nv 02-MAY-83 1001 98
二、可变数组
1、创建带有可变数组的表
(1)创建可变数组的基类型
CREATE OR REPLACE TYPE MingXiType AS OBJECT
(
GoodID varchar2(20),
InCount int,
ProviderID varchar(20)
);
(2)创建嵌套项类型的可变数组
CREATE OR REPLACE TYPE arrMingXiType AS VARRAY(100) OF MingXiType;
(3)创建一个主表
CREATE TABLE InStockOrder
(
OrderID varchar(15) Not Null Primary Key,
InDate date,
OperatorID varchar(15),
MingXi arrMingXiType
);
2、操作可变数组
(1)插入数据
INSERT INTO InStockOrder
VALUES('200710110001',TO_DATE('2007-10-11','YYYY-MM-DD'),'007',
arrMingXiType(MingXiType('G001',100,'1001'),
MingXiType('G002',888,'1002'))
);
(2)查询数据
SQL> SELECT * FROM InStockOrder;
ORDERID
INDATE OPERATORID
--------------- --------- ---------------
MINGXI(GOODID, INCOUNT, PROVIDERID)
----------------------------------------------------------------------
200710110001
11-OCT-07 007
ARRMINGXITYPE(MINGXITYPE('G001', 100, '1001'), MINGXITYPE('G002', 888, '1002')
(3)使用Table()函数
SQL> SELECT * FROM Table(SELECT t.MingXi FROM InStockOrder t
WHERE t.OrderID='200710110001');
GOODID
INCOUNT PROVIDERID
-------------------- ---------- --------------------
G001
100 1001
G002
888 1002
(4)修改数据
UPDATE InStockOrder
SET MingXi=arrMingXiType(MingXiType('G001',200,'1001'),
MingXiType('G002',8888,'1002'))
WHERE OrderID='200710110001'
注意:不能更新VARRAY中的单个元素,必须更新整个VARRAY
三、嵌套表
1、创建嵌套表
(1)创建嵌套表的基类型
CREATE OR REPLACE TYPE MingXiType AS OBJECT
(
GoodID varchar2(20),
InCount int,
ProviderID varchar(20)
) not final;
(2)创建嵌套表类型
CREATE OR REPLACE TYPE nestMingXiType AS TABLE OF MingXiType;
(3)创建主表,其中一列是嵌套表类型
CREATE TABLE InStockTable
(
OrderID varchar(15) Not Null Primary Key,
InDate date,
OperatorID varchar(15),
MingXi nestMingXiType
) Nested Table MingXi STORE AS MingXiTable;
2、操作嵌套表
(1)向嵌套表中插入记录
INSERT INTO InStockTable
VALUES('20071012001',TO_DATE('2007-10-12','YYYY-MM-DD'),'007',
nestMingXiType(MingXiType('G003',666,'1001'),
MingXiType('G004',888,'1002'),
MingXiType('G005',8888,'1003'))
);
(2)查询数据
SQL> SELECT * FROM InStockTable;
ORDERID
INDATE OPERATORID
--------------- --------- ---------------
MINGXI(GOODID, INCOUNT, PROVIDERID)
----------------------------------------------------------------------------------------------------
20071012001
12-OCT-07 007
NESTMINGXITYPE(MINGXITYPE('G003', 666, '1001'), MINGXITYPE('G004', 888, '1002'), MINGXITYPE('G005', 8888, '1003')
(3)使用Table()函数
SQL> SELECT * FROM Table(SELECT T.MingXi FROM InStockTable t
WHERE OrderID='20071012001')
GOODID
INCOUNT PROVIDERID
-------------------- ---------- --------------------
G003
666 1001
G004
888 1002
G005
8888 1003
(4)更新嵌套表中的数据
UPDATE Table(SELECT t.MingXi FROM InStockTable t WHERE OrderID='20071012001') tt
SET tt.InCount=1666 WHERE tt.GoodID='G003';
(5)删除表中数据
DELETE Table(SELECT t.MingXi FROM InStockTable t WHERE OrderID='20071012001') tt
WHERE tt.GoodID='G003'
四、对象表
1、创建对象表
CREATE TABLE ObjectTable OF MingXiType;
2、向表中插入数据
INSERT INTO ObjectTable VALUES('G001',500,'P005');
INSERT INTO ObjectTable VALUES('G002',1000,'P008');
3、查询对象表中的记录
A 直接查询
SQL> SELECT * FROM ObjectTable;
GOODID
INCOUNT PROVIDERID
-------------------- ---------- ---------------
G001
500 P005
G002
1000 P008
B 用VALUE()函数查询
SQL> SELECT VALUE(O) FROM ObjectTable O;
VALUE(O)(GOODID, INCOUNT, PROVIDERID)
------------------------------------------
MINGXITYPE('G001', 500, 'P005')
MINGXITYPE('G002', 1000, 'P008')
4、查看对象标志符(OID)
A REF操作符引用行对象
SQL> SELECT REF(t) FROM ObjectTable t;
REF(T)
--------------------------------------------------------------------------------
0000280209771F103ED34842478A9C439CDAEFEF6324B0ACF849F14BD7A8B52F4B0297D1C90040A9
5A0000
0000280209A2D3359E0F0C44B3AF652B944F8823F524B0ACF849F14BD7A8B52F4B0297D1C90040A9
5A0001
B 将OID用于创建外键
CREATE TABLE Customer
( CustomerID varchar(10) PRIMARY KEY,
CustomerName varchar(20),
CustomerGoods REF MingXiType SCOPE IS ObjectTable,--引用MingXiType外键,关联的是OID的值
CustomerAddress varchar(20)
);
C 向Customer表中插入数据,此表将从上面创建的对象表中引用数据
INSERT INTO Customer SELECT '007','Yuanhy',REF(O),'France'
FROM ObjectTable O
WHERE GoodID='G001';
D 查询Customer表
SQL> SELECT * FROM Customer;
CUSTOMERID CUSTOMERNAME
---------- --------------------
CUSTOMERGOODS
-----------------------------------------------------------------------------
CUSTOMERADDRESS
--------------------
007
Yuanhy
0000220208771F103ED34842478A9C439CDAEFEF6324B0ACF849F14BD7A8B52F4B0297D1C9
France
E 用DEREF操作符返回对象的值
SQL> SELECT CustomerID,CustomerName,DEREF(t.CustomerGoods),CustomerAddress
2 FROM Customer t;
CUSTOMERID CUSTOMERNAME
---------- --------------------
DEREF(T.CUSTOMERGOODS)(GOODID, INCOUNT, PROVIDERID)
----------------------------------------------------------------------------
CUSTOMERADDRESS
--------------------
007
Yuanhy
MINGXITYPE('G001', 500, 'P005')
France
五、对象视图
将关系表化装成对象表
1、 创建对象视图
A 创建基于关系表父表的对象类型
CREATE OR REPLACE TYPE depttype AS OBJECT
(
deptid number(10),
deptname varchar(30),
loc number(10)
);
B 创建基于关系表的对象视图
CREATE VIEW deptview OF depttype WITH OBJECT OID(deptid) AS
SELECT department_id,department_name,location_id FROM dept;
C 查询视图
SQL> SELECT * FROM deptview;
DEPTID DEPTNAME LOC
---------- ------------------------------ ----------
10 Administration 1700
20 Marketing 1800
30 Purchasing 1700
40 Human Resources 2400
50 Shipping 1500
60 IT 1400
70 Public Relations 2700
SQL> select ref(t) from deptview t;
REF(T)
----------------------------------------------------------------------------------------------------
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
2、创建引用视图(类似于关系表创建一个从表)
CREATE VIEW empview AS SELECT MAKE_REF(deptview,department_id) deptOID,employee_id,
first_name,last_name FROM emp;
查询对象视图empview
SQL> SELECT * FROM empview;
DEPTOID
----------------------------------------------------------------------------------------------------
EMPLOYEE_ID FIRST_NAME
LAST_NAME
----------- -------------------- -------------------------
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
100 Steven King
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
101 Neena Kochhar
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
102 Lex De Haan
00004A038A004667BAC3685B444520A60ED30027E8F25F0000001426010001000100290000000000090604002A00078401FE
103 Alexander Hunold