Write a query to create a new table which consists of data and structure copied from the other table
-- structure and data
CREATE TABLE students_backup AS
SELECT * FROM students;
-- only the structure (empty table, with indexes)
CREATE TABLE students_empty LIKE students;
-- structure first, then copy the data
INSERT INTO students_empty SELECT * FROM students;
CREATE TABLE ... AS SELECT copies the columns and the data, but it does not copy the primary key, indexes and auto increment. CREATE TABLE ... LIKE copies the full structure, including indexes, but no data. Use WHERE in the SELECT to copy only some rows.
Learn more about the similar topics:
| Tutorials |
|---|
| MySQL Data Types |
| MySQL CREATE DATABASE |
| MySQL USE and SHOW DATABASES |
| MySQL DROP DATABASE |
| MySQL CREATE TABLE |
| MySQL Constraints |
| MySQL DROP TABLE |
| Exercises & Assignments |
|---|
| Write a query to get all column names of a database table |
| Write a query to create a new table with the data and structure of another table |