mysql create a table from another table code example

Example 1: create table in mysql

# Creates a Simple User table
# Uses an auto-incrementing primary key as userId 

CREATE TABLE user (
    userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100),
    password VARCHAR(100) 
) ENGINE=InnoDB;

Example 2: how to create a table structure from another table in mysql

CREATE TABLE new_tbl LIKE orig_tbl;  //(creates an empty table only definition is
                                        same.)

Example 3: how to create a table based on another table in mysql

Press CTRL+C to copy CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;

Tags:

Sql Example