Automate copying tables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HansieV
Forum Newbie
Posts: 4
Joined: Sat Jun 06, 2009 5:05 am

Automate copying tables?

Post by HansieV »

Hi all,
Is there a piece of code to perform following actions;
For several tables
-Copy table to <tablename_thisyear>
- Empty original table
Would be great... :lol:
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Automate copying tables?

Post by Darhazer »

You can use CREATA TABLE ... SELECT syntax to create a table from a SELECT Command
HansieV
Forum Newbie
Posts: 4
Joined: Sat Jun 06, 2009 5:05 am

Re: Automate copying tables?

Post by HansieV »

Thanks Darhazer for your quick reply..
Unfortunately I'm not that good in writing php..
That's why I asked if there is a piece of code someone has lying around..
Sorry for being a noob.. :oops:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Automate copying tables?

Post by Weirdan »

Darhazer wrote:You can use CREATA TABLE ... SELECT syntax to create a table from a SELECT Command
Unfortunately in MySQL it won't copy any indexes.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Automate copying tables?

Post by onion2k »

HansieV wrote:Thanks Darhazer for your quick reply..
Unfortunately I'm not that good in writing php..
That's why I asked if there is a piece of code someone has lying around..
Sorry for being a noob.. :oops:
Learn.

Or pay someone.
HansieV
Forum Newbie
Posts: 4
Joined: Sat Jun 06, 2009 5:05 am

Re: Automate copying tables?

Post by HansieV »

onion2k wrote: Learn.

Or pay someone.
What kind of an answer is that..? I thought a Forum is a place where people helped other people..
Of course I can pay and buy scripts, of course I can subscribe for a study and learn..
But for your info, I'm trying to help (for free!) some other people with their website, which is in php.
If you don't have anything constructive to say, I don't (and I guess a lot of other 'help requesters') appreciate your interferance with my post.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Automate copying tables?

Post by onion2k »

HansieV wrote:
onion2k wrote:Learn.

Or pay someone.
What kind of an answer is that..? I thought a Forum is a place where people helped other people..
This is a forum where people help each other ... to learn. Just giving someone a script doesn't help anyone learn, so we're against it.

If you want help writing a script to achieve want you want then we'll give whatever help we can, but if you're asking for a solution to just be given to you for free (which is what you are doing) we won't help.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Automate copying tables?

Post by Darhazer »

Weirdan wrote:
Darhazer wrote:You can use CREATA TABLE ... SELECT syntax to create a table from a SELECT Command
Unfortunately in MySQL it won't copy any indexes.
You are right, the better option is CREATE TABLE ... LIKE and then use INSERT INTO ... SELECT :)
On the other hand, it seems that the goal is to archive data, so creating the table with the same indexes may be not needed, and with the second option I'm afraid we can run in foreign key problems... never done it actually.

P.S.
HansieV may be you can just use a database management tool to perform this task?
HansieV
Forum Newbie
Posts: 4
Joined: Sat Jun 06, 2009 5:05 am

Re: Automate copying tables?

Post by HansieV »

Hi Darhazer,
I got the SQL queries running with Create Table, then truncate the original table.
What my original thought was, that I could create a button somewhere to perform these tasks with php.
Why? Like I said earlier, I am helping other people with their website, and want to make it as easy as possible for them to do this.
So maybe you can point me to a similair script or something.
Thanks for your assisting.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Automate copying tables?

Post by Darhazer »

HansieV wrote:Hi Darhazer,
I got the SQL queries running with Create Table, then truncate the original table.
What my original thought was, that I could create a button somewhere to perform these tasks with php.
Why? Like I said earlier, I am helping other people with their website, and want to make it as easy as possible for them to do this.
So maybe you can point me to a similair script or something.
Thanks for your assisting.
phpMyAdmin have this option. Just select the table, go to operations and choose copy table.
Post Reply