π― What is Entity Constraints?
Entity Constraints ensure that each entity (row) in a table is uniquely identifiable and cannot be null or duplicated.
They are one of the most fundamental integrity constraints in a relational database.
Entity Constraint ensures that no primary key attribute of a relation (table) can be null or duplicated.
π Key Principles of Entity Constraint:
Rule | Explanation |
|---|---|
π Uniqueness | Each entity (row) must have a unique identifier (Primary Key) |
π« No NULL values | The Primary Key cannot be NULL, because every record must be identifiable |
π― Applies to Primary Keys | Entity constraint is enforced through the Primary Key |
π§Ύ Example: Student Table
StudentID (PK) | Name | Age |
|---|---|---|
101 | Alice | 20 |
102 | Bob | 21 |
NULL β | Zara | 22 |
102 β | John | 23 |
β Rows 1 and 2 are valid
β Row 3 violates entity constraint (StudentID is NULL)
β Row 4 violates entity constraint (StudentID is duplicate)
β οΈ Why is it Important?
Ensures each row is unique
Prevents duplicate or missing identifiers
Helps relate data across tables reliably using keys
Forms the backbone of data integrity
π§ͺ SQL Example:
CREATE TABLE Student (
StudentID INT PRIMARY KEY, -- Enforces entity constraint
Name VARCHAR(50),
Age INT
);PRIMARY KEYensuresStudentIDis unique and not null
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Entity Constraint β Try It Yourself</title>
<style>
.infographic-entityconstraint {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--good: #16a34a;
--good-soft: #dcfce7;
--bad: #dc2626;
--bad-soft: #fee2e2;
--display-font: Georgia, 'Iowan Old Style', serif;
--body-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
--mono-font: 'SF Mono', Consolas, monospace;
background: var(--paper);
color: var(--ink);
font-family: var(--body-font);
max-width: 860px;
margin: 0 auto;
box-sizing: border-box;
overflow-x: hidden;
}
.infographic-entityconstraint[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--good: #4ade80;
--good-soft: #123420;
--bad: #f87171;
--bad-soft: #3a1414;
}
.infographic-entityconstraint * { box-sizing: border-box; }
.ec-head { text-align: center; margin-bottom: 0.25rem; }
.ec-head h2 {
font-family: var(--display-font);
font-size: 1.9rem;
margin: 0 0 0.5rem;
}
.ec-head p {
color: var(--ink-soft);
font-size: 1rem;
margin: 0 auto;
max-width: 560px;
line-height: 1.55;
}
.ec-frame {
background: var(--paper-alt);
border: 1px solid var(--line);
border-radius: 10px;
padding: 1.25rem;
margin-top: 1.75rem;
}
.ec-frame-title {
font-family: var(--display-font);
font-size: 1.05rem;
margin: 0 0 0.85rem;
}
.ec-table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; min-width: 0; }
table.ec-table {
width: 100%;
min-width: 380px;
border-collapse: collapse;
background: var(--paper);
border-radius: 8px;
font-size: 0.9rem;
}
table.ec-table th, table.ec-table td {
border: 1px solid var(--line);
padding: 0.55rem 0.7rem;
text-align: left;
white-space: nowrap;
}
table.ec-table th { background: var(--accent); color: #fff; font-weight: 700; font-size: 0.78rem; }
table.ec-table td.ec-pk-col { font-family: var(--mono-font); color: var(--accent); font-weight: 700; }
table.ec-table td.is-null { color: var(--ink-soft); font-style: italic; }
table.ec-table tr.is-new td { animation: ec-row-in 0.5s ease; }
@keyframes ec-row-in { from { background: var(--good-soft); } to { background: transparent; } }
.ec-row-controls { display: flex; gap: 0.5rem; margin-top: 0.9rem; flex-wrap: wrap; }
.ec-btn {
font: inherit;
font-size: 0.82rem;
background: var(--paper);
border: 1px solid var(--line);
border-radius: 7px;
padding: 0.45rem 0.85rem;
cursor: pointer;
color: var(--ink);
}
.ec-btn:hover { border-color: var(--accent); }
.ec-form {
margin-top: 1.1rem;
padding-top: 1.1rem;
border-top: 1px dashed var(--line);
}
.ec-form-title { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); margin: 0 0 0.7rem; }
.ec-form-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.ec-field { flex: 1 1 140px; min-width: 0; }
.ec-field label { display: block; font-size: 0.72rem; color: var(--ink-soft); margin-bottom: 0.25rem; }
.ec-field input {
width: 100%;
font: inherit;
font-size: 0.88rem;
padding: 0.5rem 0.6rem;
border: 1px solid var(--line);
border-radius: 6px;
background: var(--paper);
color: var(--ink);
}
.ec-field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.ec-field .ec-hint { font-size: 0.66rem; color: var(--ink-soft); margin-top: 0.2rem; }
.ec-add-btn {
font: inherit;
font-size: 0.88rem;
background: var(--accent);
color: #fff;
border: none;
border-radius: 7px;
padding: 0.6rem 1.2rem;
cursor: pointer;
margin-top: 0.85rem;
}
.ec-add-btn:hover { opacity: 0.9; }
.ec-msg {
margin-top: 0.9rem;
padding: 0.7rem 0.9rem;
border-radius: 8px;
font-size: 0.85rem;
line-height: 1.55;
display: none;
}
.ec-msg.is-visible { display: block; }
.ec-msg.msg-bad { background: var(--bad-soft); color: var(--bad); }
.ec-msg.msg-good { background: var(--good-soft); color: var(--good); }
.ec-chips { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: center; margin-top: 1.75rem; }
.ec-chip {
font-size: 0.75rem;
background: var(--paper-alt);
border: 1px solid var(--line);
border-radius: 999px;
padding: 0.4rem 0.85rem;
color: var(--ink-soft);
transition: background 0.2s, color 0.2s, border-color 0.2s, transform 0.2s;
}
.ec-chip.is-hit {
background: var(--accent);
color: #fff;
border-color: var(--accent);
transform: scale(1.08);
}
@media (prefers-reduced-motion: reduce) {
.infographic-entityconstraint *, .infographic-entityconstraint *::before, .infographic-entityconstraint *::after {
transition-duration: 0.001ms !important;
animation-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<div class="infographic-entityconstraint" id="entityconstraint">
<div class="ec-head">
<h2>π Entity Constraint β Try It Yourself</h2>
</div>
<div class="ec-frame">
<p class="ec-frame-title">π Student</p>
<div class="ec-table-scroll">
<table class="ec-table" id="ecTable"></table>
</div>
<div class="ec-row-controls">
<button class="ec-btn" id="ecDupNameDemo" type="button">Try: two students both named "Alice"</button>
</div>
<div class="ec-form">
<p class="ec-form-title">Add a student</p>
<div class="ec-form-row">
<div class="ec-field">
<label for="ecId">StudentID</label>
<input id="ecId" type="text" placeholder="e.g. 3">
<div class="ec-hint">Primary Key β required, unique</div>
</div>
<div class="ec-field"><label for="ecName">Name</label><input id="ecName" type="text" placeholder="e.g. Chloe"></div>
<div class="ec-field"><label for="ecAge">Age</label><input id="ecAge" type="text" placeholder="e.g. 19"></div>
</div>
<button class="ec-add-btn" id="ecAdd" type="button">Add Student</button>
<div class="ec-msg" id="ecMsg"></div>
</div>
</div>
<div class="ec-chips" id="ecChips">
<span class="ec-chip" data-chip="unique">Uniqueness</span>
<span class="ec-chip" data-chip="nonull">No NULL values</span>
<span class="ec-chip" data-chip="appliespk">Applies to Primary Keys only</span>
</div>
</div>
<script>
(function () {
const wrapper = document.getElementById('entityconstraint');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
})();
(function () {
let students = [
{ id: '1', name: 'Alice', age: '20' },
{ id: '2', name: 'Bob', age: '21' }
];
let lastNew = -1;
const table = document.getElementById('ecTable');
const msg = document.getElementById('ecMsg');
const chips = document.querySelectorAll('.ec-chip');
function showMsg(text, type) {
msg.textContent = text;
msg.className = 'ec-msg is-visible msg-' + type;
}
function hitChip(n) {
chips.forEach(c => c.classList.remove('is-hit'));
const ids = Array.isArray(n) ? n : [n];
ids.forEach(id => {
const chip = document.querySelector('.ec-chip[data-chip="' + id + '"]');
if (chip) chip.classList.add('is-hit');
});
}
function render() {
let html = '<thead><tr><th>StudentID (PK)</th><th>Name</th><th>Age</th></tr></thead><tbody>';
students.forEach((s, i) => {
const nameCell = s.name ? '<td>' + s.name + '</td>' : '<td class="is-null">NULL</td>';
const ageCell = s.age ? '<td>' + s.age + '</td>' : '<td class="is-null">NULL</td>';
html += '<tr' + (i === lastNew ? ' class="is-new"' : '') + '>' +
'<td class="ec-pk-col">' + s.id + '</td>' + nameCell + ageCell + '</tr>';
});
html += '</tbody>';
table.innerHTML = html;
}
document.getElementById('ecAdd').addEventListener('click', () => {
const id = document.getElementById('ecId').value.trim();
const name = document.getElementById('ecName').value.trim();
const age = document.getElementById('ecAge').value.trim();
if (!id) {
hitChip('nonull');
showMsg('β StudentID can\u2019t be empty β a Primary Key can never be NULL, because every record must be identifiable.', 'bad');
return;
}
if (students.some(s => s.id === id)) {
hitChip('unique');
showMsg('β StudentID ' + id + ' already exists β each entity must have a unique Primary Key value.', 'bad');
return;
}
students.push({ id, name, age });
lastNew = students.length - 1;
render();
const dupName = students.filter(s => s.name && name && s.name.toLowerCase() === name.toLowerCase()).length > 1;
if (dupName) {
hitChip('appliespk');
showMsg('βοΈ Row added β two students can share the Name "' + name + '" just fine. Entity constraint only governs the Primary Key, not other columns.', 'good');
} else {
hitChip(['unique', 'nonull']);
showMsg('βοΈ Row added β StudentID ' + id + ' is present and unique, so the entity constraint is satisfied.', 'good');
}
document.getElementById('ecId').value = '';
document.getElementById('ecName').value = '';
document.getElementById('ecAge').value = '';
});
document.getElementById('ecDupNameDemo').addEventListener('click', () => {
document.getElementById('ecId').value = String(Math.max(...students.map(s => Number(s.id))) + 1);
document.getElementById('ecName').value = 'Alice';
document.getElementById('ecAge').value = '';
hitChip('appliespk');
showMsg('π ID and Name are pre-filled β click "Add Student" to see that a duplicate Name goes through fine, unlike a duplicate ID.', 'good');
});
render();
})();
</script>
</body>
</html>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});β Summary:
Feature | Description |
|---|---|
π― Enforced by | Primary Key |
π« Not Allowed | NULL or duplicate primary key values |
π Purpose | Guarantee each row is uniquely identifiable |
π Applies to | All entity sets (tables) in the relational model |