| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| NamePros Regular | 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. |
| |
| | #2 (permalink) |
| Professional Monkey | 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] |
| |
| | #3 (permalink) | |
| NamePros Regular | Quote:
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. | |
| |
| | #4 (permalink) |
| Senior Member | 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 |
| |
![]() |
| 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 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 |