π Base Table (used for all examples)
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade VARCHAR(2),
section CHAR(1)
);
INSERT INTO students (id, name, age, grade, section) VALUES
(1, 'Anya', 17, 'A', 'A'),
(2, 'Raj', 18, 'B', 'B');We will use this same table for all commands below.
β INSERT β Add New Rows Only
The INSERT command adds new rows.
β If the primary key already exists β it throws an error.
Example
INSERT INTO students (id, name, age, grade, section)
VALUES (3, 'Swastika', 17, 'B', 'C');βοΈ Adds a new third row
β Fails if id = 3 already exists
π REPLACE β Insert or Replace Row
REPLACE will:
β‘οΈ insert if the primary key is new
β‘οΈ delete + insert if primary key already exists
Example
REPLACE INTO students (id, name, age, grade, section)
VALUES (1, 'Swasti', 17, 'A', 'C');βοΈ If id = 1 exists β old row removed
βοΈ new row inserted
β οΈ ALL old row values get wiped
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>INSERT vs REPLACE β Try It Yourself</title>
<style>
.infographic-replacerunner {
--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;
--warn: #b45309;
--warn-soft: #fef3c7;
--on-accent: #ffffff;
--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: 800px;
margin: 0 auto;
box-sizing: border-box;
overflow-x: hidden;
}
.infographic-replacerunner[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;
--warn: #fbbf24;
--warn-soft: #3a2f0e;
--on-accent: #101418;
}
.infographic-replacerunner * { box-sizing: border-box; }
.rp-topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; gap: 0.5rem; flex-wrap: wrap; }
.rp-progress { font-size: 0.78rem; color: var(--ink-soft); }
.rp-showfull-btn {
font: inherit; font-size: 0.78rem; background: none; border: 1px solid var(--line);
border-radius: 999px; padding: 0.35rem 0.8rem; cursor: pointer; color: var(--ink-soft);
}
.rp-showfull-btn:hover { border-color: var(--accent); color: var(--accent); }
.rp-showfull-btn.is-on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.rp-fullcode-panel { display: none; margin-bottom: 1.25rem; }
.rp-fullcode-panel.is-visible { display: block; }
.rp-fullcode-scroll { overflow-x: auto; }
.rp-fullcode {
background: var(--ink); color: #d4e2ff; border-radius: 8px; padding: 1rem 1.2rem;
font-family: var(--mono-font); font-size: 0.72rem; line-height: 1.7; white-space: pre; opacity: 0.85;
display: inline-block;
min-width: 100%;
}
.infographic-replacerunner[data-theme="dark"] .rp-fullcode { background: #05070a; }
.rp-frame { background: var(--paper-alt); border: 1px solid var(--line); border-radius: 10px; padding: 1.25rem; }
.rp-kw { color: #9cc5ff; } .rp-str { color: #f8d980; } .rp-num { color: #4ade80; } .rp-cm { color: #7c8aa8; }
.rp-step { display: none; margin-bottom: 1rem; }
.rp-step.is-revealed { display: block; animation: rp-step-in 0.4s ease; }
@keyframes rp-step-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.rp-step.is-done .rp-step-code-full { display: none; }
.rp-step.is-done .rp-step-done-pill { display: flex; }
.rp-step-done-pill {
display: none; align-items: center; gap: 0.5rem; background: var(--good-soft); color: var(--good);
border-radius: 7px; padding: 0.55rem 0.85rem; font-family: var(--mono-font); font-size: 0.78rem; cursor: pointer;
}
.rp-step-done-pill.is-danger-done { background: var(--bad-soft); color: var(--bad); }
.rp-step-done-pill.is-warn-done { background: var(--warn-soft); color: var(--warn); }
.rp-step-done-pill .chevron { margin-left: auto; font-size: 0.7rem; opacity: 0.7; }
.rp-step.is-expanded-done .rp-step-code-full { display: block; }
.rp-step.is-expanded-done .rp-step-done-pill { background: var(--accent-soft); color: var(--accent); }
.rp-step-label { font-family: var(--display-font); font-size: 1.02rem; margin: 0 0 0.5rem; }
.rp-step-desc { font-size: 0.82rem; color: var(--ink-soft); margin: 0 0 0.7rem; line-height: 1.45; }
.rp-step-warn {
font-size: 0.8rem; color: var(--warn); background: var(--warn-soft); border-radius: 7px;
padding: 0.5rem 0.7rem; margin: 0 0 0.7rem; line-height: 1.4;
}
.rp-code-scroll { overflow-x: auto; }
.rp-code {
background: var(--ink); color: #d4e2ff; border-radius: 8px; padding: 0.8rem 1rem;
font-family: var(--mono-font); font-size: 0.78rem; line-height: 1.6; white-space: pre;
display: inline-block;
min-width: 100%;
}
.infographic-replacerunner[data-theme="dark"] .rp-code { background: #05070a; }
.rp-run-row { display: flex; justify-content: center; gap: 0.5rem; margin-top: 0.85rem; flex-wrap: wrap; }
.rp-run-btn {
font: inherit; font-size: 0.83rem; background: var(--accent); color: var(--on-accent);
border: none; border-radius: 7px; padding: 0.55rem 1rem; cursor: pointer;
}
.rp-run-btn:hover { opacity: 0.9; }
.rp-run-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.rp-run-btn.is-danger { background: var(--bad); }
.rp-run-btn.is-secondary { background: var(--paper); color: var(--ink); border: 1px solid var(--line); }
.rp-run-btn.rp-next-btn { background: var(--good); display: none; }
.rp-mini-msg {
margin-top: 0.7rem; padding: 0.55rem 0.75rem; border-radius: 7px; font-size: 0.8rem; line-height: 1.5; display: none;
}
.rp-mini-msg.is-visible { display: block; }
.rp-mini-msg.st-good { background: var(--good-soft); color: var(--good); }
.rp-mini-msg.st-bad { background: var(--bad-soft); color: var(--bad); }
.rp-mini-msg.st-warn { background: var(--warn-soft); color: var(--warn); }
.rp-output-label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); margin: 1.1rem 0 0.6rem; }
.rp-table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; min-width: 0; }
table.rp-table {
width: 100%; min-width: 380px; border-collapse: collapse; background: var(--paper);
border-radius: 8px; font-size: 0.85rem;
}
table.rp-table th, table.rp-table td {
border: 1px solid var(--line); padding: 0.5rem 0.65rem; text-align: left; white-space: nowrap;
transition: background 0.5s, color 0.5s, opacity 0.5s;
}
table.rp-table th { background: var(--accent); color: var(--on-accent); font-weight: 700; font-size: 0.72rem; }
table.rp-table tr.is-new td { animation: rp-row-in 1s ease; }
@keyframes rp-row-in { 0%,35% { background: var(--good-soft); } 100% { background: transparent; } }
table.rp-table tr.is-removing td { opacity: 0.3; text-decoration: line-through; background: var(--bad-soft); }
table.rp-table td.is-null { color: var(--warn); font-style: italic; font-weight: 700; }
.rp-reset-row { display: flex; justify-content: center; margin-top: 1.25rem; }
@media (prefers-reduced-motion: reduce) {
.infographic-replacerunner *, .infographic-replacerunner *::before, .infographic-replacerunner *::after {
transition-duration: 0.001ms !important; animation-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<div class="infographic-replacerunner" id="replacerunner">
<div class="rp-topbar">
<span class="rp-progress" id="rpProgress"></span>
<button class="rp-showfull-btn" id="rpShowFullBtn" type="button">ποΈ Show full SQL script</button>
</div>
<div class="rp-fullcode-panel" id="rpFullCodePanel">
<div class="rp-fullcode-scroll">
<div class="rp-fullcode" id="rpFullCodeContent"></div>
</div>
</div>
<div class="rp-frame">
<div id="rpSteps"></div>
<p class="rp-output-label">Table: students</p>
<div class="rp-table-scroll">
<table class="rp-table" id="rpTable">
<thead><tr><th>id</th><th>name</th><th>age</th><th>grade</th><th>section</th></tr></thead>
<tbody id="rpTbody"></tbody>
</table>
</div>
<div class="rp-reset-row">
<button class="rp-run-btn is-secondary" id="rpReset" type="button">βΊ Reset all</button>
</div>
</div>
</div>
<script>
(function () {
const wrapper = document.getElementById('replacerunner');
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 () {
function kw(s) { return '<span class="rp-kw">' + s + '</span>'; }
function str(s) { return '<span class="rp-str">' + s + '</span>'; }
function num(s) { return '<span class="rp-num">' + s + '</span>'; }
function cm(s) { return '<span class="rp-cm">' + s + '</span>'; }
const setupCode =
kw('CREATE TABLE') + ' students (\n' +
' id ' + kw('INT PRIMARY KEY') + ',\n' +
' name ' + kw('VARCHAR') + '(50),\n' +
' age ' + kw('INT') + ',\n' +
' grade ' + kw('VARCHAR') + '(2),\n' +
' section ' + kw('CHAR') + '(1)\n' +
');\n\n' +
kw('INSERT INTO') + ' students (id, name, age, grade, section) ' + kw('VALUES') + '\n' +
'(' + num('1') + ', ' + str("'Anya'") + ', ' + num('17') + ', ' + str("'A'") + ', ' + str("'A'") + '),\n' +
'(' + num('2') + ', ' + str("'Raj'") + ', ' + num('18') + ', ' + str("'B'") + ', ' + str("'B'") + ');';
const insertNewCode = kw('INSERT INTO') + ' students (id, name, age, grade, section)\n' + kw('VALUES') + ' (' + num('3') + ', ' + str("'Swastika'") + ', ' + num('17') + ', ' + str("'B'") + ', ' + str("'C'") + ');';
const insertDupCode = cm('-- Trying id = 3 again') + '\n' + kw('INSERT INTO') + ' students (id, name, age, grade, section)\n' + kw('VALUES') + ' (' + num('3') + ', ' + str("'Someone Else'") + ', ' + num('16') + ', ' + str("'A'") + ', ' + str("'A'") + ');';
const replaceFullCode = kw('REPLACE INTO') + ' students (id, name, age, grade, section)\n' + kw('VALUES') + ' (' + num('1') + ', ' + str("'Swasti'") + ', ' + num('17') + ', ' + str("'A'") + ', ' + str("'C'") + ');';
const replacePartialCode = cm('-- Only id and name are given this time') + '\n' + kw('REPLACE INTO') + ' students (id, name)\n' + kw('VALUES') + ' (' + num('2') + ', ' + str("'Raj'") + ');';
const table = document.getElementById('rpTable');
const tbody = document.getElementById('rpTbody');
const stepsContainer = document.getElementById('rpSteps');
const progress = document.getElementById('rpProgress');
const fullCodeContent = document.getElementById('rpFullCodeContent');
function renderBaseRows() {
tbody.innerHTML =
'<tr class="is-new" data-id="1"><td>1</td><td>Anya</td><td>17</td><td>A</td><td>A</td></tr>' +
'<tr class="is-new" data-id="2"><td>2</td><td>Raj</td><td>18</td><td>B</td><td>B</td></tr>';
}
const steps = [
{ key: 'setup', title: 'π Base Table', desc: 'Same table and data for every example below.', code: setupCode, type: 'setup' },
{ key: 'insertNew', title: 'β INSERT β new id', desc: 'id = 3 is new, so this succeeds normally.', code: insertNewCode, type: 'insertNew' },
{ key: 'insertDup', title: 'β INSERT β duplicate id', desc: 'id = 3 now already exists.', code: insertDupCode, type: 'insertDup' },
{ key: 'replaceFull', title: 'π REPLACE β full row', desc: '', code: replaceFullCode, type: 'replaceFull',
warn: 'β οΈ This deletes the entire old row for id = 1 and inserts a brand new one β age and grade only look unchanged because they were typed in again.' },
{ key: 'replacePartial', title: 'π REPLACE β columns omitted', desc: '', code: replacePartialCode, type: 'replacePartial',
warn: 'β οΈ The real danger: age, grade, and section aren\u2019t mentioned this time, so the new row gets NULL for all of them β REPLACE never merges with the old row.' }
];
fullCodeContent.innerHTML = steps.map(s => s.code).join('\n\n');
steps.forEach((s, i) => {
const stepEl = document.createElement('div');
stepEl.className = 'rp-step' + (i === 0 ? ' is-revealed' : '');
stepEl.id = 'step-' + s.key;
const label = s.type === 'setup' ? 'Setup' : ('Run ' + s.title.replace(/^[^\s]+\s/, ''));
const pillClass = s.key === 'insertDup' ? ' is-danger-done' : (s.warn ? ' is-warn-done' : '');
stepEl.innerHTML =
'<div class="rp-step-done-pill' + pillClass + '"><span>β ' + s.title + '</span><span class="chevron">\u25be view code</span></div>' +
'<div class="rp-step-code-full">' +
'<p class="rp-step-label">' + s.title + '</p>' +
(s.desc ? '<p class="rp-step-desc">' + s.desc + '</p>' : '') +
(s.warn ? '<p class="rp-step-warn">' + s.warn + '</p>' : '') +
'<div class="rp-code-scroll"><div class="rp-code">' + s.code + '</div></div>' +
'<div class="rp-run-row"><button class="rp-run-btn' + (s.key === 'insertDup' ? ' is-danger' : '') + '" id="btn-' + s.key + '" type="button">βΆ ' + label + '</button></div>' +
'<p class="rp-mini-msg" id="msg-' + s.key + '"></p>' +
(i < steps.length - 1 ? '<div class="rp-run-row"><button class="rp-run-btn rp-next-btn" id="nextstep-' + s.key + '" type="button">Next command β</button></div>' : '') +
'</div>';
stepsContainer.appendChild(stepEl);
});
function updateProgress(doneCount) {
progress.textContent = doneCount + ' / ' + steps.length + ' commands run';
}
updateProgress(0);
function showMsg(key, text, kind) {
const el = document.getElementById('msg-' + key);
el.textContent = text;
el.className = 'rp-mini-msg is-visible st-' + kind;
}
function unlockNext(key) {
const btn = document.getElementById('nextstep-' + key);
if (btn) btn.style.display = 'inline-block';
}
let doneCount = 0;
function goToNext(key) {
document.getElementById('step-' + key).classList.add('is-done');
doneCount++;
updateProgress(doneCount);
const idx = steps.findIndex(s => s.key === key);
const next = steps[idx + 1];
if (next) {
const nextEl = document.getElementById('step-' + next.key);
nextEl.classList.add('is-revealed');
nextEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}
steps.forEach(s => {
document.getElementById('btn-' + s.key).addEventListener('click', () => {
document.getElementById('btn-' + s.key).disabled = true;
if (s.type === 'setup') {
renderBaseRows();
showMsg(s.key, 'Table created, 2 rows inserted.', 'good');
} else if (s.type === 'insertNew') {
const tr = document.createElement('tr');
tr.className = 'is-new';
tr.dataset.id = 3;
tr.innerHTML = '<td>3</td><td>Swastika</td><td>17</td><td>B</td><td>C</td>';
tbody.appendChild(tr);
showMsg(s.key, 'Row inserted β id 3 was free to use.', 'good');
} else if (s.type === 'insertDup') {
showMsg(s.key, "Blocked β Duplicate entry '3' for key PRIMARY. INSERT never overwrites an existing row.", 'bad');
} else if (s.type === 'replaceFull') {
const row = tbody.querySelector('tr[data-id="1"]');
row.classList.add('is-removing');
setTimeout(() => {
row.remove();
const tr = document.createElement('tr');
tr.className = 'is-new';
tr.dataset.id = 1;
tr.innerHTML = '<td>1</td><td>Swasti</td><td>17</td><td>A</td><td>C</td>';
tbody.insertBefore(tr, tbody.firstChild);
showMsg(s.key, 'The old id = 1 row was deleted, and this new one inserted in its place β same id, but a completely fresh row underneath.', 'warn');
}, 550);
} else if (s.type === 'replacePartial') {
const row = tbody.querySelector('tr[data-id="2"]');
row.classList.add('is-removing');
setTimeout(() => {
row.remove();
const tr = document.createElement('tr');
tr.className = 'is-new';
tr.dataset.id = 2;
tr.innerHTML = '<td>2</td><td>Raj</td><td class="is-null">NULL</td><td class="is-null">NULL</td><td class="is-null">NULL</td>';
tbody.appendChild(tr);
showMsg(s.key, "Raj's age, grade, and section are all NULL now β REPLACE deleted the old row and built a new one from only what you gave it.", 'warn');
doneCount++;
updateProgress(doneCount);
}, 550);
}
unlockNext(s.key);
});
const nextBtn = document.getElementById('nextstep-' + s.key);
if (nextBtn) nextBtn.addEventListener('click', () => goToNext(s.key));
});
document.getElementById('rpShowFullBtn').addEventListener('click', function () {
const panel = document.getElementById('rpFullCodePanel');
const on = panel.classList.toggle('is-visible');
this.classList.toggle('is-on', on);
this.textContent = on ? 'ποΈ Hide full SQL script' : 'ποΈ Show full SQL script';
});
document.querySelectorAll('.rp-step-done-pill').forEach(pill => {
pill.addEventListener('click', () => {
const step = pill.closest('.rp-step');
step.classList.toggle('is-expanded-done');
pill.querySelector('.chevron').textContent = step.classList.contains('is-expanded-done') ? '\u25b4 hide code' : '\u25be view code';
});
});
document.getElementById('rpReset').addEventListener('click', () => {
doneCount = 0;
updateProgress(0);
tbody.innerHTML = '';
steps.forEach((s, i) => {
const el = document.getElementById('step-' + s.key);
el.classList.remove('is-done', 'is-expanded-done');
el.classList.toggle('is-revealed', i === 0);
document.getElementById('msg-' + s.key).classList.remove('is-visible');
document.getElementById('btn-' + s.key).disabled = false;
const nextBtn = document.getElementById('nextstep-' + s.key);
if (nextBtn) nextBtn.style.display = 'none';
});
});
})();
</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!');
});β οΈ Important Notes
REPLACEis NOT like UPDATEIt is delete + new insert
Missing columns = old values lost
Auto-increment values can skip
β Summary Table
Feature | INSERT | REPLACE |
|---|---|---|
Adds new row | β | β |
Fails if key exists | β | β |
Deletes old row | β | β |
Updates values safely | β | β |
Risk level | Low | High |
π§ Which to Use?
βοΈ Use INSERT when adding new records
βοΈ Use REPLACE only when overwriting is intended
βοΈ For safe overwrite β use this (MySQL):
INSERT INTO students (id, name, age, grade, section)
VALUES (1, 'Anya', 17, 'A', 'A')
ON DUPLICATE KEY UPDATE
name='Anya', age=17, grade='A', section='A';βοΈ Updates only given fields
βοΈ Doesnβt delete entire row