sqlshots: Identity Columns
There are a lot of sites out there that give an in-depth definition of identity columns (this ain't one of them), but I have to admit that it was something I took for granted recently. In my task to clean up geospatial address data, specifically for geo-coding purposes I over looked the goodness of having an identity field as part of table. The data that I received did not have an ID field and for some odd reason I thought nothing of. I figured I would make updates by comparing multiple columns to target specific rows. Well that worked fine until I realized that I had duplicate data. Digging myself deeper into a whole I kept doing things the hard way, well let's just say I did things the less efficient way.
Once I finally realized what I had been doing was inefficient I decided to introduce an ID field to the table. However old habits are hard to kill. Typically when I create ID fields I always leave (identity) off because I use UDFs to generate my sequential numbering. Reason being if there is ever a need to remove a record or delete a record (which seldom occurs since I use bit fields to designate active versus inactive records) the numbering continues. So if I have rows one through five and physically delete row five (ID 5), my next insert will become ID 6. Making the sequence 1,2,3,4,6.
Some may disagree but I really don't like that and no I am not OCD. It's just that I hate wasting IDs and avoid it whenever possible, hence my dilemma. So I add the column via the UI and save my changes. Then I start to think what is the best way to populate this newly added column. Not seeing the obvious (big rookie mistake on my part) I must have wasted 30 mins to an hour before I stepped away to grab a drink. Then all of a sudden it occurred to me that I could drop the column and re-add it via alter statement and all my worries would be gone. Which I did. You can do the same using the UI.


One simple single line could have prevented a self inflicted heartache because I was being an idiot. Once I ran it, the row was created and populated in sequential order. Just goes to show sometimes you need to step away from the task at hand to see what you are doing wrong! Granted I only needed the column temporarily; nonetheless, it helped a great deal when running bulk updates and having to compare a single column versus five to six. Especially when dealing with hundreds of thousands of records.
ALTER STATEMENT:
ALTER TABLE TableName ADD ID INT IDENTITY(1,1)
Just to give you a visual of what exactly transpired here is little step by step (example data only):
BEFORE AND AFTER

1) SELECT * FROM TestTable

2) ALTER TABLE TestTable ADD ID INT IDENTITY(1,1)

3) SELECT * FROM TestTable
