Refresh query on multiple tables

I have a circuit like:

  • employees (eno, ename, zip, hdate)
  • customers (cno, cnmae, street, zip code, phone)
  • zipcodes (zip, city)

where zip is pk in zipcodes and fk in other tables.
I have to write an update request that updates the entire origin of zipcode 4994 to 1234 across the entire database.

update zipcodes,customers,employees  
  set zip = 0  
where customers.zip = zipcodes.zip 
  and employees.zip = zipcodes.zip;  

but I know that I am not doing it right. Is there a way to update all zip tables in a single update request?

+3
source share
6 answers

Oracle does not support instructions for updating multiple tables.

  • Confirm that zipcode "1234" exists in the table ZIPCODES

    INSERT INTO ZIPCODES 
      (zip, city)
    VALUES
      (1234, '?')
    
  • CUSTOMERS EMPLOYEES:

    UPDATE CUSTOMERS
       SET zip = 1234
     WHERE zip = 4994
    
    UPDATE EMPLOYEES
       SET zip = 1234
     WHERE zip = 4994
    
  • :

    DELETE FROM ZIPCODES
     WHERE zip = 4994
    
+3

. .

NEW THOUGHT:. , 1 , .

+2

, zip zipcode, zip , , zipcode.

+1

, : SQL . , , Oracle . , , , . , , - .

; , . , , " ", zipcode, .

: Oracle:( .

+1
source

Use a stored procedure. There you can perform this operation on multiple tables using conditional statements.

0
source

This may lead to your problem. And you can update or delete in the for loop database for values

Begin
for Zip_value in 1234.. 4559 loop
update zipcodes
  set zip = 0   
where customers.zip = zipcodes.zip  
  and employees.zip = zipcodes.zip;
  and zipcodes.zip = Zip_value;
update customers
  set zip = 0   
where customers.zip = zipcodes.zip  
  and employees.zip = zipcodes.zip;
  and zipcodes.zip = Zip_value;
update employees   
  set zip = 0   
where customers.zip = zipcodes.zip  
  and employees.zip = zipcodes.zip;
  and zipcodes.zip = Zip_value;
  END LOOP;
commit;
end;
/
0
source

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


All Articles