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.