mysql - 如何复制表并保持相同的键和编码?
只是写了一个简短的脚本,将我的所有表复制到另一个本地数据库中。非常简单,但检查后我发现按键消失了,并且文本字段的编码也设置为某种默认编码。
如何使用我的脚本并在复制后复制相同的键和相同的编码,但不为每个表手动定义。我必须自动完成这件事。
我的代码非常简单:
$first_db = 'first_db';
$second_db = 'second_db';
$result = mysql_query("SHOW TABLES FROM $first_db");
while ($row = mysql_fetch_array($result)){
$current_table = $row["Tables_in_$first_db"];
$result2 = mysql_query("DROP TABLE IF EXISTS $second_db.$table_name");
$result2 = mysql_query("CREATE TABLE $second_db.$table_name SELECT * FROM $first_db.$current_table");
}
我想我应该使用 ALTER TABLE,但是它可以自动查找并定义键和编码吗?
谢谢
Just wrote a short script of copying all my tables into another local database. was quite easy but after checking it I found that the keys are gone and also the encoding for text fields are set to some default encoding.
How can I use my script and have the same keys and same encoding duplicated after copying, but not manually define for each table. I have to get this done automatically.
my code is pretty straight forward:
$first_db = 'first_db';
$second_db = 'second_db';
$result = mysql_query("SHOW TABLES FROM $first_db");
while ($row = mysql_fetch_array($result)){
$current_table = $row["Tables_in_$first_db"];
$result2 = mysql_query("DROP TABLE IF EXISTS $second_db.$table_name");
$result2 = mysql_query("CREATE TABLE $second_db.$table_name SELECT * FROM $first_db.$current_table");
}
I think I should use ALTER TABLE, but can it find and define the keys and encoding automatically ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过先复制表,然后再填充数据?
创建像first_db.table_name一样的表second_db.table_name
;然后开始填充数据。
insert into secondary_db.table_name select * from first_db.table_name
;Have you tried copying the table first then populate the data aftewards?
create table second_db.table_name like first_db.table_name
;And then start populating the data afterwards.
insert into second_db.table_name select * from first_db.table_name
;您可以使用
CREATE TABLE foo LIKE bar
和 INSERT...SELECT 语句。http://dev.mysql.com/doc/refman/ 5.1/en/create-table.html说:
You can use
CREATE TABLE foo LIKE bar
and an INSERT...SELECT statement.http://dev.mysql.com/doc/refman/5.1/en/create-table.html says: