I know how to copy a table using create new_table like old_table
, but that does not copy over the foreign key constraints as well. I can also do string manipulation on the result from show create table old_table
(using regular expressions to replace the table name and the foreign key constraint names), but that seems error prone. Is there a better way to copy the structure of a table, including the foreign keys?
Right-click the table you want to copy in Database Explorer and select Duplicate Object. In the dialog that opens, select the destination db. Select to copy the table data or structure only. Specify the name of the new table, and click OK.
1) Use the ALTER TABLE ... RENAME command and parameter to move the table to the target schema. 2) Use the CREATE TABLE ... CLONE command and parameter to clone the table in the target schema.
Possibly you could write a procedure that after the create table like
prepares ALTER TABLE ...
statements, based on information from:
SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME LIKE '<table_name>' AND TABLE_SCHEMA = '<db_name>' AND REFERENCED_TABLE_NAME IS NOT NULL;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With