MARCO KIESEWETTER, MBA
SQL: Unique IDs, Primary
Keys and archiving
inactive rows without
violating constraints.
• We want to add a constraint to an ID column - to serve as
a unique Primary Key.
• This by itself is no problem.
• We also want to archive any change to the table by not
deleting or overwriting data but by keeping it, flagged as
inactive. This leads to several rows with the same ID value,
which the constraint tries to prevent.
• The constraint needs to function but only on active rows,
not on archived (‘deleted’) rows.
• This slide deck shows a simple solution how to achieve this.
The Problem
Example Table
Example Table (Continued)
• Here the logic breaks because xID is no longer unique.
• SQL will return an error:
• Violation of PRIMARY KEY constraint 'constraint1'. Cannot insert
duplicate key in object 'dbo.__TestDeleteFlag'. The duplicate key
value is (1).
• Adding xDeleteFlag to the constraint only works for the first
edit but not for subsequent edits/deletes since the second
archived row will already share the same ID (‘1’) & Flag
(‘1’/TRUE) combination.
Constraint Violation
• We do not use a BIT or INT value as our delete / archived
flag but we use a DATETIME column as the flag.
• The new ‘Deleted’ column will hold the date and time the
row was deleted/archived. This can be achieved with
GETDATE() and therefore should be different for each
‘deletion’. It also conveniently tracks different versions of
the row over time.
• The one and only active row will use DATETIME value ‘0’ (or
‘01/01/1900 12:00:00am’)
• Now we add this column to the constraint.
Solution
Solution: Example Table
Solution: Example Table (Continued)
Solution: Example Table (Continued)
• The table will work as expected. For normal day-to-day
business we will filter out the inactive/archived rows:
Solution: Example Table (Continued)
• Testing the constraint shows that xID will be forced unique
for what we call ‘active’ rows. As we would expect, we
cannot add a second active row with xID = 1:
Solution: Example Table (Continued)
QUESTIONS?
HTTPS://WWW.LINKEDIN.COM/IN/MARCOKIESEWETTER
Thank You

SQL: Unique IDs, Primary Keys and Archiving Inactive Rows Without Violating Constraints

  • 1.
    MARCO KIESEWETTER, MBA SQL:Unique IDs, Primary Keys and archiving inactive rows without violating constraints.
  • 2.
    • We wantto add a constraint to an ID column - to serve as a unique Primary Key. • This by itself is no problem. • We also want to archive any change to the table by not deleting or overwriting data but by keeping it, flagged as inactive. This leads to several rows with the same ID value, which the constraint tries to prevent. • The constraint needs to function but only on active rows, not on archived (‘deleted’) rows. • This slide deck shows a simple solution how to achieve this. The Problem
  • 3.
  • 4.
  • 5.
    • Here thelogic breaks because xID is no longer unique. • SQL will return an error: • Violation of PRIMARY KEY constraint 'constraint1'. Cannot insert duplicate key in object 'dbo.__TestDeleteFlag'. The duplicate key value is (1). • Adding xDeleteFlag to the constraint only works for the first edit but not for subsequent edits/deletes since the second archived row will already share the same ID (‘1’) & Flag (‘1’/TRUE) combination. Constraint Violation
  • 6.
    • We donot use a BIT or INT value as our delete / archived flag but we use a DATETIME column as the flag. • The new ‘Deleted’ column will hold the date and time the row was deleted/archived. This can be achieved with GETDATE() and therefore should be different for each ‘deletion’. It also conveniently tracks different versions of the row over time. • The one and only active row will use DATETIME value ‘0’ (or ‘01/01/1900 12:00:00am’) • Now we add this column to the constraint. Solution
  • 7.
  • 8.
  • 9.
  • 10.
    • The tablewill work as expected. For normal day-to-day business we will filter out the inactive/archived rows: Solution: Example Table (Continued)
  • 11.
    • Testing theconstraint shows that xID will be forced unique for what we call ‘active’ rows. As we would expect, we cannot add a second active row with xID = 1: Solution: Example Table (Continued)
  • 12.