1import"./CWj6FrbW.js";import"./69_IOA4Y.js";import{s as p,f as c,c as e,r as t,b as r}from"./BZ3wHDxR.js";import{t as m,a as l}from"./B0yhBc00.js";import{h}from"./CY_rZqSt.js";const y={title:"Composite Primary Keys in SQLite",date:"2023-07-15",updatedAt:"2023-07-15",category:"Basics",headings:[{level:2,title:"What is a Composite Primary Key?"},{level:2,title:"How to Create a Composite Primary Key in SQLite"}]},{title:f,date:v,updatedAt:x,category:C,headings:E}=y;var b=m('<h2 id="what-is-a-composite-primary-key"><a aria-hidden="true" tabindex="-1" href="#what-is-a-composite-primary-key"><span class="icon icon-link"></span></a>What is a Composite Primary Key?</h2> <p>A composite primary key, also known as a compound key or concatenated key, is a type of primary key that is made up of two or more columns in a database table. These columns, when combined, ensure the uniqueness of each record in the table.</p> <p>Consider the following example, where each team can have multiple persons as members. Each person can belong to multiple teams, but can only once belong to the same team.</p> <table><thead><tr><th>team_id</th><th>person_id</th></tr></thead><tbody><tr><td>1</td><td>100</td></tr><tr><td>1</td><td>101</td></tr><tr><td>2</td><td>100</td></tr><tr><td>2</td><td>102</td></tr></tbody></table> <p>In this example, the person with <code>person_id</code> 100 is part of both team 1 and team 2. Despite the <code>person_id</code> being the same in multiple entries, there is no conflict because the composite primary key (<code>team_id</code> and <code>person_id</code> together) is unique in each case.</p> <h2 id="how-to-create-a-composite-primary-key-in-sqlite"><a aria-hidden="true" tabindex="-1" href="#how-to-create-a-composite-primary-key-in-sqlite"><span class="icon icon-link"></span></a>How to Create a Composite Primary Key in SQLite</h2> <p>Hereâs an example on how to create a table with a composite primary key in SQLite:</p> <div class="code-block -mt-[1em] relative"><pre class="!mb-0 border border-gray-200 bg-white !p-0 not-prose"><code class="hljs rounded-lg !px-4 !py-[1.1rem] text-gray-900 text-sm not-prose"><!></code></pre><button type="button" class="copy-code-button absolute top-0 right-0 self-start hover:text-gray-900 p-1.5 rounded-md text-gray-600"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"></path></svg><span class="sr-only">Copy code</span></button></div> <p>In this <code>team_members</code> table, the primary key is composed of both <code>team_id</code> and <code>person_id</code>. This implies that the combination of <code>team_id</code> and <code>person_id</code> must be unique across all rows in the table, ensuring that a person can only belong to a particular team once.</p>',1);
1function A(n){var a=b(),o=p(c(a),14),s=e(o),i=e(s),d=e(i);h(d,()=>`<span class="hljs-keyword">CREATE TABLE</span> team_members ( 2 team_id <span class="hljs-type">INTEGER</span>, 3 person_id <span class="hljs-type">INTEGER</span>, 4 <span class="hljs-keyword">PRIMARY KEY</span> (team_id, person_id) 5);`),t(i),t(s),r(),t(o),r(2),l(n,a)}export{A as default,y as metadata};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.