Question

My question is particularly about the approach we use to create a clone or more specifically copy of the table with LIKE operator or with the help of SELECT clause also copy all the constraints from the old table?

    CREATE TABLE newTable LIKE oldTable;

OR

    CREATE TABLE newTable AS (SELECT * FROM oldTable) WITH NO DATA;

Please explain if these above statements will create the exact working clone from the oldTable or it will just create the structure from the oldTable for newTable.

When I'm saying clone I expect that it should have to create a new working copy of the oldTable, which will have all the constraints applied. For example for my TIMESTAMP column which have a definition as shown below.

    LAST_UPDATE_TS TIMESTAMP NOT NULL GENERATED BY DEFAULT FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP;

Does my newTable's LAST_UPDATE_TS will work as it was working in the oldTable?

There are several cases and types of constraints we can have for each column so applying all those manually later or after creating table as we seen above, doesn't remain as a good option. Please explain. Thanks :)

Était-ce utile?

La solution

As far as I understood, how about this ?

SELECT TMP.* INTO DESTINATION_TABLE
FROM
(SELECT * FROM SOURCE_TABLE WHERE 1=0) AS TMP

This works & creates new empty table structure from existing table structure.

Now about the constraints, we will go through following steps,

1]

create table pk (id int primary key);

enter image description here

2]

sp_help pk

enter image description here

3]

select * into fk from (select * from pk) as tmp

enter image description here

4]

sp_help fk

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top