In enterprise applications like ecommerce, health, banking domain applications, How do developers usually take care of PK?
For any type of registration like user registration, test-report, some entity, which strategy of PK generator do developers use? I'm sure they don't have to assign the id everytime, there must be some auto-generating strategy they would be following.
For some tables a simple sequential ID generator is all that is necessary. For others, one or more columns form a natural unique ID and no generator is required at all. Still others might want an (ugh!) UUID. This is likely if there are many disconnected data sources and no master generator available. There is no one-size-fits-all solution.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
Arun Singh Raaj
posted 1 month ago
For others, one or more columns form a natural unique ID and no generator is required at all.
Do you write the logic for this in Business layer? Could you please provide an example or link to explain how it works? How to make sure the generated id will be unique?
Natural unique ID means that the data already has something that can be used as an ID. That's why you don't need a generator..... You would have to use your own judgement to know if it will be unique. If you have a date that has digits all the way to milliseconds and then the origin you could use that as an ID because something isn't going to come from the same place at the same millisecond.
It's very common to just allow the database to generate the sequential ID which Tim mentioned. The database is in the business layer already, of course.
As for the case of a natural unique ID being represented by columns in the table, an example would be if a government agency used a Social Insurance Number as the primary key for a table containing taxpaying persons. There's no logic to be written for that, and how it works ought to be pretty obvious.
As for how to make sure that ID is unique... there are situations where people can fraudulently use somebody else's taxpayer ID. In this case it would be possible for duplicate ID's to occur. However the owners and designers of the system ought to be aware of that possibility in advance and design the system to deal with that situation. And note that this is not a "generated" ID, at least it's not generated by the application I have in mind. The ID comes from a source which is external to this application.