欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql复制表结构的几种方式

程序员文章站 2022-05-29 09:36:01
...

一 远程复制 :

-- t_organization_student
CREATE TABLE `t_organization_student`  (
  `ORG_STU_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '组织学生ID',
  `STU_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生ID',
  `ORG_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '组织ID',
  `ADD_USER_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '添加者',
  `ADD_TIME` varchar(14) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '添加时间',
  `UPD_USER_ID` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
  `UPD_TIME` varchar(14) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新时间',
  PRIMARY KEY (`ORG_STU_ID`) USING BTREE,
  INDEX `ORG_ID`(`ORG_ID`) USING BTREE
) ENGINE=FEDERATED CONNECTION='mysql://root:[email protected]:3308/basesystem/t_organization_student';

二 本地复制(存储过程):

此方式不复制 主键:

set @tableTheoryItem = CONCAT('t_theory_item_',tableType,'_',groupMark);

set @createTable=concat('create table ',@tableTheoryItem,' SELECT * FROM t_theory_item_samp WHERE 1=2');
prepare stmt from @createTable;
EXECUTE stmt;
deallocate prepare stmt; -- 释放掉预处理段

此方式会复制主键 (like):

set @createTable=concat('CREATE TABLE ',@texaminee,' LIKE t_examinee_samp') ;
	prepare stmt from @createTable;
	EXECUTE stmt;
	deallocate prepare stmt; -- 释放掉预处理段