繁体   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