NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page How do you copy the structure of a table?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 02-08-2006, 06:46 AM THREAD STARTER               #1 (permalink)
NamePros Regular
 
moondog's Avatar
Join Date: Jun 2004
Posts: 587
moondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to all
 



How do you copy the structure of a table?


How do you copy the table structure from one table to another via an SQL statement so all of the table attributes (like primary keys, what can be null, default values, enum values, etc) remain in tact?

I know you can do it via phpMyAdmin in the "operations" tab, but I need to do this via a script.

Anybody know?

-Bob
__________________
Can't wait to be out of this forsaken business. Getting close! :)
moondog is offline  
Old 02-08-2006, 01:42 PM   #2 (permalink)
Professional Monkey
 
Amnezia's Avatar
Join Date: Jul 2005
Location: Escaped from the zoo
Posts: 907
Amnezia has a spectacular aura aboutAmnezia has a spectacular aura about
 


Cancer Survivorship Save a Life
use this command to create a complete copy of your source table

CREATE TABLE dest_table AS SELECT * FROM source_table;

then to clear the destination table whilst keeping the stucture in place, use the following command

TRUNCATE dest_table;
__________________
Webmaster Words
Amnezia is offline  
Old 02-08-2006, 01:53 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
moondog's Avatar
Join Date: Jun 2004
Posts: 587
moondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to all
 



Originally Posted by Amnezia
use this command to create a complete copy of your source table
????: NamePros.com http://www.namepros.com/programming/165583-how-do-you-copy-structure-table.html

CREATE TABLE dest_table AS SELECT * FROM source_table;

then to clear the destination table whilst keeping the stucture in place, use the following command

TRUNCATE dest_table;
Well, this almost works.

The table is copied, however the primary key information is not ported over.

The primary key in the source table is

id

and is tagged as such, plus it has auto_increment set. In the destination table, neither of these two atriubutes are set.



From here I supposed I can use:

ALTER TABLE <table name> ADD PRIMARY KEY(<field>);

and then

ALTER TABLE <table name> CHANGE <field> <new field name> <data type> <null / not null> <default> AUTO_INCREMENT


but it seems like the primary key + auto_increment should be preserved in the copy-over. . ..

-Bob
__________________
Can't wait to be out of this forsaken business. Getting close! :)
Last edited by moondog; 02-08-2006 at 02:03 PM.
moondog is offline  
Old 02-08-2006, 02:22 PM   #4 (permalink)
Senior Member
 
superprogrammer's Avatar
Join Date: Aug 2004
Location: Washington
Posts: 4,327
superprogrammer has much to be proud ofsuperprogrammer has much to be proud ofsuperprogrammer has much to be proud ofsuperprogrammer has much to be proud ofsuperprogrammer has much to be proud ofsuperprogrammer has much to be proud ofsuperprogrammer has much to be proud ofsuperprogrammer has much to be proud of
 



CREATE TABLE dest_table AS SELECT * FROM source_table where 1=2;
No need of truncate

For constraints you have to check in the data dictionary with the same table name and put it over the new table
__________________
Download youtube videos at www.HugYouTube.com -NO LINKS TO PARKED PAGES-
Anonymous access at www.Banned.net
superprogrammer is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySql Help Xerox Programming 2 12-22-2005 10:53 PM
Frontpage 2003 Table Border vichousefc Web Design Discussion 3 11-21-2005 04:11 AM
Tutorial: Getting Started With MySQL (The Basics) deadserious Webmaster Tutorials 3 04-18-2004 02:17 PM
Copy Any Website!! mantra For Sale / Advertising Board 5 12-30-2003 11:53 AM

Liquid Web Smart Servers  
All times are GMT -7. The time now is 06:19 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger