MySQL复制数据表

文章描述:

mysql使用语句复制一张数据表操作

复制表

复制表结构到新表

CREATE TABLE tp5_goodsx
SELECT * FROM tp5_goods WHERE 1=2

复制完的新表我们会发现少了主键和id为自动增长属性

设置id为主键

ALTER TABLE `tp5_goodsx` ADD PRIMARY KEY( `id`);

设置id为自动增长属性

ALTER TABLE `tp5_goodsx` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT;

完整语句

CREATE TABLE `tp5`.`tp5_goodsx` ( `id` int(11) NOT NULL, `title` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL, `smalltext` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `url` mediumtext COLLATE utf8_unicode_ci, `address` varchar(250) COLLATE utf8_unicode_ci NOT NULL, `inputtime` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `content` mediumtext COLLATE utf8_unicode_ci NOT NULL, `status` tinyint(3) NOT NULL DEFAULT '1' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ALTER TABLE `tp5`.`tp5_goodsx` ADD PRIMARY KEY (`id`); ALTER TABLE `tp5`.`tp5_goodsx` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'; INSERT INTO `tp5`.`tp5_goodsx`(`id`, `title`, `smalltext`, `url`, `address`, `inputtime`, `datetime`, `content`, `status`) SELECT `id`, `title`, `smalltext`, `url`, `address`, `inputtime`, `datetime`, `content`, `status` FROM `tp5`.`tp5_goods`;

 

发布时间:2022/04/21

发表评论