简体   繁体   中英

Creating a new table from two or more existing tables (MySQL)

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 JOIN s in your SELECT .

See CREATE TABLE ... SELECT for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM