mysql如何在一个表中建立多个表示,从两个或多个现有表中创建一个新表(MySQL)...

本文探讨了如何在MySQL中使用CREATE TABLE语句结合现有表创建新表。通过示例代码展示了如何利用SELECT * FROM...WHERE FALSE的方式创建新表,并指出可以指定新表的索引和重命名列。同时,提到了使用NATURAL JOIN来合并同名列的可能性。
摘要由CSDN通过智能技术生成

Question:

Is it possible to create a table in mySQL from two or more existing tables?

Details:

Can I create a table like so:

CREATE TABLE IF NOT EXISTS USERNAME ( AGE INT NOT NULL )

CREATE TABLE IF NOT EXISTS USERAGE ( NAME VARCHAR(100) NOT NULL )

CREATE TABLE IF NOT EXISTS USER LIKE USERNAME LIKE USERAGE;

I can't make heads or tails of Reference Manual:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

{ LIKE old_tbl_name | (LIKE old_tbl_name) }

Does the trailing (LIKE old_tbl_name) mean 1 or more occurrences of LIKE old_tbl_name?

If it is possible, then what happens to same name columns, or two primary ids?

EDIT: I am trying to use the schema of existing tables to define a new table, not trying to populate a new table with the contents of existing tables.

解决方案

How about:

CREATE TABLE IF NOT EXISTS USER SELECT * FROM USERNAME, USERAGE WHERE FALSE;

You can furthermore specify any indexes you require in the new table prior to the SELECT keyword, or rename columns/select some subset as usual on the right of SELECT:

CREATE TABLE IF NOT EXISTS USER (PRIMARY KEY(UNAME)) SELECT NAME AS UNAME -- etc

If you want to combine all columns of the same name, just use NATURAL JOINs in your SELECT.

See CREATE TABLE ... SELECT for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值