study/oracle/oracle编程艺术/chapt11/function_base_index.sql
2020-02-23 22:23:40 +08:00

82 lines
2.0 KiB
SQL

/*
* 基于函数的索引
*/
prompt
prompt 'create table eoda.emp and idnex eoda.emp_upper_idx'
declare
row_count number(5);
row_count_emp_idx number(5);
begin
select count(*) into row_count from user_tables where table_name = upper('emp');
if row_count = 1 then
execute immediate 'drop table eoda.emp';
end if;
execute immediate '
create table eoda.emp
as
select *
from scott.emp
where 1=0
';
insert into eoda.emp
(empno,ename,job,mgr,hiredate,sal,comm,deptno)
select rownum empno,
initcap(substr(object_name,1,10)) ename,
substr(object_type,1,9) job,
rownum mgr,
created hiredate,
rownum sal,
rownum comm,
(mod(rownum,4) + 1) * 10 deptno
from all_objects
where rownum < 10000;
select count(1) into row_count_emp_idx from user_indexes where index_name = upper('emp_upper_idx');
if row_count_emp_idx = 1 then
execute immediate 'drop idnex eoda.emp_upper_idx';
end if;
execute immediate '
create index eoda.emp_upper_idx on eoda.emp(upper(ename))
';
dbms_stats.gather_table_stats( user,'EMP',cascade=>true );
end;
/
select * from eoda.emp where upper(ename) = 'KING';
prompt
prompt 'create function my_soundex'
create or replace function my_soundex( p_string in varchar )
return varchar2
deterministic
as
l_return_string varchar2(6) default substr( p_string,1 ,1 );
l_char varchar2(1);
l_last_digit number default 0;
type vcArray is table of varchar2(10) index by binary_integer;
l_code_table vcArray;
begin
stats.cnt := stats.cnt + 1;
l_code_table(1) := 'BPFV';
l_code_table(2) := 'CSKGJQXZ';
l_code_table(3) := 'DT';
l_code_table(4) := 'L';
l_code_table(5) := 'MN';
l_code_table(6) := 'R';
for i in 1 .. length(p_string)
loop
exit when (length(l_return_string) = 6 );
l_char := upper(substr( p_string,1 ,1 ));
for j in 1 .. l_code_table.count
loop
if (instr(l_code_table(j), l_char ) > 0 and j <> l_last_digit )
then
l_return_string := l_return_string || to_char(j,'fm9');
l_last_digit := j;
end if;
end loop;
end loop;
end;
/