Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 43

INSERT INTO Command

INSERT क्या करता है

INSERT table में नई rows जोड़ता है। यह data manipulation language statement है: table structure पहले से मौजूद होती है और statement ऐसी values देता है जिन्हें data types तथा constraints पूरा करना होता है।

Prerequisite: CREATE DATABASE और CREATE TABLE lesson से coding_school.students table बनाएँ। नीचे सभी foundation pages में जानबूझकर same columns और data उपयोग किया गया है।

सबसे reliable basic form:

INSERT INTO table_name (column_1, column_2)
VALUES (value_1, value_2);

Columns और values position के अनुसार match होती हैं। Text तथा date literals single quotes में और numeric literals बिना quotes लिखे जाते हैं। Readability के लिए SQL keywords uppercase रख सकते हैं।

एक Row Insert करें

USE coding_school;

INSERT INTO students
  (full_name, class_name, marks, status)
VALUES
  ('Aarav', 'X-A', 86.50, 'Active');
सामान्य result Query OK, 1 row affected

student_id omit किया गया है क्योंकि MySQL इसे AUTO_INCREMENT से बनाता है। created_at omit करने पर उसका CURRENT_TIMESTAMP default चलता है। Explicit column list दोनों decisions साफ दिखाती है।

SELECT LAST_INSERT_ID() AS new_student_id;

Successful insert के तुरंत बाद same session में LAST_INSERT_ID() चलाएँ। Application drivers generated ID सामान्यतः सीधे देते हैं।

कई Rows Efficiently Insert करें

INSERT INTO students
  (full_name, class_name, marks, status)
VALUES
  ('Meera', 'X-A', 91.00, 'Active'),
  ('Kabir', 'X-B', 74.00, 'Inactive'),
  ('Sana',  'X-B', 88.50, 'Active');
सामान्य result Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0

Multi-row statement client/server round trips घटाता है और दिखाता है कि rows एक load का भाग हैं। हर parenthesized group में same column order के अनुसार same number of values होनी चाहिए।

student_idfull_nameclass_namemarksstatus
1AaravX-A86.50Active
2MeeraX-A91.00Active
3KabirX-B74.00Inactive
4SanaX-B88.50Active

Defaults, NULL और Omitted Columns

इन तीन forms का संबंध है, पर अर्थ समान नहीं:

-- status omit है, इसलिए default 'Active' लगेगा
INSERT INTO students (full_name, class_name, marks)
VALUES ('Ishaan', 'XI-A', 82.00);

-- marks की ज्ञात value उपलब्ध नहीं
INSERT INTO students (full_name, class_name, marks, status)
VALUES ('Naina', 'XI-A', NULL, 'Active');

-- DEFAULT declared default को explicitly माँगता है
INSERT INTO students (full_name, class_name, marks, status)
VALUES ('Vihaan', 'XI-B', 79.50, DEFAULT);
  • Omitted column: MySQL उसका default, automatic value या permitted होने पर NULL उपयोग करता है।
  • DEFAULT: declared column default explicitly माँगता है।
  • NULL: कोई known value नहीं; NOT NULL column में fail होता है।
  • Empty string: known zero-length string है, NULL नहीं।
इस fragile form से बचें: INSERT INTO students VALUES (...) physical column order पर depend करता और हर column की value माँगता है। Maintainable code में target columns लिखें।

SELECT से Rows Insert करें

INSERT ... SELECT query results को compatible destination में copy करता है। Archive, summary या migration tasks में यह उपयोगी है।

CREATE TABLE active_students LIKE students;

INSERT INTO active_students
  (student_id, full_name, class_name, marks, status, created_at)
SELECT
  student_id, full_name, class_name, marks, status, created_at
FROM students
WHERE status = 'Active';

SELECT output का column count और order INSERT column list से match होना चाहिए। Job दोबारा चल सकती है तो duplicate keys handle करने की intended policy पहले तय करें।

Production rule: SELECT को अलग preview करें, row count जाँचें और बड़े data move को controlled transaction या migration process में चलाएँ।

Results जाँचें और Errors समझें

SELECT student_id, full_name, class_name, marks, status
FROM students
ORDER BY student_id;

SELECT ROW_COUNT() AS rows_affected;

कई clients affected rows सीधे दिखाते हैं। ROW_COUNT() same session में relevant data-changing statement के तुरंत बाद चलना चाहिए।

समस्यासंभावित कारणसही कार्य
Column count और value count अलगRow group में values कम या अधिकहर position को column list से मिलाएँ
Duplicate entryPrimary या unique key पहले से हैExisting row खोजकर insert/update intentionally चुनें
Cannot be NULLNOT NULL column के लिए NULL या usable default नहींValid value दें या data model सुधारें
Data too long / out of rangeValue declared type या CHECK rule में fit नहींInput सुधारें; constraint blindly कमजोर न करें
Incorrect string valueCharacter set input represent नहीं करताCorrect utf8mb4 connection और schema लें

Application Safety: SQL Concatenation न करें

User द्वारा typed values को INSERT string में सीधे न जोड़ें। Language के MySQL driver से placeholders वाला prepared statement उपयोग करें:

INSERT INTO students
  (full_name, class_name, marks, status)
VALUES (?, ?, ?, ?);

Application values अलग bind करती है। Prepared statements input को SQL syntax की तरह interpret होने से रोकते और type handling स्पष्ट बनाते हैं। Application में business rules validate करें, database constraints बनाए रखें, least-privilege credentials लें और database errors visitors को न दिखाएँ।

अभ्यास

  1. Status और created_at को defaults पर छोड़ते हुए एक student insert करें।
  2. Explicit column list से एक statement में तीन books insert करें।
  3. Unknown marks वाले student को insert करें; बताएँ NULL zero से अधिक accurate क्यों है।
  4. Inactive students archive करने वाले INSERT ... SELECT को पहले preview, फिर write करें।
  5. Disposable database में CHECK या duplicate-key error पैदा करके बताएँ data किसने protect किया।

Stored rows पढ़ने के लिए अगला SELECT command lesson खोलें।

त्वरित सारांश

  • INSERT INTO ... (columns) VALUES (...) rows जोड़ता है।
  • Explicit column list हमेशा prefer करें।
  • Multi-row INSERT round trips घटाता और consistent column order रखता है।
  • Omitted columns, DEFAULT, NULL और empty strings के अलग अर्थ हैं।
  • Constraints quality और prepared statements application को protect करते हैं।

Official संदर्भ

References 14 August 2026 को review किए गए। Imports पहले copy पर test करें और valuable data का backup रखें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या INSERT में column list हमेशा लिखनी चाहिए?
Best practice के रूप में हाँ। Explicit column list intent बताती है, schema order बदलने पर कई errors रोकती है और values को गलत columns में जाने से बचाती है।
MySQL में एक statement से कई rows कैसे insert करें?
Column list वाला एक INSERT INTO लिखें, फिर VALUES के बाद comma-separated parenthesized row groups दें। MySQL इसे एक statement मानता है।
INSERT में NULL और DEFAULT में क्या अंतर है?
Column अनुमति दे तो NULL unknown या missing value store करता है। DEFAULT column का declared default माँगता है। Column omit करने पर भी defined default उपयोग होता है।
Insert के बाद AUTO_INCREMENT value कैसे मिलेगी?
उसी MySQL session में LAST_INSERT_ID() या database driver की equivalent method लें। अगली ID को MAX(id)+1 से calculate न करें।
INSERT duplicate key error क्यों देता है?
दी गई value table में पहले से stored PRIMARY KEY या UNIQUE constraint से टकराती है। Constraint हटाने के बजाय सही existing row या intended update workflow पहचानें।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.