Answers:
要使用索引和触发器进行复制,请执行以下两个查询:
CREATE TABLE newtable LIKE oldtable;
INSERT INTO newtable SELECT * FROM oldtable;
要仅复制结构和数据,请使用此代码:
CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
我之前曾问过这个问题:
select *
在大表中将如何执行。
AUTO_INCREMENT
值。
除了上述解决方案之外,您还可以将AS
其制作成一行。
CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
create table {new_table} select * from {old_table};
不是答案,也没有提供新的信息。
MySQL方式:
CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new SELECT * FROM production.recipes;
复制表的更好方法是仅使用DDL
语句。这样,您可以独立于表中的记录数而立即执行复制。
我的目的是:
DROP TABLE IF EXISTS table_name_OLD;
CREATE TABLE table_name_NEW LIKE table_name;
RENAME TABLE table_name TO table_name_OLD;
RENAME TABLE table_name _NEW TO table_name;
这样就避免了这样的INSERT AS SELECT
陈述,即在具有大量记录的表的情况下执行可能要花费一些时间。
我建议也创建一个PLSQL过程,如下例:
DELIMITER //
CREATE PROCEDURE backup_table(tbl_name varchar(255))
BEGIN
-- DROP TABLE IF EXISTS GLS_DEVICES_OLD;
SET @query = concat('DROP TABLE IF EXISTS ',tbl_name,'_OLD');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- CREATE TABLE GLS_DEVICES_NEW LIKE GLS_DEVICES;
SET @query = concat('CREATE TABLE ',tbl_name,'_NEW LIKE ',tbl_name);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- RENAME TABLE GLS_DEVICES TO GLS_DEVICES_OLD;
SET @query = concat('RENAME TABLE ',tbl_name,' TO ',tbl_name,'_OLD');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- RENAME TABLE GLS_DEVICES_NEW TO GLS_DEVICES;
SET @query = concat('RENAME TABLE ',tbl_name,'_NEW TO ',tbl_name);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
DELIMITER ;
祝你今天愉快!亚历克斯
扩展这个答案可以使用存储过程:
CALL duplicate_table('tableName');
这将导致一个名为tableName_20181022235959
If 的重复表
SELECT NOW();
结果:
2018-10-22 23:59:59
DELIMITER $$
CREATE PROCEDURE duplicate_table(IN tableName VARCHAR(255))
BEGIN
DECLARE schemaName VARCHAR(255) DEFAULT SCHEMA();
DECLARE today VARCHAR(14) DEFAULT REPLACE(REPLACE(REPLACE(NOW(), '-', ''), ' ', ''), ':', ''); -- update @ year 10000
DECLARE backupTableName VARCHAR(255) DEFAULT CONCAT(tableName, '_', today);
IF fn_table_exists(schemaName, tableName)
THEN
CALL statement(CONCAT('CREATE TABLE IF NOT EXISTS ', backupTableName,' LIKE ', tableName));
CALL statement(CONCAT('INSERT INTO ', backupTableName,' SELECT * FROM ', tableName));
CALL statement(CONCAT('CHECKSUM TABLE ', backupTableName,', ', tableName));
ELSE
SELECT CONCAT('ERROR: Table "', tableName, '" does not exist in the schema "', schemaName, '".') AS ErrorMessage;
END IF;
END $$
DELIMITER ;
DELIMITER $$
CREATE FUNCTION fn_table_exists(schemaName VARCHAR(255), tableName VARCHAR(255))
RETURNS TINYINT(1)
BEGIN
DECLARE totalTablesCount INT DEFAULT (
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA COLLATE utf8_general_ci = schemaName COLLATE utf8_general_ci)
AND (TABLE_NAME COLLATE utf8_general_ci = tableName COLLATE utf8_general_ci)
);
RETURN IF(
totalTablesCount > 0,
TRUE,
FALSE
);
END $$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE statement(IN dynamic_statement TEXT)
BEGIN
SET @dynamic_statement := dynamic_statement;
PREPARE prepared_statement FROM @dynamic_statement;
EXECUTE prepared_statement;
DEALLOCATE PREPARE prepared_statement;
END $$
DELIMITER ;
对于MySQL
CREATE TABLE newtable LIKE oldtable ;
INSERT newtable SELECT * FROM oldtable ;
供MSSQL使用MyDatabase
:
Select * into newCustomersTable from oldCustomersTable;
该SQL用于复制表,此处oldCustomersTable的内容将复制到newCustomersTable
。
确保数据库newCustomersTable
中不存在。