Skip to content

Instantly share code, notes, and snippets.

@RobertWang
Created May 9, 2014 02:57
Show Gist options
  • Save RobertWang/da027802900f53d18d2a to your computer and use it in GitHub Desktop.
Save RobertWang/da027802900f53d18d2a to your computer and use it in GitHub Desktop.
[mysql]在MySQL数据库中拷贝数据表
-- 在 MySQL 中拷贝表,将 old_table 表拷贝为 new_table 表。
-- 1. 不拷贝表数据,只拷贝结构。
CREATE TABLE new_table LIKE old_table
-- 2. 通过 SELECT 查询来拷贝,new_table 表会丢失主键、索引等信息。
CREATE TABLE new_table AS
(
SELECT *
FROM old_table
)
-- 3. 完全拷贝表
CREATE TABLE new_table LIKE old_table;
INSERT INTO new_table SELECT * FROM old_table;
-- 4. 仅拷贝字段
CREATE TABLE new_table AS
(
SELECT field1, field2 FROM old_table
)
-- 5. 部分拷贝
CREATE TABLE new_table AS
(
SELECT * FROM old_table WHERE field1 = 'mangguo'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment