Using create table tab2 as select * from tab1;, I am able to copy the data but not the primary key constraint :
SQL> desc tab1;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME VARCHAR2(20)
SQL> select * from tab1;
ID NAME
---------- --------------------
1 A
SQL> create table tab2 as select * from tab1;
Table created.
SQL> desc tab2;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
NAME VARCHAR2(20)
SQL> select * from tab2;
ID NAME
---------- --------------------
1 A
SQL>
How can I copy the table with all its constraints as well?
I’d start with something like
This returns a
create tablestatement forTAB1(in schema <schemaname>). You canthen copy that statement and change the identfier TAB1 to TAB2. You should make sure that
you also change the names of all constraints since they must be unique in Oracle.
Finally, you’ll want to do a
insert into TAB2 select * from TAB1