{"id":33,"date":"2003-02-22T11:14:19","date_gmt":"2003-02-22T01:14:19","guid":{"rendered":"http:\/\/www.thunderguy.com\/plusplus\/20050422\/don%e2%80%99t-use-automatically-generated-unique-ids\/"},"modified":"2010-09-07T23:19:49","modified_gmt":"2010-09-07T10:19:49","slug":"dont-use-automatically-generated-unique-ids","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2003\/02\/22\/dont-use-automatically-generated-unique-ids\/","title":{"rendered":"Don&#8217;t use automatically generated unique IDs"},"content":{"rendered":"<p>Think carefully before using an automatically-generated unique ID as a primary key in a SQL table. Using such automatically-generated IDs introduces extra implementation detail. This is a pain to manage, especially if they are used as primary keys or worse, foreign keys in tables.<\/p>\n<p>The best use for them is in a non-primary key field, with a uniqueness constraint on them. This allows quick and easy reference to a particular row. For example, you could retrieve rows with a complex query, keep the UIDs, and then use the UIDs to delete or update the rows. Note that such fields are generated automatically by some databases, like Oracle&#8217;s <code>ROWID<\/code> pseudocolumn.<\/p>\n<p>Using them as foreign keys means it is very likely they will leak out of the database layer into the business layer. This complicates the business logic with no benefit.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Think carefully before using an automatically-generated unique ID as a primary key in a SQL table. Using such automatically-generated IDs introduces extra implementation detail. This is a pain to manage, especially if they are used as primary keys or worse, foreign keys in tables. The best use for them is in a non-primary key field, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-general","tag-sql"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":1,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":458,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/33\/revisions\/458"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}