Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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';
share|improve this answer
    
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 '12 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... –  Aaron Bertrand Feb 7 '12 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 '12 at 20:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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