There are three tables
CREATE TABLE A ( ID NUMBER, B_ID NUMBER NOT NULL, ........... CONSTRAINT FK_A_B FOREIGN KEY (B_ID) REFERENCES B (ID) ENABLE VALIDATE, CONSTRAINT PK_A PRIMARY KEY (ID) ENABLE VALIDATE ) CREATE TABLE B ( ID NUMBER, TYPE NUMBER NOT NULL, ........... CONSTRAINT PK_B PRIMARY KEY (ID) ENABLE VALIDATE ) CREATE TABLE C ( ID NUMBER, A_ID NUMBER NOT NULL, DATA NUMBER NOT NULL, ........... CONSTRAINT FK_C_A FOREIGN KEY (A_ID) REFERENCES A (ID) ENABLE VALIDATE, CONSTRAINT PK_A PRIMARY KEY (ID) ENABLE VALIDATE ) ( A_ID pair, DATA not unique and can be repeated up to three times)
After (or during) data import into these tables, it is necessary for all new records to insert records into such a table.
CREATE TABLE D ( ID NUMBER GENERATED ALWAYS AS IDENTITY, A_ID NUMBER NOT NULL, DATA NUMBER, CONSTRAINT FK_D_A FOREIGN KEY (A_ID) REFERENCES A (ID) ON DELETE CASCADE ENABLE VALIDATE, CONSTRAINT PK_D PRIMARY KEY (ID) ENABLE VALIDATE, CONSTRAINT UNQ_D UNIQUE (A_ID, "DATA") ENABLE VALIDATE ) But insert by tricky algorithm.
- For each new entry in Tables
AandCthrough TableA, get the valueB.TYPE - For this value, call the function
my_func, which returns 0 or 1 If the function returns 0, then execute the query
INSERT INTO D (A_ID, DATA) SELECT ID, NULL FROM A WHERE ID = current_id;If the function returns 1, then execute such a request
INSERT INTO D (A_ID, DATA) SELECT DISTINCT A_ID, DATA FROM C WHERE A_ID = current_a_id;
The function my_func is
CREATE FUNCTION my_func (in_type IN NUMBER) RETURN NUMBER DETERMINISTIC IS BEGIN IF (in_type IN (5, 13, 17, 20, 65, 79)) THEN RETURN 1; ELSE RETURN 0; END IF; END; How to solve this problem more effectively? Given that a situation is possible when importing the same data can occur in parallel (if the data already exists, then nothing needs to be done. Rather, you need to update other fields, but they do not belong to this algorithm). The task is complicated by the fact that a situation is possible (although it is unlikely) when additional entries are added to existing entries in С that refer to one entry from A that refer to the same entry and for my_func() = 1 you will need to add entries to D
Thousands of entries in A can come in one import package (it’s not a fact that they will all be new). Up to 10 entries from C can refer to each entry from A