我有一個attendance table名為的 Oracle 模式attendance_database和另一個名為的模式payroll_database,其表名是payroll_attendance_table.
如果我插入新資料attendance_table然后payroll_attendance_table與新插入的資料自動同步(自動插入/更新),是否有可能。我聽說它可以通過觸發器來完成。是嗎,不然還有什么辦法。我想在資料庫端處理這個,不想用任何后端語言處理這個。
uj5u.com熱心網友回復:
任何一個:
- 創建從
attendance_database到的資料庫鏈接payroll_database。 attendance_database.attendance_table為INSERT跨資料庫鏈接的新行創建一個行級觸發器到payroll_database.payroll_attendance_table.
或者:
- 創建從
payroll_database到的資料庫鏈接attendance_database。 - 創建
payroll_attendance_table一個MATERIALIZED VIEW具有FAST REFRESH ON COMMIT的attendance_database.attendance_table跨資料庫鏈接。
uj5u.com熱心網友回復:
首先,由于資料只是在兩個不同的模式中,你確定你真的需要將它從一個復制到另一個嗎?如果attendance_database.attendance_table和payroll_database.payroll_attendance_table應該具有相同的資料,那么消除一個并僅在payroll_database該點中創建一個同義詞會更有意義attendance_database。
grant select on attendance_database.attendance_table
to payroll_database;
create synonym payroll_database.payroll_attendance_table
for attendance_database.attendance_table;
如果它們不應該具有相同的資料(payroll_database.payroll_attendance_table例如,該表應該具有其他屬性),您很可能想要創建payroll_database.payroll_attendance_table一個子表。就像是
grant references on attendance_database.attendance_table
to payroll_database;
create table payroll_database.payroll_attendance_table (
attendance_id integer references attendance_database.attendance_table( attendance_id ),
<<additional attributes>>
);
如果您真的,真的想擁有相同資料的兩個單獨副本并通過觸發器維護它們,您可以執行以下操作
create or replace trigger trg_pointless_sync
before insert or update or delete on attendance_database.attendance_table
for each row
declare
begin
if inserting
then
insert into payroll_database.payroll_attendance_table( attendance_id,
col1,
col2,
...
colN )
values( :new.attendance_id,
:new.col1,
:new.col2,
...,
:new.colN );
elsif updating
then
update payroll_database.payroll_attendance_table
set col1 = :new.col1,
col2 = :new.col2,
...
colN = :new.colN
where attendance_id = :new.attendance_id;
else
delete from payroll_database.payroll_attendance_table
where attendance_id = :new.attendance_id;
end if;
end;
當然,基于觸發器的解決方案通常有些問題。它們可以通過將源表上良好、快速的基于集合的操作轉換為遞回的逐行操作來施加相當大的性能損失。如果某個 DBA 想要快速恢復attendance_table到以前的狀態,因為有人誤操作了某些資料并且沒有意識到洗掉所有資料會產生從payroll_database表中洗掉所有資料的副作用,那么他們可能會產生維護問題。而且它們幾乎總是會產生同步問題——例如,如果有人修改了 中的一行,會發生什么payroll_database?或者如果有人更新了 PKattendance_table,我撰寫的觸發器不夠聰明,無法正確處理(當然,您可以擴展它,但通常這些極端情況比您可能撰寫的要多),因此這兩個表最終將是不太同步。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/361896.html
上一篇:Oracle18:根據給定日期的最大狀態計數,使用分組資料
下一篇:結果集未正確關閉
