开发者

Delete data from table without date field

I am trying to d开发者_如何学JAVAelete the data from table older than 6 months where there is no date field. Please let me know.


Without a date field this isn't possible.

However, if you have a backup from 6 months ago, you could delete all data from the table that was in that backup.


You could calculate the average of how much data per month is generated. * 6 will give you the number of rows to keep. So you could take the current max ID, less n rows. If your table does not have an ID, you can use an artifical one like this:

SELECT  *, n = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
FROM    your_table_name


Without a date column, you cannot know which rows are older than six months. This is especially a problem if the key column is not a monotonically increasing value (i.e., not a simple integer sequence).

However, if you have backups of the database, you might possibly generate a list of rows in the six-month-old backup dB and compare it to the rows in the current dB, and then deduce which rows were added after the backup was made. You know then that the remaining rows are at least six months old.

Suggestion #2

If the row's key column is a monotonically increasing value (such as an integer sequence, or uniquely increasing account ID), then you could insert a special sentinel record every month (or week, or day, whatever) with special values in it that tell you that it's a marker row (and not normal data). You then know that every row with a lower key field value than the sentinel's key value was inserted before the sentinel.

Then every month (week/day/whatever), finding those older rows and deleting them is fairly simple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜