如何使用PK制作表格的副本?

Fer*_*ret 1 sql oracle oracle10g

在 Oracle 10g 数据库中,我想复制现有表。我希望它具有与原始表相同的数据和行。原始表虽然使用 PK,所以我不确定如何复制它并保持它们的唯一性。

col*_*sar 5

oracle 维护 pk 作为列约束。您必须复制该表,然后为新表创建此约束。

以下代码说明了如何完成您的工作。

  -- setting up table t1 - this is just for the sake of demonstration
  create table t1 (
        t_id  integer
      , t_data   varchar2(40)
  );
  alter table t1 modify ( t_id constraint t1_pk primary key );

  insert into t1 values ( 1, 'test');
  insert into t1 values ( 2, 'another test');
  insert into t1 values ( 3, 'final test');
  commit;

  -- copying table t1 (definition + contents) and defining the pk
  create table t2 as ( select * from t1 );
  alter table t2 modify ( t_id constraint t2_pk primary key );
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助,

此致,

卡斯滕