像改变表格一样 [英] ALTER TABLE LIKE

查看:72
本文介绍了像改变表格一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 ALTER TABLE 上使用类似于 MySQL 中的 CREATE TABLE 的 LIKE 语句?

Is it possible to use the LIKE statement on ALTER TABLE similar to CREATE TABLE in MySQL?

例如.'创建表 db.tbl1 LIKE db.tbl2'

Eg. 'CREATE TABLE db.tbl1 LIKE db.tbl2'

这会克隆数据库表的结构.我想更改具有相同列的现有表,但要获取另一个表的主键.

This clones a database table's structure. I want to alter an existing table with the same columns but to pick up the primary keys of another table.

我正在考虑诸如ALTER TABLE db.tbl1 LIKE db.tbl2"之类的东西,但这会引发错误.

I was thinking of something like 'ALTER TABLE db.tbl1 LIKE db.tbl2' but this throws back an error.

有什么想法吗?

谢谢

推荐答案

我需要类似的东西并决定使用以下程序:

I required a similar thing and settled to use the following procedure:

ALTER TABLE tbl1 RENAME tbl1_old;
CREATE TABLE tbl1 LIKE tbl2;
INSERT INTO tbl1 SELECT * FROM tbl1_old;
DROP TABLE tbl1_old;

虽然这不是一个单一的声明,但它应该可以完成这项工作.唯一的问题可能是不同的索引设置(UNIQUE 等),这会导致在对原始表内容执行INSERT INTO"操作时出错.

Altough this is not a single statement it should do the job. Only problem could be different index settings (UNIQUE, etc.) which cause errors when doing the "INSERT INTO" of the original table contents.

这篇关于像改变表格一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆