π§βπ What is Relationship?
A Relationship in DBMS defines how two or more entities are connected or associated with each other.
In other words, it shows how one entity is related to another in a real-world context.
π§βπ Example (Real-life):
In a university database:
π€ Student
π Course
The relationship could be:
π A Student ENROLLS in a Course
Here:
StudentandCourseare entitiesENROLLSis the relationship between them
<div class="infographic-cardinality">
<style>
.infographic-cardinality {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--mono-font: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
background: var(--paper);
color: var(--ink);
padding: 2rem 1.5rem 2.5rem;
max-width: 780px;
margin: 0 auto;
box-sizing: border-box;
}
.infographic-cardinality[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
}
.infographic-cardinality *, .infographic-cardinality *::before, .infographic-cardinality *::after {
box-sizing: border-box;
}
.infographic-cardinality h2 {
font-size: 1.4rem;
margin: 0 0 0.25rem;
letter-spacing: -0.01em;
}
.infographic-cardinality .subtitle {
color: var(--ink-soft);
font-size: 0.92rem;
margin: 0 0 1.5rem;
max-width: 54ch;
}
.infographic-cardinality .tabs {
display: inline-flex;
background: var(--paper-alt);
border-radius: 999px;
padding: 4px;
margin-bottom: 1.1rem;
gap: 4px;
}
.infographic-cardinality .tab {
font: inherit;
font-size: 0.85rem;
font-weight: 700;
padding: 0.5rem 1.1rem;
border-radius: 999px;
border: none;
background: transparent;
color: var(--ink-soft);
cursor: pointer;
transition: background 0.2s ease, color 0.2s ease;
white-space: nowrap;
}
.infographic-cardinality .tab.is-active {
background: var(--accent);
color: #fff;
}
.infographic-cardinality .stage {
background: var(--paper-alt);
border-radius: 14px;
padding: 0.75rem 0.5rem 0.25rem;
margin-bottom: 1rem;
}
.infographic-cardinality svg {
width: 100%;
height: auto;
display: block;
}
.infographic-cardinality .col-header {
font-size: 13px;
font-weight: 700;
fill: var(--ink);
text-anchor: middle;
}
.infographic-cardinality .node-circle {
fill: var(--paper);
stroke: var(--accent);
stroke-width: 2;
opacity: 0;
transform-origin: center;
transform-box: fill-box;
animation: card-node-in 0.35s ease forwards;
}
.infographic-cardinality .node-emoji {
font-size: 16px;
text-anchor: middle;
dominant-baseline: central;
opacity: 0;
animation: card-node-in 0.35s ease forwards;
}
.infographic-cardinality .node-label {
font-size: 11px;
fill: var(--ink-soft);
text-anchor: middle;
opacity: 0;
animation: card-node-in 0.35s ease forwards;
}
@keyframes card-node-in {
from { opacity: 0; transform: scale(0.6); }
to { opacity: 1; transform: scale(1); }
}
.infographic-cardinality .rel-line {
stroke: var(--accent);
stroke-width: 2;
fill: none;
}
.infographic-cardinality .example {
font-size: 0.85rem;
color: var(--ink-soft);
text-align: center;
padding: 0.7rem 0.5rem 1rem;
}
.infographic-cardinality .example strong { color: var(--accent); }
.infographic-cardinality .meaning-row {
display: grid;
grid-template-columns: 34% 66%;
gap: 0.5rem;
padding: 0.7rem 0.9rem;
background: var(--paper-alt);
border-radius: 10px;
font-size: 0.85rem;
margin-bottom: 1.25rem;
}
.infographic-cardinality .meaning-row .m-type {
font-weight: 700;
}
.infographic-cardinality .meaning-row .m-desc {
color: var(--ink-soft);
}
.infographic-cardinality .meaning-row .m-desc strong { color: var(--ink); }
.infographic-cardinality .takeaway {
margin-top: 0.25rem;
font-size: 0.88rem;
color: var(--ink-soft);
border-left: 3px solid var(--accent);
padding: 0.5rem 0 0.5rem 0.9rem;
}
.infographic-cardinality .takeaway strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-cardinality *, .infographic-cardinality *::before, .infographic-cardinality *::after {
animation-duration: 0.001ms !important;
transition-duration: 0.001ms !important;
}
}
</style>
<h2>π’ Types of Relationships (Cardinality)</h2>
<p class="subtitle">The lines don't lie. Switch cardinality and watch how many connections each side actually gets.</p>
<div class="tabs" role="tablist">
<button class="tab is-active" type="button" data-key="1:1">1:1</button>
<button class="tab" type="button" data-key="1:N">1:N</button>
<button class="tab" type="button" data-key="M:N">M:N</button>
</div>
<div class="meaning-row" id="meaning-row">
<span class="m-type">1:1 (One to One)</span>
<span class="m-desc">One entity of A is related to <strong>only one</strong> entity of B</span>
</div>
<div class="stage">
<svg id="rel-svg" viewBox="0 0 640 300" xmlns="http://www.w3.org/2000/svg"></svg>
</div>
<p class="example" id="rel-example"></p>
<p class="takeaway"><strong>The pattern is the definition.</strong> Count how many lines leave one side β that number alone tells you whether it's 1:1, 1:N, or M:N.</p>
<script>
(function () {
var wrapper = document.currentScript.closest('.infographic-cardinality')
|| document.querySelector('.infographic-cardinality');
var bodyAttr = document.body.getAttribute('data-darkmode');
var dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
var match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
var CONFIG = {
'1:1': {
meaningType: '1:1 (One to One)',
meaningDesc: 'One entity of A is related to <strong>only one</strong> entity of B',
leftLabel: 'Students', leftIcon: 'π§βπ',
leftItems: ['Aisha', 'Rahul', 'Meera'],
rightLabel: 'Lockers', rightIcon: 'π',
rightItems: ['Locker 1', 'Locker 2', 'Locker 3'],
connections: [[0, 0], [1, 1], [2, 2]],
example: '<strong>1:1</strong> β One student has one locker β every match is exclusive, nothing shared.'
},
'1:N': {
meaningType: '1:N (One to Many)',
meaningDesc: 'One entity of A is related to <strong>many</strong> entities of B',
leftLabel: 'Teacher', leftIcon: 'π¨βπ«',
leftItems: ['Mr. Shah'],
rightLabel: 'Students', rightIcon: 'π§βπ',
rightItems: ['Aisha', 'Rahul', 'Meera', 'Karan'],
connections: [[0, 0], [0, 1], [0, 2], [0, 3]],
example: '<strong>1:N</strong> β One teacher teaches many students β a single node fans out to several.'
},
'M:N': {
meaningType: 'M:N (Many to Many)',
meaningDesc: 'Many entities of A can relate to <strong>many</strong> entities of B',
leftLabel: 'Students', leftIcon: 'π§βπ',
leftItems: ['Aisha', 'Rahul', 'Meera'],
rightLabel: 'Courses', rightIcon: 'π',
rightItems: ['DBMS', 'AI', 'Web Dev'],
connections: [[0, 0], [0, 1], [1, 1], [1, 2], [2, 0], [2, 2]],
example: '<strong>M:N</strong> β Many students enroll in many courses β lines cross freely both ways.'
}
};
var svg = wrapper.querySelector('#rel-svg');
var tabs = wrapper.querySelectorAll('.tab');
var meaningRow = wrapper.querySelector('#meaning-row');
var exampleEl = wrapper.querySelector('#rel-example');
var SVG_NS = 'http://www.w3.org/2000/svg';
var LEFT_X = 110, RIGHT_X = 530, TOP_Y = 70, BOTTOM_Y = 250, RADIUS = 20;
function yFor(i, count) {
if (count <= 1) return (TOP_Y + BOTTOM_Y) / 2;
return TOP_Y + i * ((BOTTOM_Y - TOP_Y) / (count - 1));
}
function el(tag, attrs) {
var e = document.createElementNS(SVG_NS, tag);
Object.keys(attrs).forEach(function (k) { e.setAttribute(k, attrs[k]); });
return e;
}
function render(key) {
var cfg = CONFIG[key];
svg.innerHTML = '';
var leftPositions = cfg.leftItems.map(function (_, i) { return yFor(i, cfg.leftItems.length); });
var rightPositions = cfg.rightItems.map(function (_, i) { return yFor(i, cfg.rightItems.length); });
// headers
var lh = el('text', { class: 'col-header', x: LEFT_X, y: 30 });
lh.textContent = cfg.leftIcon + ' ' + cfg.leftLabel;
svg.appendChild(lh);
var rh = el('text', { class: 'col-header', x: RIGHT_X, y: 30 });
rh.textContent = cfg.rightIcon + ' ' + cfg.rightLabel;
svg.appendChild(rh);
// lines first (so nodes sit on top), animated draw-in
cfg.connections.forEach(function (pair, i) {
var y1 = leftPositions[pair[0]], y2 = rightPositions[pair[1]];
var length = Math.hypot(RIGHT_X - LEFT_X, y2 - y1);
var line = el('line', {
class: 'rel-line',
x1: LEFT_X + RADIUS, y1: y1,
x2: RIGHT_X - RADIUS, y2: y2
});
line.style.strokeDasharray = length;
line.style.strokeDashoffset = length;
line.style.transition = 'stroke-dashoffset 0.5s ease';
line.style.transitionDelay = (0.15 + i * 0.08) + 's';
svg.appendChild(line);
requestAnimationFrame(function () {
requestAnimationFrame(function () { line.style.strokeDashoffset = '0'; });
});
});
// left nodes
cfg.leftItems.forEach(function (name, i) {
var y = leftPositions[i];
var c = el('circle', { class: 'node-circle', cx: LEFT_X, cy: y, r: RADIUS });
c.style.animationDelay = (i * 0.05) + 's';
svg.appendChild(c);
var t = el('text', { class: 'node-emoji', x: LEFT_X, y: y });
t.textContent = cfg.leftIcon;
t.style.animationDelay = (i * 0.05) + 's';
svg.appendChild(t);
var l = el('text', { class: 'node-label', x: LEFT_X, y: y + RADIUS + 16 });
l.textContent = name;
l.style.animationDelay = (i * 0.05) + 's';
svg.appendChild(l);
});
// right nodes
cfg.rightItems.forEach(function (name, i) {
var y = rightPositions[i];
var c = el('circle', { class: 'node-circle', cx: RIGHT_X, cy: y, r: RADIUS });
c.style.animationDelay = (i * 0.05 + 0.1) + 's';
svg.appendChild(c);
var t = el('text', { class: 'node-emoji', x: RIGHT_X, y: y });
t.textContent = cfg.rightIcon;
t.style.animationDelay = (i * 0.05 + 0.1) + 's';
svg.appendChild(t);
var l = el('text', { class: 'node-label', x: RIGHT_X, y: y + RADIUS + 16 });
l.textContent = name;
l.style.animationDelay = (i * 0.05 + 0.1) + 's';
svg.appendChild(l);
});
meaningRow.innerHTML =
'<span class="m-type">' + cfg.meaningType + '</span>' +
'<span class="m-desc">' + cfg.meaningDesc + '</span>';
exampleEl.innerHTML = cfg.example;
}
tabs.forEach(function (tab) {
tab.addEventListener('click', function () {
tabs.forEach(function (t) { t.classList.remove('is-active'); });
tab.classList.add('is-active');
render(tab.getAttribute('data-key'));
});
});
render('1:1');
})();
</script>
</div>.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!');
});π§Ύ Relationship Degree:
The degree of a relationship refers to the number of entity sets that participate in a relationship.
Degree | Meaning | Example |
|---|---|---|
Unary | Relationship between same entity | Employee manages another Employee |
Binary | Relationship between two entities | Student ENROLLS in Course |
Ternary | Relationship between three entities | Doctor prescribes Medicine to Patient |
π§ Summary:
πΉ Concept | π Description |
|---|---|
Entity | Real-world object (e.g., Student, Course) |
Attribute | Property of an entity (e.g., Name, ID) |
Relationship | Describes how entities are connected (e.g., ENROLLS) |
ER Diagram | Uses diamonds to show relationships between rectangles |