MySQL 从选择创建表
示例
您可以通过在SELECT语句末尾添加一条语句来从另一个表创建一个表CREATE TABLE:
CREATE TABLE stack (id_user INT,
username VARCHAR(30),
password VARCHAR(30)
);
在同一数据库中创建一个表:
-- create a table from another table in the same database with all attributesCREATE TABLE stack2 AS SELECT * FROM stack;
-- create a table from another table in the same database with some attributes
CREATE TABLE stack3 AS SELECT username, password FROM stack;
从不同的数据库创建表:
-- create a table from another table from another database with all attributesCREATE TABLE stack2 AS SELECT * FROM second_db.stack;
-- create a table from another table from another database with some attributes
CREATE TABLE stack3 AS SELECT username, password FROM second_db.stack;
NB
要创建与另一个数据库中存在的另一个表相同的表,您需要指定数据库名称,如下所示:
FROM NAME_DATABASE.name_table
以上是 MySQL 从选择创建表 的全部内容, 来源链接: utcz.com/z/334574.html