ORACLE物件表和巢狀表

2021-10-24 06:37:03 字數 3281 閱讀 5416

物件表和巢狀表在plsql工具中均可以在tables裡面查到

物件表和巢狀表在user_tables檢視中均無法查到,在user_objects檢視中可以查到,查詢sql為

select * from user_objects where object_type = 'table'

建立乙個type型別

create type type_object as object (

test_num number(4),

test_var varchar2(20),

test_date date

) ;

依據這個type建立物件表

--drop table object_table;

create table object_table of type_object;

可以像給普遍**插入資料一樣

insert into object_table values (1,'測試物件表1',to_date('20180101','yyyymmdd'));
也可以先將資料轉成type型別再直接插入type資料

insert into object_table values (type_object(2,'測試物件表2',to_date('20180102','yyyymmdd')));
與普通表的查詢一樣

select * from object_table
也可以進行分頁查詢

select * from (select a.*, rownum rn from (select * from object_table) a where rownum < 3) where rn > 1
與普通表一樣,物件表的列屬性也可以在user_tab_columns檢視中查到

select * from user_tab_columns where table_name = 'object_table'
create index my_index on object_table(test_var);

alter table object_table add primary key (test_num);

巢狀表是巢狀在其他表裡面的,因此建立巢狀表時得有個被巢狀的表,在這裡我稱呼其為母表

建立乙個type

create or replace type struct_type as object(

id number,

detail varchar2(100)

)

依據這個type再建立乙個巢狀表型別(其中type可以為任何型別,包括varray 和 object ,通常object 居多。)

create or replace type struct_type_mul as table of struct_type

使用該巢狀表型別,作為乙個列巢狀到母表中

create table nested_table

( id number not null,

name varchar2(60),

detail_nested struct_type_mul

)nested table detail_nested store as nested_table_store;

可以注意到建表語句尾部多了一句:nested table detail_nested store as nested_table_store,這是宣告這個巢狀表型別的列為乙個巢狀表,被宣告的表為:nested_table_store。該建表語句執行後,會建立兩個表,乙個為母表nested_table,乙個就是巢狀表nested_table_store。

巢狀表是無法單獨插入資料的,必須通過母表插入資料:

insert into nested_table values 

(1, 'test', struct_type_mul(struct_type(11,'北京'), struct_type(12, '上海')));

其插入資料的方式與變長陣列varray類似

select * from nested_table
巢狀表的列資料無法在user_tab_columns中查到

巢狀表建立後在檢視:user_nested_tables中存在記錄

select * from user_nested_tables
裡面包含了巢狀表的基本屬性

查詢所有表的時候,查出了所有普通表和物件表,排除了巢狀表(查詢檢視為user_objects)

select * from user_objects o left join user_nested_tables nt on o.object_name = nt.table_name where nt.table_name is null and o.object_type='table'
需注意plsql是把巢狀表顯示在tables裡面了的

在1的基礎上查詢某個表是不是物件表

select * from user_tables where table_name = 'object_table'
若其在user_tables裡面有值則證明其為普通表,否則為物件表

查物件表所依賴的物件

select referenced_name from user_dependencies where referenced_type = 'type' and name = 'object_table'
依據以上資訊就可以拼出該物件表的建表語句了

直接查乙個表是不是巢狀了其它表即可

select * from user_nested_tables where parent_table_name = 'nested_table'
若該表內有巢狀表,則可以查出相關資料。

根據裡面的資料可以拼出巢狀表的建立語句,即nested table detail_nested store as nested_table_store,其中detail_nested為parent_table_column,nested_table_store為table_name

Oracle表連線之巢狀迴圈

在資料庫系統中執行乙個查詢sql語句,如果這個查詢只操作一張表,那麼僅僅涉及到這個表及關聯物件的訪問。訪問方式通常是三種 全表掃瞄 全索引掃瞄和索引掃瞄。如果這個查詢操作兩張及以上的表,那麼需要操作的表之間的連線關係就變得至關重要。資料庫系統執行該sql時,永遠都是兩個結果集關聯。例如,操作三張表,...

oracle復合型別 巢狀表模糊查詢

在表列中使用巢狀表 必須先使用create type命令來建立巢狀表型別,當使用巢狀表型別作為表列的資料型別時,必須為巢狀表指定專門的儲存表 create type phone type01 is table of varchar2 20 create table employee011 id nu...

定義巢狀表

巢狀表是對索引表的擴充套件,與索引表最大的不同在於巢狀表可以儲存到oracle資料庫表中,而索引表僅僅是記憶體表。而且,巢狀表必須使用其構造方法對巢狀表進行初始化。巢狀表 沒有index by子句,這個與索引表直接最明顯的區別,因為巢狀表必須用有序的關鍵字建立,而且關鍵字不能為負數。索引表7369 ...