HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Want to add a "variable" to a table

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
wantvariabletableadd

Problem

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:

  • Create a table with the sole purpose of storing the date



  • 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.

Solution

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';

Code Snippets

CREATE TABLE dbo.EventLog
(
  EventDate SMALLDATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  [Description] NVARCHAR(2000)
);
INSERT dbo.EventLog([Description])
  SELECT N'Re-populated table x';
SELECT MAX(EventDate) FROM dbo.EventLog
  WHERE [Description] = N'Re-populated table x';
DELETE dbo.EventLog
  WHERE [Description] = N'Re-populated table x';

Context

StackExchange Database Administrators Q#12411, answer score: 4

Revisions (0)

No revisions yet.