One example would be a NULL timestamp indicating what happened as "file_exported". Once a file has been exported and has a non-NULL value, it should never be set to NULL again.
Another example is the hit counter, where an integer is allowed to increase, but can never decrease.
In both cases, I simply did not record these changes as attributes in the annotated table; An โexportedโ or โhit counterโ is a great idea representing related but orthogonal real-world concepts from the objects to which they relate:
So they would just be a different relationship. Since we want file_exported to occur once:
CREATE TABLE thing_file_exported( thing_id INTEGER PRIMARY KEY REFERENCES(thing.id), file_name VARCHAR NOT NULL )
Hit counter is similar to another table:
CREATE TABLE thing_hits( thing_id INTEGER NOT NULL REFERENCES(thing.id), hit_date TIMESTAMP NOT NULL, PRIMARY KEY (thing_id, hit_date) )
And you can request using
SELECT thing.col1, thing.col2, tfe.file_name, count(th.thing_id) FROM thing LEFT OUTER JOIN thing_file_exported tfe ON (thing.id = tfe.thing_id) LEFT OUTER JOIN thing_hits th ON (thing.id = th.thing_id) GROUP BY thing.col1, thing.col2, tfe.file_name
source share