Primary key

기본 키

A primary key uniquely identifies each row in a table.

···
html
<div class="demo"><div class="head"><b>PRIMARY KEY (id)</b><span>users</span></div><div class="stage pk"><div class="existing"><div class="cell">1 · Ana</div><div class="cell">3 · Bo</div></div><div class="arrow">←</div><div class="cell incoming" id="incoming">3 · Cy</div></div><div class="foot"><span id="verdict">duplicate id: rejected</span><span>unique + not null</span></div></div>
css
*{box-sizing:border-box}.demo{width:min(96vw,820px);height:min(94vh,350px);padding:clamp(8px,2vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);color:var(--fg);display:flex;flex-direction:column;gap:clamp(5px,1.5vmin,12px);font:600 clamp(12px,3.5vmin,15px)/1.25 var(--font-sans),sans-serif;overflow:hidden}.head,.foot,.row{display:flex;justify-content:space-between;align-items:center;gap:8px}.head b{color:var(--accent)}.head span,.foot,.muted{color:var(--muted)}.stage{flex:1;min-height:0;display:flex;align-items:center;justify-content:center;gap:8px}.cell,.pill{border:1px solid var(--line);border-radius:8px;background:var(--bg);padding:clamp(4px,1.2vmin,9px);text-align:center}.pill{border-radius:999px}.on{border-color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 17%,var(--surface))!important;color:var(--fg)!important}.bad{border-color:#e16a5d!important;background:color-mix(in srgb,#e16a5d 18%,var(--surface))!important}.foot{font-size:clamp(12px,3vmin,14px)}.pk{justify-content:space-around}.existing{display:grid;gap:6px;min-width:35%}.incoming{min-width:35%;transition:.25s}.arrow{font-size:clamp(20px,6vmin,34px);color:var(--accent)}
js
let n=0;function tick(){let e=document.getElementById('incoming');e.textContent=n?'4 · Cy':'3 · Cy';e.classList.toggle('bad',!n);e.classList.toggle('on',!!n);document.getElementById('verdict').textContent=n?'id 4: inserted':'duplicate id: rejected';n=1-n}tick();const t=setInterval(tick,1100);document.querySelector('.demo').onclick=()=>{clearInterval(t);tick()}

A primary key is a column or combination of columns that identifies one row. The same primary-key value cannot appear twice in a table, and primary-key columns cannot be NULL.

The demo rejects a second id 3 but accepts id 4. If a business value can change, consider a stable identifier and a separate business uniqueness constraint.

When to use

Define one when rows need stable references or updates.

Open as page ↗