473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to "undo" a database delete

Hello everybody,

I have an intranet application which is mostly a bunch of data editing
forms.

The database has about 20 tables all related together.

Recently somebody deleted a "the wrong record" and the cascade delete
in sql server did its work nicely :(

When they asked me to put the data back, I looked pretty stupid
as I had no idea how.

How is this normally done in ASP.NET app?

Morgan

Nov 19 '05
13 2383
Hi Morgan,

I have recently been trying to solve this problem too.

(I am assuming you are using SQL Server)

This is what I came up with:

Using "IsDeleted" type approach is OK but try and use a timestamp
so null = not deleted, not null = time of deletion
this enables you to purge records that really should be deleted
as they are only taking up space

the biggest problem with this approach is it doesn't work very well
for more complex schemas with cascade deleting etc.

it can work but it gets complicated very quickly

to solve this you can combine a table which logs the deletes with a
timestamp
a trigger on the tables you want to protect
and then a script that does a point in time restore of the database (to
a new database)
and then simply copies the records back

here is some example code:

--example table

create table LogDeletions (
LogID int primary key identity
, LogDate datetime not null default getdate()
, TableName varchar(255) not null
, RecordID int not null
)

-- example trigger

create trigger trAuditLogDelet e
on auditlog
for delete
as
--
insert into LogDeletions (LogDate, TableName, RecordID)
select getdate(), 'AuditLog', AuditLogID
from deleted
--(end)

-- example point in time restore

drop database Recovery
go
restore database Recovery
from disk = 'filename of last full backup', norecovery
go
restore log Recovery
from disk = 'filename of suitable log file', stopat = '2005-05-05
12:01:32.435'
go
--example insert of related records

select primarykey into #temp1
from firstdependentt able where maintable.forei gnkey = the one deleted
from the maintable
insert into originaldatabas e.dbo.seconddep *endenttable
select *
from seconddependent table
where firstdependentt able.foreignkey in (select primarykey from #temp1)

insert into originaldatabas e.dbo.firstdepe *ndenttable
select *
from firstdependentt able
where primarykey in (select primarykey from #temp1)
insert into originaldatabas e.dbo.maintable
select *
from maintable
where primarykey = the one deleted from the maintable

this isn't a fully automatic restore solution
but more of an accelerated disaster recovery
solution

i hope it helps,

it works fine for me

and, except for the triggers is "non-invasive"
ie: i didn't have to rewrite 100 queries etc.

All the best,

John Rivers

Nov 19 '05 #11
The most unobtrusive way I can come up with is the use a tool that scans
the transaction log and can selectively rollback certain transactions.
This requires no architectural changes.

There is actually a faq about it:

http://www.aspfaq.com/show.asp?id=2449

These are the products that get mentioned in the article:

Apex SQL Log

Log Explorer

Log P.I.

SQL Log Rescue

Shortcomings:
- No web interface to let savvy site users do a rollback
Cheers,
Tom Pester
Hi Morgan,

I have recently been trying to solve this problem too.

(I am assuming you are using SQL Server)

This is what I came up with:

Using "IsDeleted" type approach is OK but try and use a timestamp
so null = not deleted, not null = time of deletion
this enables you to purge records that really should be deleted
as they are only taking up space
the biggest problem with this approach is it doesn't work very well
for more complex schemas with cascade deleting etc.

it can work but it gets complicated very quickly

to solve this you can combine a table which logs the deletes with a
timestamp
a trigger on the tables you want to protect
and then a script that does a point in time restore of the database
(to
a new database)
and then simply copies the records back
here is some example code:

--example table

create table LogDeletions (
LogID int primary key identity
, LogDate datetime not null default getdate()
, TableName varchar(255) not null
, RecordID int not null
)
--example insert of related records

select primarykey into #temp1
from firstdependentt able where maintable.forei gnkey = the one deleted
from the maintable
insert into originaldatabas e.dbo.seconddep *endenttable
select *
from seconddependent table
where firstdependentt able.foreignkey in (select primarykey from
#temp1)
insert into originaldatabas e.dbo.firstdepe *ndenttable
select *
from firstdependentt able
where primarykey in (select primarykey from #temp1)
insert into originaldatabas e.dbo.maintable
select *
from maintable
where primarykey = the one deleted from the maintable
this isn't a fully automatic restore solution
but more of an accelerated disaster recovery
solution
i hope it helps,

it works fine for me

and, except for the triggers is "non-invasive"
ie: i didn't have to rewrite 100 queries etc.
All the best,

John Rivers

Nov 19 '05 #12
I checked out Log Explorer once

it was very slick but quite expensive

i feel for all the people who paid
for such software unnecessarily
as i almost did.

The author of the article you mentioned
also missed the fact that you can
backup a database after you mess it up
and still restore to a point in time before the backup.

Of course Lumigent will never admit that if you ring them - they
totally lied to me
on the phone - you would think a case of mushroom management of staff
(keep them in the dark and feed them sh*t) but i could hear they were
lying so
I researched further and solved my problem without it - great product
but don't
be tricked into buying it unnecessarily - knowledge is power :-)

Nov 19 '05 #13
For SQL Server I would suggest creating z-Tables. Anytime a record is
deleted (and I would also suggest for updated) just insert a record into
your backup table. If you doing eveything through stored procs you can do a
simple insert, if your doing direct table access I would suggest using
triggers to do the insert.

"Morgan Bachu" <ds********@tis cali.co.uk> wrote in
news:11******** **************@ g14g2000cwa.goo glegroups.com:
Hello everybody,

I have an intranet application which is mostly a bunch of data editing
forms.

The database has about 20 tables all related together.

Recently somebody deleted a "the wrong record" and the cascade delete
in sql server did its work nicely :(

When they asked me to put the data back, I looked pretty stupid
as I had no idea how.

How is this normally done in ASP.NET app?

Morgan


Nov 19 '05 #14

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

Similar topics

2
3260
by: dsfsdfs878 | last post by:
Hello, Somebody accidently deleted a record with cascade deletes and took out about 80 records. The database has been used heavily since then. How do I put back the data? I have spent hours researching BOL and all I can find
14
1662
by: ss.wisch | last post by:
Ok here's the thing... I have a vBulletin forum database... there is a table in there called "post" which has all of my forums posts in it. What I have are many posts with regular titles like "Oh hello how are you" or whatever, and many posts (which are replies) with "Re:" before them and whatever message following that. I have recently updated my forums not to show the Re: thing, and I need a quick way to execute a query or something...
3
4618
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that the user has to use my New Record button. When you click the New Record button, the form presents a new record for editing. My client wants to use the Escape key to cancel changes to new or existing records. On existing records, Access already...
3
11630
by: babylon | last post by:
any facilities in csharp that can help me implmenting undo/redo in my application? thx
3
4555
by: Carpe Diem | last post by:
Hello I have an aspx page that loses Session("user") value after a few minutes even after I set <sessionState mode="InProc" cookieless="false" timeout="300"> in web.config and wrote function Session_Start() { Session.Timeout = 3000; } in global.asax
2
2779
by: monkey3333sg | last post by:
I would like to add an "undo" button in my programme
0
1256
by: monkey3333sg | last post by:
I am programming some flow chart program using VB6... which is similar to Visual Logic software... I would like to add in "Undo" and "Redo" part...anyone can help? Thank you.
7
16353
by: PW | last post by:
Hi, I have a form with unbound fields on it. The user selects a record from a recordset and I populate the unbound fields. When I try to change the unbound quantity text box, Access 2003 tells me "The data has been changed. Another user edited this record and saved the changes before you attempted to save your changes. Re-edit the record." This does not always happen. And yes, I can re-edit the quantity text
1
1177
by: Nilam2477 | last post by:
I have a form with multiple TextBox controls and few other controls like comboBox, checkBox etc. I'm trying to implement Undo functionality for all the controls in the form. I'm using Push & Pop operations of Stack to store the control values. My question is regarding the TextBox control. I would like to know on which event of TextBox control i can save the Text in the stack. If i use TextChanged event, then this event is triggered on each...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.