Oracle 多資料庫的資料同時更新

來源:互聯網
上載者:User

Oracle 多資料庫的資料同時更新

1.建立dblink

create database link test connect to mall identified by test using '
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 113.116.216.60)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
';

2.建立同義字

create or replace synonym store for sale_record@mall;

執行以下sql可以查看遠端資料庫對應表的內容:

select * from store;

3.建立表,索引

create table sale_record(
 id int primary key,
 storeId int not null,
 saledate date not null,
 productId int,
 num varchar(30) not null
);

CREATE SEQUENCE sale_record_seq
INCREMENT BY 1 -- 每次加幾個
START WITH 1000000000 -- 從1開始計數
NOMAXvalue -- 不設定最大值
NOCYCLE -- 一直累加,不迴圈
CACHE 10;

4.建立觸發器

create or replace trigger addSaleRecords
before insert on sale_record
for each row
declare
  -- local variables here 
 v_ID varchar(10);
 v_storeId number(10); 
 v_saledate date;
 v_productId number(10);
 v_num varchar(40);
begin
 select sale_record_seq.nextval into :new.id  from dual;
 v_ID := :new.id; 
 v_storeId := :new.storeId;
 v_saledate := :new.saledate;
 v_productId := :new.productId;
 v_num := :new.num;
 insert into sale_record@mall(id, storeId, saledate, productId, num) values(v_ID, v_storeId, v_saledate, v_productId, v_num);
end addSaleRecords;

測試:

在本地表中添加資料:

insert into sale_record(id, storeId, saledate, productId, num) values(sale_record_seq.nextval,10000001, '2013-12-12 12:12:12', 900000001, 11);

commit;

執行:

SQL> conn test/test@113.116.216.60:1521/orcl
SQL> select * from sale_record

可以看到遠端資料庫中也被添加了同樣的一條資料。 

Linux-6-64下安裝Oracle 12C筆記

在CentOS 6.4下安裝Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虛擬機器中安裝步驟

Debian 下 安裝 Oracle 11g XE R2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.