[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 02-08-2006, 05:46 AM   #1 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


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
__________________
The mass purge has begun.
moondog is offline  
Old 02-08-2006, 12:42 PM   #2 (permalink)
Professional Monkey
 
Amnezia's Avatar
 
Join Date: Jul 2005
Location: Escaped from the zoo
Posts: 908
13.25 NP$ (Donate)

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;
__________________
[http://www.webmasterwords.com/python-split-and-join-examples]Python Tutorials[/url]
Amnezia is offline  
Old 02-08-2006, 12:53 PM   #3 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


Quote:
Originally Posted by Amnezia
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;
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
__________________
The mass purge has begun.

Last edited by moondog; 02-08-2006 at 01:03 PM.
moondog is offline  
Old 02-08-2006, 01:22 PM   #4 (permalink)
Senior Member
 
superprogrammer's Avatar
 
Join Date: Aug 2004
Location: Washington
Posts: 4,319
0.13 NP$ (Donate)

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 and free domains list atwww.FreeDroplists.com
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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

Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 09:06 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85