user3416803
user3416803

Reputation: 379

MYSQL copy fields from one table's structure and add them to another tables

I've added some additional fields for my table in db. Now I need to have this additional fields in other few tables. So the question is - can I somehow copy those fields from source table and add them to another tables? Both mysql console and phpmyadmin variants woulbe be nice. Thanks!

Upvotes: 0

Views: 1266

Answers (1)

Dennis G.
Dennis G.

Reputation: 303

A phpmyadmin variant would be to export the table's structure only (Export->Custom->Choose "Structure"). After that, you will get something like this in the exported SQL file:

 CREATE TABLE `table` (
      `id` int(10) NOT NULL,
      `name` varchar(50) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

At that point, you can go ahead and remove the last line of the query and the parenthesis after the CREATE TABLE. Then, go ahead and substitute the [other_table] name and change the query to match the following:

 ALTER TABLE `[other_table]`
      ADD `id` int(10) NOT NULL,
      ADD `name` varchar(50) DEFAULT NULL;

Notice how all I did was change CREATE to ALTER and add ADD before each field.

NOTE: This is not very useful on such a trivial example, but when dealing with large amounts of columns, it could prove somewhat useful.

Upvotes: 3

Related Questions