copy data and structure to new table

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.
LVL 8
thenoneAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
todd_farmerConnect With a Mentor Commented:
Try this:

CREATE TABLE table1  LIKE table2;
ALTER TABLE table1 DISABLE KEYS;
INSERT INTO table1  SELECT * FROM 2;
ALTER TABLE table1 ENABLE KEYS;
0
 
Raynard7Connect With a Mentor Commented:
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
 
sajuksConnect With a Mentor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
thenoneAuthor Commented:
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
 
thenoneAuthor Commented:
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
 
todd_farmerCommented:
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
 
thenoneAuthor Commented:
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
 
thenoneAuthor Commented:
ok will try one sec
0
 
thenoneAuthor Commented:
it crashed the table
0
 
thenoneAuthor Commented:
everything copied over crashed when I enabled keys
0
 
todd_farmerCommented:
Can you describe what error messages (if any) you received when it crashed?  Are you talking about the server or the app?
0
 
thenoneAuthor Commented:
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
 
thenoneAuthor Commented:
when I tried this before I think it was comlaining about the primary key numbers they are auto numbers but it should work.
0
 
todd_farmerCommented:
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
 
Raynard7Commented:
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
 
thenoneAuthor Commented:
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
 
thenoneAuthor Commented:
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
 
thenoneAuthor Commented:
ok I have to enable keys I do not know what I was thinking and when I do that the table crashes.
0
 
NovaDenizenCommented:
What version of mysql do you have, thenone?  "SHOW VARIABLES LIKE 'version';"
0
 
thenoneAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.