I know that some of you have worked with SQL, so maybe you can help me.
I'm designing an inventory database which will use barcodes to track items. First I'll use it to sell some junk on eBay, and after that, if it works well, I'll try to convince a local business owner to let me build a similar system for him.
My DB design uses a big pre-generated list of barcodes stored in a Barcode table. This table has no foreign key relationships with other tables. It exists to be a source of barcodes, and to record which of the barcodes have been used and which ones are free. It's intentionally non-relational.
This is a choice I made after going through several relational designs and finding easy ways to destroy all of them. Ultimately, it comes down to the fact that row IDs can change after a row is deleted (due to DB vacuuming/optimization), and therefore, the Barcode-Item pairing process must not involve row IDs. An ID-based pairing would eventually lead to barcode mismatches, destroying a big chunk of the barcode system like a game of Jenga. And if it can't involve row IDs, then it can't involve foreign key relationships because the only unique column in an Item is its row ID/primary key.
My solution: When a new Item is inserted, an SQL script will grab the next "unused" barcode, copy it to the new Item, and mark the barcode as "used" in the Barcode table. For safety, Barcodes will remain "used" after an Item is deleted, even if it is deleted immediately after creation. This will result in a growing block of "used" barcodes at the beginning of the Barcode table - one solid chunk, not scattered.
Assuming this is a good idea... what's the best way to get the next available Barcode? I guess I could count the number of used barcodes, or I could use a counter variable... the first idea is definitely bad, and the second feels like a sin against SQL... what do you think?