oracle-对象表-外部表

http://www.blogjava.net/decode360/archive/2008/10/16/286802.html


create or replace type person as object(
  name varchar2(10),
  sex varchar2(2),
  birthday date,
  age number(2)
);

两种创建对象表的方式

第一,
create table t(
  person_col person,
  emp_id number,
  dep_id number
);

第二,

create table t of person;

delete from t;
insert into t values('ww','f',sysdate-1,23);
insert into t values('ni','m',sysdate-2,24);
insert into t values('ss','f',sysdate-3,25);


insert into t values (
  person('张三','男',sysdate-1,23),
  100,
  10);

insert into t values (
  person('张四','女',sysdate-2,44),
  101,
  11);
 
insert into t values (
  person('wo','fe',sysdate-3,44),
  102,
  11);
insert into t values (
  person('ni','f',sysdate-4,45),
  103,
  12);
delete from t where emp_id=101;

在sqlplus中用下面语句可以看到内容
select * from t;

但在sqldeveloper中看不到person_col列的值,是如下的值

[KYC_ACC.PERSON]    102    11
[KYC_ACC.PERSON]    103    12

一定要用别名,否则引起岐义(schema名字)
select t1.person_col.age from t t1;

###################################

说明了schema object的依赖关系

create table test_table(col1 integer,col2 integer);

create or replace procedure test_proc
as
begin
  for x in (select col1,col2 from test_table)
  loop
    null;
  end loop;
end;
/

--反复查看这个过程的status列是否valid或invalid
select * from user_objects
where object_type='PROCEDURE';

--下面添加列没有依赖
alter table test_table add col3 number;

--但是修改列1时就有依赖关系了
alter table test_table modify col1 varchar2(20);

--这时要重新执行过程,才会变成valid
execute test_proc;

################################

创建普通表

create table employees (
  employee_id number(6),
  first_name  varchar2(20),
  last_name   varchar2(25)
    constraint emp_last_name_nn not null,
  email       varchar2(25)
    default 'aa@qq.com'
    constraint emp_email_nn not null,
  phone_number varchar2(20),
  hire_date date
    --defalut sysdate
    constraint emp_hire_date_nn not null,
  job_id varchar2(10)
    constraint emp_job_nn not null,
  salary number(8,2),
  commission_pct number(2,2),
  manager_id number(6),
  department_id number(4),
  constraint emp_salary_min check (salary>0),
  constraint emp_email_uk unique (email)
);

################################

创建外部表

1先授予权限

grant read,write on directory data_pump_dir to test

2

[oracle@oracle1 dpdump]$  cat student.data
10001@#$kerry@#$male@#$28@#$1
10002@#$jimmy@#$male@#$22@#$1
10003@#$ken@#$male@#$21@#$1
10004@#$merry@#$femal@#$20@#$1
10005@#$ll@#$male@#$30@#$1
this is a bad file

3然后将外部数据文件student.data放入/u01/app/oracle/admin/kyc/dpdump/这个data_pump_dir目录中

4创建外部表

CREATE TABLE EXTER_TEST
(
     ID              NUMBER(5)      ,
     NAME            VARCHAR(12)    ,
     SEX             VARCHAR(8)     ,
     AGE             NUMBER(3)      ,
     GRADE           NUMBER(1)
) ORGANIZATION EXTERNAL
(
            type        oracle_loader
            default directory data_pump_dir
            access parameters
            (
                    records delimited by newline
                    fields terminated by '@#$'
            )
            location ('student.data')
);

5查询
select * from exter_test;

出现下面错误,是因为student.data文件中有不符合规范的记录,可以删除“this is a bad file”这一条记录,但是这是为了测试下面情况,所以可以通过执行

alter table exter_test reject limit unlimited;跳过一些限制。

6如果源文件格式有误,会有日志写入log文件,并且bad文件中记录错误的行

[oracle@oracle1 dpdump]$ ll
total 24
-rw-r----- 1 oracle oinstall  116 Jan 26 15:16 dp.log
-rw-r----- 1 oracle oinstall   19 Apr 18 08:16 EXTER_TEST_27856.bad
-rw-r----- 1 oracle oinstall 3391 Apr 18 08:22 EXTER_TEST_27856.log
-rw-r----- 1 oracle oinstall   19 Apr 18 08:14 EXTER_TEST_27860.bad
-rw-r----- 1 oracle oinstall 1249 Apr 18 08:14 EXTER_TEST_27860.log
-rwxrwxrwx 1 oracle oinstall  146 Apr 18 08:22 student.data
[oracle@oracle1 dpdump]$ pwd
/u01/app/oracle/admin/kyc/dpdump

select * from all_external_locations;
select * from user_external_locations;
select * from dba_external_locations;

select * from user_external_tables;
select * from all_external_tables;
select * from dba_external_tables;

外部表限制:

1. 只能对表进行SELECT,不能进行DELETE、UPDATE、INSERT这些DML操作。
2. 因为外部表需要在ORACLE数据库“服务端”创建目录,OS文件必须放在这些目录中。即这些文件只能放在数据库服务端。如果数据文件不位于服务器,则无法使用外部表
3. 外部表上不能创建索引。但可以建立视图
4. 外部表不支持LOB对象。如果要使用LOB类型,则不能使用外部表。
eg:删除外部表的记录
delete from exter_test where id=10001;

################################

原文地址:https://www.cnblogs.com/createyuan/p/6891625.html