Cloning an existing table
suggest changeA 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;
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents