databaseprimary-keyurl-schemeapplication-design

Is it insecure to reveal a row's primary key to the user?


Why do many applications replace the primary key of a database with a seemingly random alternative id when revealing the record to the user?

My guess is that it prevents users from guessing other rows in the table. If so, isn't that just false sense of security?


Solution

  • I guess you are talking about surrogate keys here. One of the desired or supposed advantages of surrogate keys is that they aren't burdened by any external meaning or dependency on anything outside the database. So for example the surrogate key values can safely be reassigned or the key can be refactored or discarded without any consequences for users of the system.

    Generally surrogate keys are kept hidden from users so that they don't acquire any such external dependencies. Being hidden from users was in fact part of the original definition of a surrogate key as proposed by E.F.Codd. If key values reside in the user's browser cache or favourites list then they aren't much use as "surrogates" any more. So that's one common reason why you will see one key used only inside the database and a different key for the same table made visible in the application.