CREATE TABLE FROM SELECT

suggest change

You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement:

CREATE TABLE stack (
    id_user INT,
    username VARCHAR(30),
    password VARCHAR(30)
);

Create a table in the same database:

-- same database, all columns (indexes and the PRIMARY KEY are not copied)
CREATE TABLE stack2 AS SELECT * FROM stack;

-- same database, only some columns
CREATE TABLE stack3 AS SELECT username, password FROM stack;

Create tables from different databases:

-- another database, all columns (indexes are still not copied)
CREATE TABLE stack2 AS SELECT * FROM second_db.stack;

-- another database, only some columns
CREATE TABLE stack3 AS SELECT username, password FROM second_db.stack;

N.B

To copy from a table in another database, qualify the table name:

FROM NAME_DATABASE.name_table

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents