一、复制表结构与数据
create table new_tbl_name select * from old_tbl_name;//复制表结构并插入数据,但没有索引
二、复制表结构
create table new_tbl_name like old_tbl_name;//复制表结构有索引,但不会插入数据
或create table new_tbl_name select * from old_tbl_name where 0=1;#来筛选不合适的数据
以下在python中进行:
from sqlalchemy import create_engine
def get_db(user,db):
db = create_engine("mysql+pymysql://root:{}/{}?charset=utf8".format(user,db))
return db
db_test = get_db('lcx121@localhost','test')
#复制表结构与数据
sql1 = 'create table c select * from a'
db_test.execute(sql1)
#仅复制表结构
sql2 = 'create table e select * from a where 0=1' #where用来筛选不合适数据
db_test.execute(sql2)
本文介绍如何使用MySQL SQL语句及Python脚本复制表结构与数据,包括完整的数据复制及仅复制表结构的方法。

被折叠的 条评论
为什么被折叠?



