DB2中結構化型別和型別化表的管理

2021-08-29 06:32:16 字數 3207 閱讀 8447

db2中結構化型別和型別化表的管理

使用結構化型別定義的表稱為型別化表,同樣使用結構化型別定義的檢視稱為型別化檢視,

乙個結構化型別可以是另乙個結構化型別(超型別)的子型別,子型別可以繼承超型別的所有屬

性,並且可以增加新的屬性,乙個子型別也可以是其它結構型別的超型別,因此使用者可以執行子

型別和超型別去建立乙個結構化型別的型別層次,下面介紹結構化型別和型別化表的管理.

1. 建立結構化型別

create type語句可以建立結構化型別

如:create type  type_new as

(emp_no  varchar(40)

qty_age  integer)

ref using  integer

mode db2sql;

2. 建立表並且引用這個新建立的型別

create table employee of type_new

(ref is oid user generated);

create table emp of emp_t under employee

inherit select privileges;

表中的列oid是物件識別符號,每個型別化表中必須有oid列作為第一列的,oid列並且是唯一的.

oid列的資料型別是reference.

ref is定義oid列的列名.

user generated子語句表示每當插入新行時,該行的oid列的值由使用者決定,一旦插入成功,

該列oid的數值就不在允許更新.

inherit select privileges表示在超表中擁有select許可權的任何使用者或組在新建的子表中

將被授予乙個相同的許可權.

3. 新建表employee進行插入資料

insert into employee

(oid,emp_no,qty_age)

values

(type_new(1),'s1895',33);

上面的例子中結構化型別type_new用兩個屬性emp_no,qty_age來定義的,然後在型別化表

employee中使用該結構化型別typ_new進行定義

4. 修改結構化型別

alter type 語句可以增加或刪除乙個已經存在的結構化型別的屬性

如:alter type type_new add attribute tel_no char(12);

alter type type_new drop attribute tel_no;

注意:如果乙個型別或它的子型別是乙個已經存在的表的型別,則不能用alter type來修改之.

5. 刪除型別化表

用語句drop table hierarchy table_name可以刪除型別化表

用語句drop view  hierarchy view_name可以刪除型別化檢視

6. 從型別化表中查詢

查詢全部記錄

select *  from employee;

只查詢表employee上的行,使用only語句

select *  from only(employee);

不僅查詢指定表上的列,還要查詢該錶的子表上的列,可以使用outer語句

select *  from outer(employee);

7. 更新型別化表中的記錄

update employee set qty_age=40  where oid=type_new(2) ;

強制轉換函式type_new將整數型別轉換為reference型別

8. 刪除型別化表中的記錄  

delete from table;

如果使用者只刪除特定型別表(不包括它的子表)中的行,可以使用only語句

delete from only(table);

9. 型別化表屬性的查詢

syscat.tables中的rowtypeschema列和rowtypename列包含型別化表的資訊.

syscat.datatypes可以查詢每乙個結構化型別.

syscat.hierarchies包括子表和它的直接超表間的關係以及子型別和它的直接超型別間的關係.

其中metatype列的包括物件的關係型別編碼如下:

r---結構化型別之間的關係

u---型別化表間關係

w---型別化檢視間關係

10.引用列

在型別化表定義中,使用者可以將列定義為另乙個型別化表的引用列.

被引用的型別化表稱為目標表

如:create type dept_t as (name char(10),;ocation char(20))

ref using integer mode db2sql;

create type emp_t under type_new

as (salary integer,deptref ref(dept_t)) mode db2sql;

emp_t的定義中規定了兩個屬性,乙個是integer的salary,另乙個是reference型別的deptref,

ref(dept_t)意味emp_t型別的deptref屬性是引用型別的,並且引用目標是行型別dept_t或

dept_t的子型別的表中的行.

下面是建立基於上門的結構化型別的型別化表

create table dept of dept_t(ref is oid user generated);

create table emp of emp_t under employee

inherit select privileges

(deptref with options scope dept);

deptref with options scope dept表示deptref列的數值指向dept表中的行

scope被稱為作用域,型別化表emp引用領乙個型別化表的引用列,這種關係在create table時稱為作用域.

11.解除引用操作符(dereference operator)

解除引用操作符(->)從有oid列匹配的行返回目標表或它的子表的命名列值.

如:select e.name from emp e

where e.deptref->location='austn';

上面的語句等價於

select e.name from emp e,dept d

where e.deptref=d.oid

and d.location='austn';

DB2 中的LONG VARCHAR 型別

db2 中long varchar與varchar 資料型別都用來儲存長文字,但是它們之間的用法有很大不同。varchar 與普通資料型別一樣,要使用到bufferpool,在建立表時受制於最大的bufferpool page size,而long varchar 則與lob資料一樣,有單獨的儲存區...

DB2 中的LONG VARCHAR 型別

db2 中long varchar與varchar 資料型別都用來儲存長文字,但是它們之間的用法有很大不同。varchar 與普通資料型別一樣,要使用到bufferpool,在建立表時受制於最大的bufferpool page size,而long varchar 則與lob資料一樣,有單獨的儲存區...

DB2 中的LONG VARCHAR 型別

db2 中long varchar 與varchar 資料型別都用來儲存長文字,但是它們之間的用法有很大不同。varchar 與普通資料型別一樣,要使用到bufferpool,在建立表時受制於最大的bufferpool page size,而long varchar 則與lob資料一樣,有單獨的儲存...