PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

SQL · Theory

Replace

← All stacks

Theory

36/439

Replace

REPLACE INTO students (roll, name, marks) VALUES (12, 'Asha', 90); if roll 12 exists, MySQL deletes that row and inserts the new one (PK/UNIQUE clash). It is not an in-place UPDATE — triggers and FKs behave like delete + insert.

Prefer INSERT … ON DUPLICATE KEY UPDATE when you want to keep the same row id and only change columns. REPLACE can break child rows.

Trap — using REPLACE as a casual synonym for UPDATE.

On the example next to this theory: Replace: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Exam tip

REPLACE is delete+insert on key clash. Upsert with ON DUPLICATE KEY UPDATE.

Replace — sample query

-- Replace
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

Replace: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Short notes

  • DefREPLACE = delete old PK row + insert new.
  • RulePrefer ON DUPLICATE KEY UPDATE for upserts.
  • TrapREPLACE breaking FOREIGN KEY children.

Questions

1

REPLACE vs UPDATE?

36 / 439

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.