<p>Here we go again, the old argument still arises... </p>

<p>Would we better have a business key as a primary key, or would we rather have a surrogate id (i.e. an SQL Server identity) with a unique constraint on the business key field? </p>

<p>Please, provide examples or proof to support your theory.</p>

[{'answer_id': 63108, 'author': 'Iain Holder', 'author_id': 1122, 'author_profile': 'https://Stackoverflow.com/users/1122', 'pm_score': 4, 'selected': False, 'text': '<p>Alway use a key that has no business meaning.  It\'s just good practice.</p>\n\n<p>EDIT: I was trying to find a link to it online, but I couldn\'t.  However in <a href="http://martinfowler.com/books.html" rel="noreferrer">\'Patterns of Enterprise Archtecture\'</a> [Fowler] it has a good explanation of why you shouldn\'t use anything other than a key with no meaning other than being a key.  It boils down to the fact that it should have one job and one job only.</p>\n'}, {'answer_id': 63117, 'author': 'Rimantas', 'author_id': 6906, 'author_profile': 'https://Stackoverflow.com/users/6906', 'pm_score': 5, 'selected': False, 'text': '<p>Surrogate key will NEVER have a reason to change. I cannot say the same about the natural keys. Last names, emails, ISBN nubmers - they all can change one day.</p>\n'}, {'answer_id': 63140, 'author': 'Santiago Cepas', 'author_id': 6547, 'author_profile': 'https://Stackoverflow.com/users/6547', 'pm_score': 2, 'selected': False, 'text': "<p>On a datawarehouse scenario I believe is better to follow the surrogate key path. Two reasons:</p>\n\n<ul>\n<li>You are independent of the source system, and changes there --such as a data type change-- won't affect you.</li>\n<li>Your DW will need less physical space since you will use only integer data types for your surrogate keys. Also your indexes will work better.</li>\n</ul>\n"}, {'answer_id': 63144, 'author': 'Mark Embling', 'author_id': 6844, 'author_profile': 'https://Stackoverflow.com/users/6844', 'pm_score': 3, 'selected': False, 'text': "<p>Using a surrogate key is better in my opinion as there is zero chance of it changing. Almost anything I can think of which you might use as a natural key could change (disclaimer: not always true, but commonly). </p>\n\n<p>An example might be a DB of cars - on first glance, you might think that the licence plate could be used as the key. But these could be changed so that'd be a bad idea. You wouldnt really want to find that out <em>after</em> releasing the app, when someone comes to you wanting to know why they can't change their number plate to their shiny new personalised one.</p>\n"}, {'answer_id': 63159, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 5, 'selected': False, 'text': "<p>Surrogate keys (typically integers) have the added-value of making your table relations faster, and more economic in storage and update speed (even better, foreign keys do not need to be updated when using surrogate keys, in contrast with business key fields, that do change now and then).</p>\n\n<p>A table's primary key should be used for identifying uniquely the row, mainly for join purposes. Think a Persons table: names can change, and they're not guaranteed unique.</p>\n\n<p>Think Companies: you're a happy Merkin company doing business with other companies in Merkia. You are clever enough not to use the company name as the primary key, so you use Merkia's government's unique company ID in its entirety of 10 alphanumeric characters. \nThen Merkia changes the company IDs because they thought it would be a good idea. It's ok, you use your db engine's cascaded updates feature, for a change that shouldn't involve you in the first place. Later on, your business expands, and now you work with a company in Freedonia. Freedonian company id are up to 16 characters. You need to enlarge the company id primary key (also the foreign key fields in Orders, Issues, MoneyTransfers etc), adding a Country field in the primary key (also in the foreign keys). Ouch! Civil war in Freedonia, it's split in three countries. The country name of your associate should be changed to the new one; cascaded updates to the rescue. BTW, what's your primary key? (Country, CompanyID) or (CompanyID, Country)? The latter helps joins, the former avoids another index (or perhaps many, should you want your Orders grouped by country too).</p>\n\n<p>All these are not proof, but an indication that a surrogate key to uniquely identify a row for all uses, including join operations, is preferable to a business key.</p>\n"}, {'answer_id': 63189, 'author': 'Jay Shepherd', 'author_id': 7511, 'author_profile': 'https://Stackoverflow.com/users/7511', 'pm_score': 7, 'selected': False, 'text': '<p>Just a few reasons for using surrogate keys:</p>\n\n<ol>\n<li><p><strong>Stability</strong>: Changing a key because of a business or natural need will negatively affect related tables.  Surrogate keys rarely, if ever, need to be changed because there is no meaning tied to the value.</p></li>\n<li><p><strong>Convention</strong>: Allows you to have a standardized Primary Key column naming convention rather than having to think about how to join tables with various names for their PKs.</p></li>\n<li><p><strong>Speed</strong>: Depending on the PK value and type, a surrogate key of an integer may be smaller, faster to index and search.</p></li>\n</ol>\n'}, {'answer_id': 63287, 'author': 'Charles Graham', 'author_id': 7705, 'author_profile': 'https://Stackoverflow.com/users/7705', 'pm_score': 2, 'selected': False, 'text': "<p>This is one of those cases where a surrogate key pretty much <em>always</em> makes sense.  There are cases where you either choose what's best for the database or what's best for your object model, but in both cases, using a meaningless key or GUID is a better idea.  It makes indexing easier and faster, and it is an identity for your object that doesn't change.</p>\n"}, {'answer_id': 63325, 'author': 'derek lawless', 'author_id': 400464, 'author_profile': 'https://Stackoverflow.com/users/400464', 'pm_score': 3, 'selected': False, 'text': "<p>Surrogate keys are quite handy if you plan to use an ORM tool to handle/generate your data classes. While you can use composite keys with some of the more advanced mappers (read: hibernate), it adds some complexity to your code. </p>\n\n<p>(Of course, database purists will argue that even the notion of a surrogate key is an abomination.)</p>\n\n<p>I'm a fan of using uids for surrogate keys when suitable. The major win with them is that you know the key in advance e.g. you can create an instance of a class with the ID already set and guaranteed to be unique whereas with, say, an integer key you'll need to default to 0 or -1 and update to an appropriate value when you save/update.</p>\n\n<p>UIDs have penalties in terms of lookup and join speed though so it depends on the application in question as to whether they're desirable.</p>\n"}, {'answer_id': 63462, 'author': 'user7658', 'author_id': 7658, 'author_profile': 'https://Stackoverflow.com/users/7658', 'pm_score': 3, 'selected': False, 'text': "<p>Always use a single column, surrogate key if at all possible.  This makes joins as well as inserts/updates/deletes much cleaner because you're only responsible for tracking a single piece of information to maintain the record.</p>\n\n<p>Then, as needed, stack your business keys as unique contraints or indexes.  This will keep you data integrity intact.</p>\n\n<p>Business logic/natural keys can change, but the phisical key of a table should NEVER change.</p>\n"}, {'answer_id': 63566, 'author': 'Ted', 'author_id': 7972, 'author_profile': 'https://Stackoverflow.com/users/7972', 'pm_score': 8, 'selected': True, 'text': '<p>Both.  Have your cake and eat it.</p>\n\n<p>Remember there is nothing special about a primary key, except that it is labelled as such.  It is nothing more than a NOT NULL UNIQUE constraint, and a table can have more than one.</p>\n\n<p>If you use a surrogate key, you still want a business key to ensure uniqueness according to the business rules.</p>\n'}, {'answer_id': 541602, 'author': 'Tony Andrews', 'author_id': 18747, 'author_profile': 'https://Stackoverflow.com/users/18747', 'pm_score': 6, 'selected': False, 'text': '<p>It appears that no one has yet said anything in support of non-surrogate (I hesitate to say "natural") keys.  So here goes...</p>\n\n<p>A <strong>disadvantage</strong> of surrogate keys is that they are <strong>meaningless</strong> (cited as an advantage by some, but...).  This sometimes forces you to join a lot more tables into your query than should really be necessary.  Compare:</p>\n\n<pre><code>select sum(t.hours)\nfrom timesheets t\nwhere t.dept_code = \'HR\'\nand t.status = \'VALID\'\nand t.project_code = \'MYPROJECT\'\nand t.task = \'BUILD\';\n</code></pre>\n\n<p>against:</p>\n\n<pre><code>select sum(t.hours)\nfrom timesheets t\n     join departents d on d.dept_id = t.dept_id\n     join timesheet_statuses s on s.status_id = t.status_id\n     join projects p on p.project_id = t.project_id\n     join tasks k on k.task_id = t.task_id\nwhere d.dept_code = \'HR\'\nand s.status = \'VALID\'\nand p.project_code = \'MYPROJECT\'\nand k.task_code = \'BUILD\';\n</code></pre>\n\n<p>Unless anyone seriously thinks the following is a good idea?:</p>\n\n<pre><code>select sum(t.hours)\nfrom timesheets t\nwhere t.dept_id = 34394\nand t.status_id = 89    \nand t.project_id = 1253\nand t.task_id = 77;\n</code></pre>\n\n<p>"But" someone will say, "what happens when the code for MYPROJECT or VALID or HR changes?"  To which my answer would be: "why would you <strong>need</strong> to change it?"  These aren\'t "natural" keys in the sense that some outside body is going to legislate that henceforth \'VALID\' should be re-coded as \'GOOD\'.  Only a small percentage of "natural" keys really fall into that category - SSN and Zip code being the usual examples.  I would definitely use a meaningless numeric key for tables like Person, Address - but not for <strong>everything</strong>, which for some reason most people here seem to advocate.</p>\n\n<p>See also: <a href="https://stackoverflow.com/questions/337503/whats-the-best-practice-for-primary-keys-in-tables/337716#337716">my answer to another question</a></p>\n'}, {'answer_id': 541918, 'author': 'David Thornley', 'author_id': 14148, 'author_profile': 'https://Stackoverflow.com/users/14148', 'pm_score': 2, 'selected': False, 'text': '<p>Surrogate keys can be useful when business information can change or be identical.  Business names don\'t have to be unique across the country, after all.  Suppose you deal with two businesses named Smith Electronics, one in Kansas and one in Michigan.  You can distinguish them by address, but that\'ll change.  Even the state can change; what if Smith Electronics of Kansas City, Kansas moves across the river to Kansas City, Missouri?  There\'s no obvious way of keeping these businesses distinct with natural key information, so a surrogate key is very useful.</p>\n\n<p>Think of the surrogate key like an ISBN number.  Usually, you identify a book by title and author.  However, I\'ve got two books titled "Pearl Harbor" by H. P. Willmott, and they\'re definitely different books, not just different editions.  In a case like that, I could refer to the looks of the books, or the earlier versus the later, but it\'s just as well I have the ISBN to fall back on.</p>\n'}, {'answer_id': 621755, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>In the case of point in time database it is best to have combination of surrogate and natural keys. e.g. you need to track a member information for a club. Some attributes of a member never change. e.g Date of Birth but name can change.\nSo create a Member table with a member_id surrogate key and have a column for DOB.\nCreate another table called person name and have columns for member_id, member_fname, member_lname, date_updated. In this table the natural key would be member_id + date_updated.</p>\n'}, {'answer_id': 1488518, 'author': 'Ken', 'author_id': 180615, 'author_profile': 'https://Stackoverflow.com/users/180615', 'pm_score': 5, 'selected': False, 'text': '<p>I hate surrogate keys in general.  They should only be used when there is no quality natural key available.  It is rather absurd when you think about it, to think that adding meaningless data to your table could make things better.</p>\n\n<p>Here are my reasons: </p>\n\n<ol>\n<li><p>When using natural keys, tables are clustered in the way that they are most often searched thus making queries faster.</p></li>\n<li><p>When using surrogate keys you must add unique indexes on logical key columns.  You still need to prevent logical duplicate data.  For example, you can’t allow two Organizations with the same name in your Organization table even though the pk is a surrogate id column.</p></li>\n<li><p>When surrogate keys are used as the primary key it is much less clear what the natural primary keys are.  When developing you want to know what set of columns make the table unique.</p></li>\n<li><p>In one to many relationship chains, the logical key chains.  So for example, Organizations have many Accounts and Accounts have many Invoices.  So the logical-key of Organization is OrgName.  The logical-key of Accounts is OrgName, AccountID.  The logical-key of Invoice is OrgName, AccountID, InvoiceNumber.</p>\n\n<p>When surrogate keys are used, the key chains are truncated by only having a foreign key to the immediate parent.  For example, the Invoice table does not have an OrgName column. It only has a column for the AccountID.  If you want to search for invoices for a given organization, then you will need to join the Organization, Account, and Invoice tables.  If you use logical keys, then you could Query the Organization table directly.</p></li>\n<li><p>Storing surrogate key values of lookup tables causes tables to be filled with meaningless integers.  To view the data, complex views must be created that join to all of the lookup tables.  A lookup table is meant to hold a set of acceptable values for a column.  It should not be codified by storing an integer surrogate key instead.  There is nothing in the normalization rules that suggest that you should store a surrogate integer instead of the value itself.</p></li>\n<li><p>I have three different database books.  Not one of them shows using surrogate keys.</p></li>\n</ol>\n'}, {'answer_id': 9220187, 'author': 'WillC', 'author_id': 1200764, 'author_profile': 'https://Stackoverflow.com/users/1200764', 'pm_score': 1, 'selected': False, 'text': '<p>Horse for courses. To state my bias; I\'m a developer first, so I\'m mainly concerned with giving the users a working application. </p>\n\n<p>I\'ve worked on systems with natural keys, and had to spend a lot of time making sure that value changes would ripple through. </p>\n\n<p>I\'ve worked on systems with only surrogate keys, and the only drawback has been a lack of denormalised data for partitioning.</p>\n\n<p>Most traditional PL/SQL developers I have worked with didn\'t like surrogate keys because of the number of tables per join, but our test and production databases never raised a sweat; the extra joins didn\'t affect the application performance. With database dialects that don\'t support clauses like "X inner join Y on X.a = Y.b", or developers who don\'t use that syntax, the extra joins for surrogate keys do make the queries harder to read, and longer to type and check: see @Tony Andrews post. But if you use an ORM or any other SQL-generation framework you won\'t notice it. Touch-typing also mitigates.</p>\n'}, {'answer_id': 11228837, 'author': 'Bryan', 'author_id': 965283, 'author_profile': 'https://Stackoverflow.com/users/965283', 'pm_score': 2, 'selected': False, 'text': '<p>As a reminder it is not good practice to place clustered indices on random surrogate keys i.e. GUIDs that read XY8D7-DFD8S, as they SQL Server has no ability to physically sort these data. You should instead place unique indices on these data, though it may be also beneficial to simply run SQL profiler for the main table operations and then place those data into the Database Engine Tuning Advisor.</p>\n\n<p>See thread @ <a href="http://social.msdn.microsoft.com/Forums/en-us/sqlgetstarted/thread/27bd9c77-ec31-44f1-ab7f-bd2cb13129be" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-us/sqlgetstarted/thread/27bd9c77-ec31-44f1-ab7f-bd2cb13129be</a> </p>\n'}, {'answer_id': 13049552, 'author': 'Stefanos Kargas', 'author_id': 350061, 'author_profile': 'https://Stackoverflow.com/users/350061', 'pm_score': 3, 'selected': False, 'text': "<p><em><strong>Case 1:</strong></em> Your table is a <strong>lookup table</strong> with less than 50 records (50 types)</p>\n<p>In this case, use <strong>manually named keys, according to the meaning of each record</strong>.</p>\n<p>For Example:</p>\n<pre><code>Table: JOB with 50 records\nCODE (primary key)       NAME               DESCRIPTION\nPRG                      PROGRAMMER         A programmer is writing code\nMNG                      MANAGER            A manager is doing whatever\nCLN                      CLEANER            A cleaner cleans\n...............\njoined with\nTable: PEOPLE with 100000 inserts\n\nforeign key JOBCODE in table PEOPLE\nlooks at\nprimary key CODE in table JOB\n</code></pre>\n<hr />\n<p><em><strong>Case 2:</strong></em> Your table is a <strong>table with thousands of records</strong></p>\n<p>Use <strong>surrogate/autoincrement keys</strong>.</p>\n<p>For Example:</p>\n<pre><code>Table: ASSIGNMENT with 1000000 records\njoined with\nTable: PEOPLE with 100000 records\n\nforeign key PEOPLEID in table ASSIGNMENT\nlooks at\nprimary key ID in table PEOPLE (autoincrement)\n</code></pre>\n<hr />\n<p><strong>In the first case:</strong></p>\n<ul>\n<li>You can select all programmers in table <code>PEOPLE</code> without use of join with table <code>JOB</code>, but just with: <code>SELECT * FROM PEOPLE WHERE JOBCODE = 'PRG'</code></li>\n</ul>\n<p><strong>In the second case:</strong></p>\n<ul>\n<li>Your database queries are faster because your primary key is an integer</li>\n<li>You don't need to bother yourself with finding the next unique key because the database itself gives you the next autoincrement.</li>\n</ul>\n"}, {'answer_id': 16195443, 'author': 'lrb', 'author_id': 2316169, 'author_profile': 'https://Stackoverflow.com/users/2316169', 'pm_score': 1, 'selected': False, 'text': "<p>Maybe not completely relevant to this topic, but a headache I have dealing with surrogate keys. Oracle pre-delivered analytics creates auto-generated SKs on all of its dimension tables in the warehouse, and it also stores those on the facts. So, anytime they (dimensions) need to be reloaded as new columns are added or need to be populated for all items in the dimension, the SKs assigned during the update makes the SKs out of sync with the original values stored to the fact, forcing a complete reload of all fact tables that join to it. I would prefer that even if the SK was a meaningless number, there would be some way that it could not change for original/old records. As many know, out-of-the box rarely serves an organization's needs, and we have to customize constantly. We now have 3yrs worth of data in our warehouse, and complete reloads from the Oracle Financial systems are very large. So in my case, they are not generated from data entry, but added in a warehouse to help reporting performance. I get it, but ours do change, and it's a nightmare.</p>\n"}, {'answer_id': 20803912, 'author': 'mwnsiri', 'author_id': 3094941, 'author_profile': 'https://Stackoverflow.com/users/3094941', 'pm_score': 5, 'selected': False, 'text': '<p>I want to share my experience with you on this endless war :D on natural vs surrogate key dilemma. I think that <strong>both</strong> surrogate keys (artificial auto-generated ones) and natural keys (composed of column(s) with domain meaning) have <strong>pros</strong> and <strong>cons</strong>. So depending on your situation, it might be more relevant to choose one method or the other. <br/> </p>\n\n<p>As it seems that many people present surrogate keys as the almost perfect solution and natural keys as the plague, I will focus on the other point of view\'s arguments:</p>\n\n<h1>Disadvantages of surrogate keys</h1>\n\n<p>Surrogate keys are:</p>\n\n<ol>\n<li>Source of performance problems:\n\n<ul>\n<li>They are usually implemented using auto-incremented columns which mean:\n\n<ul>\n<li>A round-trip to the database each time you want to get a new Id (I know that this can be improved using caching or [seq]hilo alike algorithms but still those methods have their own drawbacks).</li>\n<li>If one-day you need to move your data from one schema to another (It happens quite regularly in my company at least) then you might encounter Id collision problems. And Yes I know that you can use UUIDs but those lasts requires 32 hexadecimal digits! (If you care about database size then it can be an issue).</li>\n<li>If you are using one sequence for all your surrogate keys then - for sure - you will end up with contention on your database.</li>\n</ul></li>\n</ul></li>\n<li>Error prone. A sequence has a max_value limit so - as a developer - you have to put attention to the following points:\n\n<ul>\n<li>You must cycle your sequence ( when the max-value is reached it goes back to 1,2,...).</li>\n<li>If you are using the sequence as an ordering (over time) of your data then you must handle the case of cycling (column with Id 1 might be newer than row with Id max-value - 1).</li>\n<li>Make sure that your code (and even your client interfaces which should not happen as it supposed to be an internal Id) supports 32b/64b integers that you used to store your sequence values.  </li>\n</ul></li>\n<li>They don\'t guarantee non duplicated data. You can always have 2 rows with all the same column values but with a different generated value. For me this is <strong>THE</strong> problem of surrogate keys from a database design point of view.</li>\n<li><a href="http://en.wikipedia.org/wiki/Surrogate_key" rel="noreferrer" title="Disadvantages section">More in Wikipedia...</a></li>\n</ol>\n\n<h1>Myths on natural keys</h1>\n\n<ol>\n<li>Composite keys are less inefficient than surrogate keys. No! It depends on the used database engine:\n\n<ul>\n<li><a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:6070436300346195032" rel="noreferrer" title="askTom: Natural Key as Primary Key Vs Surrogate Key">Oracle</a></li>\n<li><a href="https://stackoverflow.com/questions/1460465/composite-primary-key-performance-drawback-in-mysql">MySQL</a></li>\n</ul></li>\n<li>Natural keys don\'t exist in real-life. Sorry but they do exist! In aviation industry, for example, the following tuple will be always unique regarding a given <strong>scheduled</strong> flight (airline, departureDate, flightNumber, operationalSuffix). More generally, when a set of business data is guaranteed to be unique by a given <strong>standard</strong> then this set of data is a [good] natural key candidate.</li>\n<li>Natural keys "pollute the schema" of child tables. For me this is more a feeling than a real problem. Having a 4 columns primary-key of 2 bytes each might be more efficient than a single column of 11 bytes. Besides, the 4 columns can be used to query the child table directly (by using the 4 columns in a where clause) without joining to the parent table.  </li>\n</ol>\n\n<h1>Conclusion</h1>\n\n<p>Use natural keys when it is relevant to do so and use surrogate keys when it is better to use them.</p>\n\n<p>Hope that this helped someone!</p>\n'}]
['https://Stackoverflow.com/questions/63090', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4690/']