← Back to MySQL Course | Chapter 7: Updating & Deleting | Lesson 4 of 4

REPLACE INTO

REPLACE INTO पहले से एक form रखने वाले student के लिए एक नया form सौंपने जैसा है: पुराना form फेंक दिया जाता है और नया इसकी जगह ले लेता है।
Syntax
sql
REPLACE INTO table_name (column1, column2)
VALUES (value1, value2);

REPLACE INTO का Introduction

REPLACE INTO INSERT जैसा काम करता है, लेकिन duplicates handle करने के लिए एक twist के साथ। अगर same primary key या unique index वाली एक row पहले से exist करती है, MySQL पहले पुराने row को delete करता है और नया insert करता है। अगर row exist नहीं करती, यह इसे बस एक normal INSERT जैसा insert कर देता है।

उदाहरण: Introduction to REPLACE INTO

sql
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO users VALUES (1, 'Amit');
REPLACE INTO users VALUES (1, 'Amit Kumar');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Set Syntax के साथ REPLACE INTO

आप column-list form के बजाय REPLACE statement के साथ SET syntax भी इस्तेमाल कर सकते हैं। यह highly readable है और UPDATE syntax जैसा ही काम करता है। यह एक नज़र में clear कर देता है कि कौन से columns को नई values मिल रही हैं।

उदाहरण: REPLACE INTO with Set Syntax

sql
REPLACE INTO users SET id = 1, name = 'Amit Kumar';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Select Queries के साथ REPLACE INTO

आप REPLACE INTO को एक SELECT query के साथ इस्तेमाल करके data को एक table से दूसरे में copy कर सकते हैं, एक staging table को एक live में sync करने के लिए उपयोगी। अगर destination table में duplicate keys मिलें, वे error होने के बजाय automatically नई rows से update हो जाएँगी।

उदाहरण: REPLACE INTO with Select Queries

sql
REPLACE INTO live_users SELECT * FROM staging_users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Behind-the-Scenes Delete

जब REPLACE एक duplicate ढूँढता है, यह behind the scenes पुराना row delete करता है और एक नया insert करता है। इसका मतलब है कि कोई auto-increment primary key बदल सकती है अगर explicitly न दी गई हो।

यह table पर किसी भी DELETE और INSERT triggers को भी trigger करता है, जो आपको surprise कर सकता है अगर आप सिर्फ एक update की उम्मीद कर रहे थे।

उदाहरण: The Behind-the-Scenes Delete

sql
-- REPLACE deletes the old row and inserts a new one; a fresh AUTO_INCREMENT value may be assigned
REPLACE INTO users (id, name) VALUES (1, 'Amit Kumar');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

REPLACE बनाम INSERT ... ON DUPLICATE KEY UPDATE

REPLACE पुराना row delete करता है और एक नया insert करता है, हर column को आपकी दी गई values पर reset करते हुए। INSERT ... ON DUPLICATE KEY UPDATE मौजूदा row को delete करने के बजाय सिर्फ specified columns update करता है।

REPLACE तब इस्तेमाल करें जब आप सभी columns को आसानी से overwrite करना चाहें, और untouched columns preserve करना चाहें तो UPDATE form इस्तेमाल करें।

उदाहरण: REPLACE vs INSERT ... ON DUPLICATE KEY UPDATE

sql
REPLACE INTO users (id, name, email) VALUES (1, 'Amit', '[email protected]'); -- resets every column
INSERT INTO users (id, name, email) VALUES (1, 'Amit', '[email protected]')
ON DUPLICATE KEY UPDATE name = 'Amit'; -- updates only name

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. कुछ columns update करना चाहते समय REPLACE INTO इस्तेमाल करना, क्योंकि यह पुराना row delete करके एक नया insert करता है, ताकि unspecified columns वापस defaults पर चले जाएँ।
  2. बिना PRIMARY KEY या UNIQUE index के इसके काम करने की उम्मीद करना, ताकि यह बस नई rows insert करे।
  3. यह surprise होना कि AUTO_INCREMENT values और foreign-key cascades affect होते हैं क्योंकि row असल में पहले delete होता है।
चैप्टर सारांश
  • UPDATE मौजूदा rows बदलता है, और यह एक साथ कई columns set कर सकता है।
  • DELETE किसी table से rows हटाता है, और एक WHERE clause control करता है कि कौन सी।
  • REPLACE INTO एक row insert करता है या same key वाली एक मौजूदा को replace करता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.