سؤال

في كثير من الأحيان في جدول لا يوجد لديه مفتاح طبيعي، فلا يزال مفيدا للمستخدمين ليكونوا قادرين على الحصول على معرف تم إنشاؤه بشكل فريد.إذا كان الجدول يحتوي على مفتاح أساسي بديل (وفي مثل هذه الحالة، فستتوقع ذلك بالتأكيد)، فيجب أن يتعرض هذا المفتاح للمستخدم أو يجب استخدام حقل آخر لهذا الغرض؟

سبب واحد لعدم عرض المفتاح البدلي هو أنه لا يمكنك الآن القيام بالعمليات التي تحافظ على العلاقة بين السجلات، ولكن تغيير القيم الرئيسية، مثل أنواع معينة من الحذف / إعادة الإدراج، العديد من طرق نسخ البيانات منقاعدة بيانات واحدة إلى أخرى، إلخ.

الميزة الرئيسية لفضح المفتاح البدلي هو بساطاة استخدام حقل لديك على أي حال.

تحت أي ظروف من الأفضل أن تعرض مباشرة مفتاح البديلة للمستخدمين؟

هل كانت مفيدة؟

المحلول

تحتاج إلى أن تكون جاهزا لأي معرف يتعرض للمستخدمين / العملاء الذين يحتاجون إلى تغيير، وتغيير هوية صف في قاعدة بيانات ونشر هذا التغيير إلى جميع المفاتيح الأجنبية يطلب فقط كسر البيانات.

إذا لم يكن لدى البيانات مفتاح عمل طبيعي، فيمكنك إضافة حقل إضافي ل "معرف أعمال". يجب تحسين هذا للعمليات التي يتم استخدامها ل. إن دخول لوحة المفاتيح الهاتف يعني رقمي فقط. عبر الهاتف / اللفظي يعني تجنب رموز السبر مماثلة (B / D، M / N، إلخ). يمكنك حتى الالتزاج بعض العبارة التي لا تنسى بسهولة ("Jelly Gelly").

تأثير ذلك هو أن العمل يمكن أن يغير لاحقا كيف يريدون الرجوع إلى السجلات، وتغيير مخطط البيانات الوحيد هو إما إضافة عمود جديد لهذا النمط بالمعرف أو تحويل المعرفات بالفعل. لا ينتشر التغيير من خلال قاعدة البيانات بأكملها، ولا يزال لديك معرف واحد (بديل) صالح بمرور الوقت.

باختصار، أود تجنب تعريض مفاتيح بديلة للمستخدمين. كما تشير التعليقات، يجب ألا تتغير مفاتيح البديلة أبدا. على العكس من ذلك، تريد الشركات تغيير كل شيء. إذا تعرض المفتاح البدلي، فهذا مجرد مسألة وقت قبل أن يريد العمل تغييره.

كملاحظة جانبية، عندما أقول "تعريض" هنا، أقصد إعطاء المفتاح للمستخدم مع التوقعات التي يستخدمها مباشرة (مثل الدعوة إلى الدعم مع رقم الطلب ).

نصائح أخرى

In some cases, surrogate keys are expected and make sense to users. My favorite example is "order number". Order number isn't really a natural key: a natural key might be timestamp plus user, or maybe more than that if you expect users to generate more than one order within the granularity of your timestamp.

Nonetheless, users understand and expect the convenience of an order number. There is no harm, and lots of value, if you let users know about them.

On the other hand, some surrogate keys make no sense to a user. Sure, my health insurance company has some surrogate key that identifies me based on my member id, date of birth, carrier, etc, but I don't care about that, I care about the info on my card (which often includes ids based on my employer and are not unique across the universe... Hence the surrogate key at the insurance company).

In layman's words:

  • Surrogates should be hidden from the user.
  • You should expose some other business candidate key to the user.
  • If no other candidate key exist you should show the PK. But in this case the PK is not considered a surrogate since it's not a substitute for other column.

you should ONLY expose a field to a user that provides useful information to the user, either directly or in reporting defects to you.

conversely, you should ALWAYS expose "surrogate primary keys" when they are the principal means of identifying a record (simple or complex) for an interaction the user performs.

You should only expose a surrogate key if it's a properly generated GUID/UUID*. Exposing sequential surrogate keys is number 4 on the OWASP Top 10 security issues.

* In practice, it's best to assume that it wasn't properly generated for these purposes unless you know that it was created by a cryptographically secure random or pseudo-random number generator.

If a table has no natural key, surrogate keys allow rows like this.

surrogate_key  some_name
--
1              Wibble
2              Wibble
...
17             Wibble
...
235            Wibble

I'd call these artificial keys instead of surrogate keys, but that distinction isn't important for this question.

Now, assuming that there's important data referencing these surrogate keys through foreign keys, how would the end users know which row to update if they don't know the surrogate key values?

It shouldn't matter whether you expose the keys or not to the end user. Your application should perform the necessary authorization such that simply knowing an order id, for example, can't allow them access to something they normally wouldn't have access to.

caveat: this assumes a web based or n-tier application where server side authorization is possible/feasable. If you have a VB app thats directly executing sql, thats a whole 'nother issue.

According to the principle of encapsulation, which is a foundational concept in OOP, you should hide implementation details. Once a surrogate becomes public it becomes data. One man's surrogate key is the next man's natural key.

In practice I think surrogate keys should be encapsulated at the boundary of a service layer. If you have a service for some aggregate root then the internal domain objects and repository would all use the surrogate key. These internals would all be encapsulated. The public API would use some other format which could include nesting to hide parent keys, so hiding the surrogate key of the root record isn't exactly a seven year leap.

One health-care company that I did a project for had a nasty problem with their "provider IDs." These were originally hand-managed and they contained embedded information. A single provider might have more than one ID, and had to know the "right" one to use at each clinic. And, unfortunately, some provider-IDs had been assigned to more than one provider at a time!

To solve the problem and to allow several different systems to communicate with each other unambiguously, I created an internal-only system of surrogate keys: they were simply random strings of letters that were the same length as the old (numeric) provider-ID strings. This was done so that column-sizes and data types didn't have to be changed.

One master system had to take a provider-ID, "figure out who it meant," and return the proper surrogate key, which always corresponded 1:1 to a single person.

Sometimes, that same system had to go the other way ... "which (legacy) provider-ID should we give them when referring to [this_person_surrogate] in [that_system]?"

Each of the downstream systems was now re-coded to use these unambiguous surrogate keys to refer to persons, knowing that every other system would be using only the same surrogate value.

Within each system's database, auto-increment integers were conveniently used as primary and foreign keys, and these keys were of course never shared with any other system. The record for each "person" contained its surrogate.

Any request that one system made to another system always used the surrogate. No system ever knew what any other system's "primary keys" were.

Carrying this concept one step further, surrogate keys were never published externally. The company eventually re-vamped its provider-ID system in an effort to clean up their mess, but when they did so we created a new provider-id "for public consumption," and tied those values to the surrogates just as we had done with the old IDs. Thus, the "purity" of the surrogates was not compromised, and the company didn't have to give its providers "ugly character strings."

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top