19

I have quite a large and complex table in a MySQL database that would be a pain to have to build a query for manually to recreate if anything went wrong.

Is there someway I can get MySQL or some other tool/script I can use to get a Query made automatically that would recreate the structure of the table?

Ideally if you suggested a tool/script it would be for Linux

9 Answers 9

26
SHOW CREATE TABLE `thetable`;

Also any backup method like mysqldump should give you an option to save structure as well as data.

Sign up to request clarification or add additional context in comments.

1 Comment

wow as simple as that, either my google skills are lacking or my eyes weren't working
24

from http://dev.mysql.com/doc/refman/5.0/en/create-table.html

to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:

CREATE TABLE new_tbl LIKE orig_tbl;

Comments

16

execute these queries :

If you want to copy table structure with primary key, foreign key and constrains with data...

CREATE TABLE newtableName LIKE oldTableName; 
INSERT newtableName SELECT * FROM oldTableName;

Comments

4

You can use the mysqldump tool to export the structure and data of a mysql table.

http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

1 Comment

mysqldump -u <username> -p<password> <database_name> --no-data --tables <table_name> > table.ddl
3

CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 =0

1 Comment

Creates the new table with the same structure but, does not copy over the primary key to the new table.
1
SHOW CREATE TABLE `table_name`;

Comments

0

you can export the structure of the table from phpmyadmin to a sql file...

Comments

0

I definitely agree with other folks:
with mysqldump you get a dump of your table (option --tables), let's say dumpMyTable.sql
then if you want to load this dump on a new schema:

mysql -uuser_name -ppassword MyNewSchema < dumpMyTable.sql

if you want to load it in the same schema but in a new table you will have to edit the dumpMyTable.sql file and change the name of the table manually. If the dump file is very big, I recommend the use of the "sed" command

Comments

0

You may use SqlYog for this purpose. Solution is just 2 clicks away from this tool. You may use DataBase menu for this purpose. You can also copy only the schema or the whole data and both to even different servers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.