gpt4 book ai didi

mysql复制列数据类型到另一个表

转载 作者:可可西里 更新时间:2023-11-01 07:18:37 28 4
gpt4 key购买 nike

有没有办法将列的结构从已填充的表复制到空的新表?我只是问复制没有数据的结构

例子:我们有一张 table

CREATE TABLE `animals` (
`animal` varchar(11) NOT NULL,
`food` varchar(11) NOT NULL,
PRIMARY KEY (`animal`)
) ENGINE=InnoDB

INSERT INTO `animals` (`animal`, `food`) VALUES
('cat', 'chips'),
('dog', 'bones'),
('shark', 'ppl');

还有一个名为 predators 的新表,我只想为其创建一列,但数据类型与动物的列类型相同。

有没有一种方法可以将 SHOW COLUMNS/FIELDS 与 CREATE TABLE 相结合,或者使用具有某种类型(如 VARCHAR(17))的列创建表,然后将其更改为与 相同的类型动物列?

我知道这是一个简单的问题,但我还没有找到答案

最佳答案

如果你想复制数据:

INSERT INTO newTable (col1, col2) 
SELECT col1, col2 FROM otherTable

如果要复制表结构:

Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:

CREATE TABLE new_tbl LIKE orig_tbl;

The copy is created using the same version of the table storage format as the original table. The SELECT privilege is required on the original table.

Documentation

如果要复制结构和数据:

CREATE TABLE animals2 AS 
SELECT *
FROM animals ;

如果你想复制没有数据的结构(但不是所有列):

CREATE TABLE animals2 AS 
SELECT animal -- only the columns you want
FROM animals
WHERE FALSE; -- and no data

关于mysql复制列数据类型到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14913555/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com