Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Perbarui beberapa nilai kolom tabel menggunakan kueri tunggal

/** XXX CODING HORROR... */

Bergantung pada kebutuhan Anda, Anda dapat menggunakan tampilan yang dapat diperbarui . Anda membuat tampilan tabel dasar dan menambahkan pemicu "bukan" ke tampilan ini dan Anda memperbarui tampilan secara langsung.

Beberapa contoh tabel:

create table party (
    party_id integer,
    employee_id integer
    );

create table party_name (
    party_id integer,
    first_name varchar2(120 char),
    last_name varchar2(120 char)
    );

insert into party values (1,1000);   
insert into party values (2,2000);
insert into party values (3,3000);

insert into party_name values (1,'Kipper','Family');
insert into party_name values (2,'Biff','Family');
insert into party_name values (3,'Chip','Family');

commit;

select * from party_v;

PARTY_ID    EMPLOYEE_ID    FIRST_NAME    LAST_NAME
1            1000           Kipper        Family
2            2000           Biff          Family
3            3000           Chip          Family

... lalu buat tampilan yang dapat diperbarui

create or replace view party_v
as
select
    p.party_id,
    p.employee_id,
    n.first_name,
    n.last_name
from
    party p left join party_name n on p.party_id = n.party_id;

create or replace trigger trg_party_update
instead of update on party_v 
for each row
declare
begin
--
    update party
    set
        party_id = :new.party_id,
        employee_id = :new.employee_id
    where
        party_id = :old.party_id;
--
    update party_name
    set
        party_id = :new.party_id,
        first_name = :new.first_name,
        last_name = :new.last_name
    where
        party_id = :old.party_id;
--
end;
/

Sekarang Anda dapat memperbarui tampilan secara langsung...

update party_v
set
    employee_id = 42,
    last_name = 'Oxford'
where
    party_id = 1;

select * from party_v;

PARTY_ID    EMPLOYEE_ID    FIRST_NAME    LAST_NAME
1            42             Kipper        Oxford
2            2000           Biff          Family
3            3000           Chip          Family


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Layanan Database di AWS dan Oracle Cloud Platform

  2. Oracle- Split string koma delimited (string berisi spasi dan koma berturut-turut)

  3. Cara melihat pernyataan Oracle SQL aktual yang sedang dieksekusi

  4. NVL() Fungsi di Oracle

  5. Bagaimana cara menyalin atau mengimpor skema Oracle antara dua database yang berbeda di server yang berbeda?