Solved

copy data and structure to new table

Posted on 2006-07-06
20
1,653 Views
Last Modified: 2008-01-09
I have tried these sql statements and its just not working.I am trying to create a table and keep the same data and structure as table1.

I have tried

create table table1 like table2<<<Keeps the same structure but no data

insert into table1(select * from table2) <<<< Keeps crashing the new table

and then I tried create table table1(select * from table2)<<< retrieves the data but not the structure such as primary keys etc.
0
Comment
Question by:thenone
  • 12
  • 4
  • 2
  • +2
20 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 200 total points
ID: 17055983
I have just tried this with some simple tables;

create table `test`.`TableName1` (    `a` int (10)   NULL ,  `b` int (19)   NULL ,  `c` int (19)   NULL    );
insert into `test`.`tablename1` ( `a`, `b`, `c` ) values (  '1',  '2',  '3' );
insert into `test`.`tablename1` ( `a`, `b`, `c` ) values (  '4',  '3',  '2' );
create table tablename2 like tablename1 ;
insert into tablename2   (select * from tablename1) ;

and it worked perfectly; you may need to check your syntax and try again - because this is how it should work
0
 
LVL 33

Assisted Solution

by:sajuks
sajuks earned 100 total points
ID: 17055996
you cannot have the indexes,pks and attributes like auto_increment, NOT NULL  wont be copied into the new table if u just use
create table tablename select  * from tablename2

you will have to do it in two steps
first create the table structure and then insert the data ...
CREATE TABLE table1  LIKE table2;
INSERT INTOtable1  SELECT * FROM 2;

0
 
LVL 8

Author Comment

by:thenone
ID: 17055998
so how would my sql statement be to create a table with the same data and structure.


by the way the table does have full text enabled
0
 
LVL 8

Author Comment

by:thenone
ID: 17056004
CREATE TABLE table1  LIKE table2;
INSERT INTOtable1  SELECT * FROM 2;

I have tried this but I think its because I have full text indexes it will not work.
0
 
LVL 30

Expert Comment

by:todd_farmer
ID: 17056005
It sounds like maybe you have a lot of data in table1?

An alternate way to do this is to use mysqldump to dump the table structure and contents of table1, then rename table1 to table2, then reload table1 from the .SQL file created by mysqldump.
0
 
LVL 8

Author Comment

by:thenone
ID: 17056006
I do have a lot of data hello Todd how is it going? I am trying to make this automated through a client application.
0
 
LVL 30

Accepted Solution

by:
todd_farmer earned 200 total points
ID: 17056011
Try this:

CREATE TABLE table1  LIKE table2;
ALTER TABLE table1 DISABLE KEYS;
INSERT INTO table1  SELECT * FROM 2;
ALTER TABLE table1 ENABLE KEYS;
0
 
LVL 8

Author Comment

by:thenone
ID: 17056013
ok will try one sec
0
 
LVL 8

Author Comment

by:thenone
ID: 17056041
it crashed the table
0
 
LVL 8

Author Comment

by:thenone
ID: 17056043
everything copied over crashed when I enabled keys
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:todd_farmer
ID: 17056053
Can you describe what error messages (if any) you received when it crashed?  Are you talking about the server or the app?
0
 
LVL 8

Author Comment

by:thenone
ID: 17056059
I do not recieve any error messages except when I execute the query it tels me how many rows are effected and then I check the connections on the server and it sais status for the query repair.
0
 
LVL 8

Author Comment

by:thenone
ID: 17056082
when I tried this before I think it was comlaining about the primary key numbers they are auto numbers but it should work.
0
 
LVL 30

Expert Comment

by:todd_farmer
ID: 17056104
OK, so it seems like you have a new table - with content - but no keys.  Is that correct?  What happens when you try to run the ALTER TABLE table1 ENABLE KEYS statement in the mysql command-line client (or phpMyAdmin or whatever you use)?  I'm wondering if we can get more information about the root cause of the problem from another client.
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17056108
in your second table make the column that is auto increment just a normal int column - and remove the primary key,

copy everything across;

then
add the auto_increment and the primary key

then run

update table table1 set auto_increment = (select max(keyID)  +1 from table 1)

0
 
LVL 8

Author Comment

by:thenone
ID: 17056122
couldn't i remove the column with the primary key and then just add it after i copy all of the data if so what is the alter command.
0
 
LVL 8

Author Comment

by:thenone
ID: 17056156
ok I think I have it Once I disable the keys and insert the data everything is fine it works beautiful it doesn't seem like I have to enable the keys.
0
 
LVL 8

Author Comment

by:thenone
ID: 17056362
ok I have to enable keys I do not know what I was thinking and when I do that the table crashes.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 17058889
What version of mysql do you have, thenone?  "SHOW VARIABLES LIKE 'version';"
0
 
LVL 8

Author Comment

by:thenone
ID: 17060261
I am using mysql version 5.20 which I believe is the latest one.I also believe that I ahve resoved my problem not unless someone has a better solution.

create table table1 like table2
alter table table1 disable keys
insert into table1 select * from table2
alter table table1 enable keys


I have aslo eliminated my auto increment primary keys which I didn't really need and this seemed to help.This process still takes alot of time though.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now