sql >> Databáze >  >> RDS >> Oracle

Oracle APEX - vyhledávací tabulka s několika odkazy

Když vytvoříte vyhledávací tabulku v SQL Workshopu, APEX vygeneruje několik příkazů DDL a DML, aby tuto úlohu provedl. V posledním kroku průvodce byste měli být schopni rozbalit oblast SQL ve spodní části, abyste viděli kód. Bohužel nevychází dobře naformátovaný, ale není příliš těžké jej vyčistit.

Jako test jsem šel dovnitř a vytvořil vyhledávací tabulku ve sloupci JOB tabulky EMP. Zde je vygenerovaný kód. Zformátoval jsem jej a přidal komentáře, abych vysvětlil, které části budete potřebovat a které ne.

/*
* Creates the lookup table. Not needed after the first pass.
*/
create table "JOB_LOOKUP"(
  "JOB_ID" number not null primary key, 
  "JOB" varchar2(4000) not null
);

/*
* Creates the sequence for the primary key of the lookup table. 
* Not needed after the first pass.
*/
create sequence "JOB_LOOKUP_SEQ";

/*
* Creates the trigger that links the sequence to the table.
* Not needed after the first pass. 
*/
create or replace trigger "T_JOB_LOOKUP" 
before insert or update on "JOB_LOOKUP" 
for each row 
begin 
if inserting and :new."JOB_ID" is null then 
  for c1 in (select "JOB_LOOKUP_SEQ".nextval nv from dual) loop 
    :new."JOB_ID" := c1.nv;   end loop; end if; 
end;
/

/*
* Inserts the distinct values from the source table into the lookup
* table. If the lookup table already contains ALL of the needed values,
* country names in your case, then you can skip this step. However, if
* the source table has some values that are not in the lookup table, then
* you'll need to execute a modified version of this step. See notes below.
*/
insert into "JOB_LOOKUP" ( "JOB" ) 
select distinct "JOB" from "DMCGHANTEST"."EMP"
where "JOB" is not null;

/*
* The rest of the statements add the foreign key column, populate it,
* remove the old column, rename the new column, and add the foreign key.
* All of this is still needed.
*/
alter table "EMP" add "JOB2" number;

update "EMP" x set "JOB2" = (select "JOB_ID" from "JOB_LOOKUP" where "JOB" = x."JOB");

alter table "EMP" drop column "JOB";
alter table "EMP" rename column "JOB2"  to "JOB_ID";
alter table "EMP" add foreign key ("JOB_ID") references "JOB_LOOKUP" ("JOB_ID");

Pokud jde o příkaz insert, který vyplní vyhledávací tabulku, zde je upravená verze, kterou budete potřebovat:

insert into "JOB_LOOKUP" ( "JOB" ) 
select distinct "JOB" from "DMCGHANTEST"."EMP"
where "JOB" is not null
  and "JOB" not in (
    select "JOB"
    from JOB_LOOKUP
  );

To zajistí, že do vyhledávací tabulky budou přidány pouze nové, jedinečné hodnoty.




  1. oracle 11g a integrace hibernate spring a jsf

  2. PLS-00386:Mezi proměnnými FETCH a INTO byla nalezena neshoda typu

  3. Zkoumání chyby ORA 028513 DG4ODBC

  4. Django Admin - přihlášení