473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Restoring data from one table to the other.

Hello,
I have the a table (content) with some content and every time I edit it I
store all the table contents in an other table (content_bak) when I want to
restore the contents
How am i going to do that ? I am trying to do :

REPLACE INTO content SELECT bak_content_id, bak_content_title FROM
content_bak WHERE backup_id = '".$_GET['buckup_id']."'";

TABLE CONTENT
content_id int PK
content_title varchar

TABLE CONTENT_BAK
backup_id int PK
bak_content_id int
bak_content_title varchar

What I want to do is to REPLACE the TABLE CONTENT content_id = $x with
the TABLE CONTENT backup_id = $y AND bak_content_id =$x

Any ideas ?
Jul 23 '05 #1
2 1203
Angelos wrote:
Hello,
I have the a table (content) with some content and every time I edit it I
store all the table contents in an other table (content_bak) when I want to
restore the contents
How am i going to do that ? I am trying to do :

REPLACE INTO content SELECT bak_content_id, bak_content_title FROM
content_bak WHERE backup_id = '".$_GET['buckup_id']."'";

TABLE CONTENT
content_id int PK
content_title varchar

TABLE CONTENT_BAK
backup_id int PK
bak_content_id int
bak_content_title varchar
I assume you mean that the PK is a compound key: (backup_id,
bak_content_id).
What I want to do is to REPLACE the TABLE CONTENT content_id = $x with
the TABLE CONTENT backup_id = $y AND bak_content_id =$x


If you're certain that all content_id values you need to restore still
exist in the content table (that is, no records have been deleted and no
new records have been added since the backup was made), you could try a
multi-table UPDATE:

UPDATE content, content_bak
SET content.content_title = content_bak.bak_content_title
WHERE content.content_id = content_bak.bak_content_id
AND content_bak.backupd_id = '$y'

See http://dev.mysql.com/doc/mysql/en/update.html

If you can't make the assumptions about content_id values remaining the
same (which is probably a safe bet), you could use the REPLACE method.
You should first get rid of any rows that don't appear in the backup,
since I assume you don't want a mix of old record and newer records.

DELETE FROM content
USING content LEFT OUTER JOIN content_bak
ON content.content_id = content_bak.bak_content_id
AND content_bak.backup_id = '$y'
WHERE content_bak.backup_id IS NULL

Then do the REPLACE, and name your columns more explicitly to ensure
that the fields match up from the backup:

REPLACE INTO content (content_id, content_title)
SELECT bak_content_id, bak_content_title
FROM content_bak
WHERE backup_id = '$y'

Does this not work?

A third possibility is to empty the content table, and then just do a
simple INSERT from the backup table. That ensures no crumbs left over.

DELETE FROM content;
INSERT INTO content (content_id, content_title)
SELECT bak_content_id, bak_content_title
FROM content_bak
WHERE backup_id = '$y'

Regards,
Bill K.
Jul 23 '05 #2
> Regards,
Bill K.


Thanks a lot Bill !!!
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Raffi | last post by:
I use the --all-databases switch to backup my entire database. Sometimes there's a need to restore individual databases or tables form the backup file. What command should I use for this? ...
5
by: wanchan | last post by:
Dear all, Is it possible to restore some (not all) tablespaces to a new database? We have a full offline backup, and as the manual says, we can restore the whole set of tablespaces to a new...
2
by: p175 | last post by:
Hi folks, I've tried reading just about every post I can on raid stiping / extent size etc and I'm just getting myself more confused than ever. Here's my situation. On a Windows Server 2000...
2
by: Fischer Ulrich | last post by:
Hi I have a problem with the restoring of a database which uses tsearch2. I made a backup as discribed in 'tsearch-v2-intro' on the tsearch2 page. Now i'm trying to restore it into a...
5
by: Mike Nolan | last post by:
I have a 600K row table on my production system (running 7.3.3) that I dump and load on my development system (7.4.1) every night using cron jobs. I would like to be able to restore the table...
1
by: Mike Nolan | last post by:
I've probably asked this question before, but what do people do when they need to restore a table that has been dumped but as a different table name? That doesn't appear to be an option in...
3
by: rdemyan via AccessMonster.com | last post by:
I'm thinking about providing a relatively easy method for users to restore the front end from a backup. The purpose is to allow for restoring if the front end becomes corrupt. Here are some...
4
by: Troels Arvin | last post by:
Hello, I've run into situations where a table was accidentally dropped. The related database contained lots of other tables in lots of other schemas used by many different users. So I couldn't...
5
by: Troels Arvin | last post by:
Hello, Every so often, I'm asked to help people recover data from tables that were either dropped or where to much data was DELETEed. The complications related to restoring data are a problem....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.