逻辑备库建立数据库链接用于表的实例化:
USER is "SYS"
SQL> connect /as sysDBA
Connected.
SQL> create public database link dblink1
2 connect to scott identified by tiger
3 using 'orcl_pd';
Database link created.
测试数据库链接是否成功:
SQL> select * from emp@dblink1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10
14 rows selected.
禁用guard:
SQL> connect / as sysdba
Connected.
SQL> alter session disable guard;
Session altered.
删除表,制造主库与逻辑备库不同步的条件:
SQL> drop table scott.emp;(可delete from scott.emp,我用的是删除表,数据库链接重建表,删除表数据);
Table dropped.
创建表:
SQL> create table scott.emp as select * from emp@dblink1;
Table created.
SQL> select * from scott.emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10
14 rows selected.
删除数据:
SQL> delete from scott.emp;
14 rows deleted.
提交:
SQL> commit;
Commit complete.
检验:
SQL> select * from scott.emp;
no rows selected
启用guard属性:
SQL> alter session enable guard;
Session altered.
SQL> select * from scott.emp;
no rows selected
重新实例化不一致的表:
SQL> alter database stop logical standby apply;
Database altered.
SQL> execute dbms_logstdby.instantiate_table('SCOTT','EMP','DBLINK1');
PL/SQL procedure successfully completed.
SQL> alter database start logical standby apply immediate;
Database altered.
检测表的重新实例化是否成功:
SQL> select count(*) from scott.emp;
COUNT(*)
----------
14
SQL> /
COUNT(*)
----------
13
SQL> show user;
USER is "SYS"
官方文档参考:
INSTANTIATE_TABLE Procedure
This procedure creates and populates a table in the standby database from a
corresponding table in the primary database. The table requires the name of the
database link (dblink) as an input parameter. If the table already exists in the logical
standby database, it will be dropped and re-created based on the table definition at the
primary database. This procedure only brings over the data associated with the table,
and not the associated indexes and constraints.
Use the INSTANTIATE_TABLE procedure to:
■ Add a table to a standby database.
■ Re-create a table in a standby database.
Syntax
DBMS_LOGSTDBY.INSTANTIATE_TABLE (
schema_name IN VARCHAR2,
table_name IN VARCHAR2,
dblink IN VARCHAR2);
Parameters
Exceptions
Usage Notes
■ Use this procedure to create and populate a table in a way that keeps the data on
the standby database transactionally consistent with the primary database.
■ This table will not be synchronized with the rest of the tables being maintained by
SQL Apply and SQL Apply will not start to maintain it until the redo log that was
current on the primary database at the time of execution is applied to the standby
database.
Examples
SQL> EXECUTE DBMS_LOGSTDBY.INSTANTIATE_TABLE (-
SCHEMA_NAME => 'HR', TABLE_NAME => 'EMPLOYEES', -
DBLINK => 'INSTANTIATE_TBL_LINK');
Table 56–5 INSTANTIATE_TABLE Procedure Parameters
Parameter Description
schema_name Name of the schema
table_name Name of the table to be created or re-created in the standby database
dblink Name of the database link account that has privileges to read and
lock the table in the primary database
Table 56–6 INSTANTIATE_TABLE Procedure Exceptions
Exception Description
ORA-16103 Logical Standby apply must be stopped to allow this operation
ORA-16236 Logical Standby metadata operation in progress
|
|