I’m working within a SQL Server 2007 database engine. I’ve got a sequence of tables that store data about widgets and the companies that can order them. To enable a company to order a widget, I use a SQL script that takes a single manual entry for each value, inserts them into the various tables, then creates additional automatic entries depending on the number of business units within the company.
These scripts were written somewhere around the beginning of time by people whose names are long since lost to the annals of history. That or contractors; same basic thing. Because we’re a small organization, there have traditionally been only two people that do this process, and up to now, the SOP for screwups has essentially been “don’t”, because we don’t have a script to undo the creation process once the tran is committed.
Guess whose job it’s become to correct this.
The data in these tables are interrelated with various foreign keys (and columns that really should be fkeys but aren’t). To create the Universal Widget Deletion Script, I can trace the logic of the Create_Widget proc, collect the values that uniquely identify a specific range of entries (say, Widget #7 ordered by MegaCorp), query the relevant tables to find entries matching these values, and delete those rows.
Now, here’s the actual question. I can do that, OR I can create a script that deletes every row with a CreatedDt timestamp of the millisecond that the original entry was created. That’s the one value that all of these tables share, and per the results of my testing, it’s the same for all entries created at the same time (as it should be, since the creation script only sets the value for CreatedDt once).
The problem is, every programmer’s instinct that’s been trained into me is insisting that this is a bad idea. When that happens, it’s usually a sign that there’s something I’m missing. In this case, I think it’s a realization that there’s nothing inherently forcing the CreatedDt values to be A) identical between tables and B) unique within them; they’re not foreign keys and there are no other constraints involved. If that’s the only problem, it’s okay, because A) nobody manually adds entries without using the Create_Widget script, and B) there are only two people who do this, and we’re not both going to assign a widget at the same millisecond.
That said, I’ve been staring at db values and SQL scripts for too long, which does funny things to one’s judgement…that’s why I want to use you guys as a sanity check before I do this. As per the title, please tell me why this is a horrible idea.
Thanks!