Cloning an existing table

suggest change

A table’s structure can be cloned as follows:

CREATE TABLE ClonedPersons LIKE Persons;

The new table will have exactly the same structure as the original table, including indexes and column attributes.

You can also create a table from the rows of another table:

CREATE TABLE ClonedPersons SELECT * FROM Persons;

You can use any of the normal features of a SELECT statement to modify the data as you go:

CREATE TABLE ModifiedPersons
SELECT PersonID, CONCAT(FirstName, LastName) AS FullName FROM Persons
WHERE LastName IS NOT NULL;

In MySQL, + is arithmetic, not string concatenation. Primary keys and indexes are not copied by CREATE TABLE ... SELECT. Declare them yourself:

CREATE TABLE ModifiedPersons (PRIMARY KEY (PersonID))
SELECT PersonID, CONCAT(FirstName, LastName) AS FullName FROM Persons
WHERE LastName IS NOT NULL;

Feedback about page:

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



Table Of Contents