我正在使用 MySQL。我有一个名为 EMP 的表,现在我需要再创建一个具有相同架构、相同列和相同约束的表 (EMP_TWO)。我该怎么做?
最佳答案
要基于另一个表结构/约束创建新表,请使用:
CREATE TABLE new_table LIKE old_table;
若需要复制数据,请使用
INSERT INTO new_table SELECT * FROM old_table;
注意 LIKE 选项的注意事项:
Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:
CREATE TABLE new_table LIKE original_table; The copy is created using the same version of the table storage format as the original table. The SELECT privilege is required on the original table.
LIKE works only for base tables, not for views.
CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions.
关于mysql - 在 MySQL 中创建与另一个表匹配的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761358/