MySQL Create Table: 3 Methods Explained
There are several methods for creating a table in MySQL.
- By using the CREATE TABLE statement, you can create a new table directly in MySQL. For example:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
- By using the CREATE TABLE LIKE statement, you can create a new table with the same structure as an existing table. For example:
CREATE TABLE new_table_name LIKE existing_table_name;
- You can use the SELECT INTO statement to insert the results of a query into a new table. For example:
SELECT column1, column2 INTO new_table_name FROM existing_table_name;
- The “CREATE TABLE AS” statement allows you to create a new table and insert the results of a query into the new table. For example:
CREATE TABLE new_table_name AS SELECT column1, column2 FROM existing_table_name;
These methods allow for selecting the appropriate way to create a table based on different needs.