4

I'm using PostgreSQL 8.4 and I have a table that is cleared out and refilled with new data every so often. I want to be able to store the date that the table was last filled, but I'm not sure where to put it. I'm really a novice at database design, and the only two things I can think of are:

  1. Create a table with the sole purpose of storing the date
  2. Timestamp every row in the table with the same timestamp

There's about 1000 to 2000 rows normally. It's just for a hobby project. Any help will be greatly appreciated.

1 Answer 1

4

Timestamping each row doesn't seem to make a lot of sense to me. I would just keep a log of the events that are interesting. Such a log could be reusable for other events too. E.g. (sorry this is SQL Server dialect, you may have to adjust slightly):

CREATE TABLE dbo.EventLog
(
  EventDate SMALLDATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  [Description] NVARCHAR(2000)
);

Whenever you reload the table, also call a stored procedure that does this:

INSERT dbo.EventLog([Description])
  SELECT N'Re-populated table x';

If you need to know the date of the last refresh, you can say:

SELECT MAX(EventDate) FROM dbo.EventLog
  WHERE [Description] = N'Re-populated table x';

If all you ever care about is the last time, then before the insert above, you could just:

DELETE dbo.EventLog
  WHERE [Description] = N'Re-populated table x';
3
  • This is a good idea. The database is very small (only 4 tables) but this makes sense in more than one way.
    – dreamlax
    Feb 7, 2012 at 20:22
  • Also instead of the delete/insert I suggested, in the case where you only care about the last time, you could simply update the row after you know it exists. But personally I think it's useful to keep a running history. It may also be useful to record how long the operation took, so you can see if data changes etc. over time are causing the operation to take longer... Feb 7, 2012 at 20:24
  • @AaronBetrand: Great idea. I need to read a decent book on database design I think, before I try anything a bit more serious than what I'm doing now.
    – dreamlax
    Feb 7, 2012 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.