Oracle
 sql >> डेटाबेस >  >> RDS >> Oracle

Oracle एपेक्स - कई लिंक के साथ लुकअप टेबल

जब आप SQL वर्कशॉप में लुकअप टेबल बनाते हैं, तो APEX काम करने के लिए कई DDL और DML स्टेटमेंट जेनरेट करता है। विज़ार्ड के अंतिम चरण में, आपको कोड देखने के लिए नीचे SQL क्षेत्र का विस्तार करने में सक्षम होना चाहिए। दुर्भाग्य से, यह अच्छी तरह से स्वरूपित नहीं होता है, लेकिन इसे साफ करना बहुत कठिन नहीं है।

एक परीक्षण के रूप में, मैं अंदर गया और ईएमपी तालिका के जॉब कॉलम पर एक लुकअप टेबल बनाया। यहां वह कोड है जो उत्पन्न हुआ था। मैंने इसे प्रारूपित कर दिया है और उन भागों की व्याख्या करने के लिए टिप्पणियां जोड़ दी हैं जिनकी आपको आवश्यकता होगी और जिनकी आपको आवश्यकता नहीं होगी।

/*
* 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");

लुकअप टेबल को पॉप्युलेट करने वाले इंसर्ट स्टेटमेंट के लिए, आपको यहां संशोधित संस्करण की आवश्यकता होगी:

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
  );

यह सुनिश्चित करेगा कि लुकअप तालिका में केवल नए, अद्वितीय मान जोड़े गए हैं।




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter त्रुटि

  2. मैं ऑब्जेक्ट प्रकारों की नेस्टेड तालिकाओं के साथ Oracle तालिका कैसे बनाऊं?

  3. Oracle PL/SQL को सर्वर का IP v4 मिलता है?

  4. जेपी प्रोग्रेसबार अपडेट नहीं होता है, कोई सुराग नहीं मिल रहा है

  5. मेवेन निर्भरता ग्रहण में हल नहीं हुई