Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy table structure only 4

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
US
Hi,

I was wondering if there was a way using SQL statements to copy the database structure of one table into another empty table. I saw a tantalizing CREATE TABLE table_name LIKE old_table_name SQL statement in the mysql manual but it doesn't work on our linux server.

Using CREATE TABLE table_name SELECT * FROM old_table_name does not properly copy the structure of the table either. I was hoping to pull this off with a SQL statement because I have to work through an ADO connection. I will not know the physical location of the database to do file copying operations.

Thank you for any help!
 
Failing all else, you could issue the statement &quot;DESCRIBE <tablename>&quot; against the old database, programmatically process that data into a second SQL statement, and issue the SQL statement against the new database. Want the best answers? Ask the best questions: TANSTAAFL!
 
That rocks. Thanks for the info! It got me on the path to SHOW CREATE TABLE <tablename>.
 
You can also use the mysqldump command.

mysqldump -u root -p -d DatabaseName TableName >file.sql

(-d means no data, just structure)

Then use:

mysql -u root -p DatabaseName <file.sql

I like using mysqldump, but it is your preference...

Aaron
 
Thanks for the tips on this. I also had trouble copying the structure from 1 table to another, but the following SQL statement works...

CREATE TABLE new_table_name SELECT * FROM old_table_name WHERE 0;

This will create a new table with the same structure, but will not copy any records.

 
thanks dalchri for showing us a nundocumented feature. one star from me also



[ponder]
----------------
ur feedback is a very welcome desire
 
I actually got it off of the MySQL-Front UI for the MySQL database server. MySQL-Front is a GUI but it actually lists the SQL statments that it is issuing to produce its GUI presentation. Its a treasure trove for the reverse engineer!
 
SHOW CR TAB can be nice indeed. In Database Workbench, I'm using it to parse FKs for InnoDB tables. It's the only way to get info about the FKs, no other info stored. Anywhere. :-/

Martijn Tonies
Database Workbench - the developer tool for InterBase, Firebird, MySQL & MS SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top