簡體   English   中英

Oracle命令從另一個模式(包括觸發器)創建表?

[英]Oracle command to create a table from another schema, including triggers?

使用此命令,我可以從另一個架構創建表,但是它不包含觸發器。 是否可以從另一個架構(包括觸發器)創建表?

create table B.tablename unrecoverable as select * from A.tablename where 1 = 0;

如果您有代碼存儲庫,第一種選擇是為那些對象運行CREATE腳本 我想你不會。

如果使用任何GUI工具,則包含SCRIPT選項卡的事情將變得越來越簡單,該選項卡使您能夠從源代碼復制代碼並將其粘貼到目標用戶。

如果您使用的是SQLPlus ,則意味着您實際上應該知道您應該做什么。 這是一個簡短的演示。

SQL> connect hr/hr@xe
Connected.
SQL> create table detail (id number);

Table created.

SQL> create or replace trigger trg_det
  2  before insert on detail
  3  for each row
  4  begin
  5    :new.id := 1000;
  6  end;
  7  /

Trigger created.

SQL>

SQL> -- you'll have to grant privileges on table to another user
SQL> grant all on detail to scott;

Grant succeeded.

以SCOTT身份連接並檢查我們所擁有的:

SQL> connect scott/tiger@xe
Connected.
SQL> -- now, query ALL_SOURCE and you'll get trigger code
SQL> set pagesize 0
SQL> col text format a50
SQL> select text from all_source where name = 'TRG_DET' order by line;
trigger trg_det
before insert on detail
for each row
begin
  :new.id := 1000;
end;

6 rows selected.

SQL>

另一個選擇是導出和導入表,它也將獲取觸發器(我已經刪除了與Oracle數據庫版本無關的部分):

C:\>exp hr/hr@xe tables=detail file=detail.dmp
About to export specified tables via Conventional Path ...
. . exporting table                         DETAIL          0 rows exported
Export terminated successfully without warnings.

C:\>imp scott/tiger@xe file=detail.dmp full=y
. importing HR's objects into SCOTT
. importing HR's objects into SCOTT
. . importing table                       "DETAIL"          0 rows imported
Import terminated successfully without warnings.

C:\>

檢查導入的內容(應該既是表又是觸發器):

SQL> desc detail
 Name                                      Null?    Type
 ----------------------------------------- -------- ---------------
 ID                                                 NUMBER

SQL> select * From detail;

no rows selected

SQL> insert into detail (id) values (-1);

1 row created.

SQL> select * From detail;

        ID
----------
      1000

SQL>

涼; 連扳機都可以。

也許還有其他選擇,但是這4個應該足以讓您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM