Updating Database Tables

I have the following database tables: enter image description here

In these tables, I have the following elements:

  • Container: may contain any container_item element; The relationship is stored in the table CONTAINER_CANDIDATES
  • Container_Item: may contain any element of the element; The relationship is stored using the COMPOUNDS table.
  • Elements: the main element in my system.

Let me tell you a question using a specific case:

In the ELEMENTS table, I can store the following items:

Id = 1 ; ElementName = 'element001'
Id = 2 ; ElementName = 'element002'
Id = 3 ; ElementName = 'element003'
Id = 4 ; ElementName = 'element004'
Id = 5 ; ElementName = 'element005'
Id = 6 ; ElementName = 'element006'
Id = 7 ; ElementName = 'element007'

In the CONTAINER_ITEM table, I can store the following items:

Id = 1 ; ContainerItemName = 'item-id-aaa'
Id = 2 ; ContainerItemName = 'item-id-bbb'
Id = 3 ; ContainerItemName = 'item-id-ccc'
Id = 4 ; ContainerItemName = 'item-id-ddd'
Id = 5 ; ContainerItemName = 'item-id-eee'

In the CONTAINER table, I can store the following items:

Id = 1; ContainerName = 'ContainerName01';
Id = 2; ContainerName = 'ContainerName02';

Using the COMPOUNDS table, I make the following connections:

    - item-id-aaa  (id = 1 in Container_Item table)
        -> element001 (id = 1 in Elements table)
        -> element002 (id = 2 in Elements table)
    - item-id-bbb (id = 2 in Container_Item table)
        -> element003 (id = 3 in Elements table)
        -> element004 (id = 4 in Elements table)
    - item-id-ccc (id = 3 in Container_Item table)
        -> element005 (id = 5 in Elements table)
        -> element006 (id = 6 in Elements table)
    - item-id-ddd (id = 4 in Container_Item table)
        -> element005 (id = 5 in Elements table)
        -> element007 (id = 7 in Elemens table);
    - item-id-eee (id = 5 in Container_Item table)
        -> element-007 (id = 7 in Elemens table)

Using the CONTAINER_CANDIDATES table, I make the following connections:

        - ContainerName01 contains the following :
            -> item-id-aaa (id = 1 in Container_Item table)
            -> item-id-bbb (id = 2 in COntainer_Item table)
            -> item-id-ccc (id = 3 in COntainer_Item table)
            -> item-id-ddd (id = 4 in COntainer_Item table)
        - ContainerName02 contains the following:
            -> item-id-aaa (id = 1 in Container_Item table)
            -> item-id-eee (id = 5 in COntainer_Item table) 

, . , ContainerName01 ( ), (: ContainerName02) ?

, Oracle PL SQL

+6
2

, , .

-, " " (CONTAINER_CANDIDATES COMPOUNDS), , DELETE CASCADE.

ALTER TABLE CONTAINER_CANDIDATES
ADD CONSTRAINT FK_CC_CONTAINER
   FOREIGN KEY (CONTAINERID)
   REFERENCES CONTAINER (ID)
   ON DELETE CASCADE;

ALTER TABLE CONTAINER_CANDIDATES
ADD CONSTRAINT FK_CC_CONTAINER_ITEM
   FOREIGN KEY (CONTAINERITEMID)
   REFERENCES CONTAINER_ITEM (ID)
   ON DELETE CASCADE;

ALTER TABLE COMPOUNDS
ADD CONSTRAINT FK_COMPOUNDS_CONTAINER_ITEM
   FOREIGN KEY (CONTAINERITEMID)
   REFERENCES CONTAINER_ITEM (ID)
   ON DELETE CASCADE;

ALTER TABLE COMPOUNDS
ADD CONSTRAINT FK_COMPOUNDS_ELEMENTS
   FOREIGN KEY (ELEMENTSID)
   REFERENCES ELEMENTS (ID)
   ON DELETE CASCADE;

, , CONTAINER_ITEM ELEMENTS, .

CREATE OR REPLACE PROCEDURE cascaded_delete_container (
    P_CONTAINER_ID VARCHAR2
) IS
BEGIN
        -- remove the master from supplied ID
        -- cascade on CONTAINER_CANDIDATES
    DELETE FROM CONTAINER
    WHERE ID = P_CONTAINER_ID;

        -- remove CONTAINER_ITEM not used in CONTAINER_CANDIDATES
        -- cascade on COMPOUNDS
    DELETE FROM CONTAINER_ITEM
    WHERE NOT EXISTS(
        SELECT 1 
        FROM CONTAINER_CANDIDATES 
        WHERE CONTAINER_ITEM.ID = CONTAINER_CANDIDATES.CONTAINERITEMID
        );

        -- remove ELEMENTS not used in COMPOUNDS
    DELETE FROM ELEMENTS
    WHERE NOT EXISTS(
        SELECT 1 
        FROM COMPOUNDS 
        WHERE ELEMENTS.ID = COMPOUNDS.ELEMENTSID
        );

    COMMIT;

END;
/

, . Cascade .

- CONTAINER_ITEM ELEMENTS, .

+2

, , , , . , CONTAINER_CANDIDATES COMPOUNDS. :

create or replace procedure delete_container(p_container_id number) is

  -- Get all compound child etries via container ID
  cursor c_get_compounds(cp_container_id number) is
    select comp.id
      from compounds comp, container_candidates cc
     where comp.containerItemID = cc.containerItemID
       and cc.containerID = cp_container_id;

  -- Get all container candidate child entries via container ID
  cursor c_get_container_candidates(cp_container_id number) is
    select cc.id
      from container_candidates cc
     where cc.containerID = cp_container_id;

begin

  -- Fetch all compound entries
  for r in c_get_compounds(cp_container_id => p_container_id) loop
    -- Delete compound entries
    delete from compounds where id = r.id;
  end loop;

  -- Fetch all container candidates
  for r in c_get_container_candidates(cp_container_id => p_container_id) loop
    -- Delete container candidates
    delete from container_candidates where id = r.id;
  end loop;

  -- Delete container entry
  delete from container where id = p_container_id;
end delete_container;

-1

Source: https://habr.com/ru/post/1015819/


All Articles