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

INSERT INTO Command

What INSERT Does

INSERT adds new rows to a table. It is a data manipulation language statement: the table structure already exists, and the statement supplies values that must satisfy its data types and constraints.

Prerequisite: create the coding_school.students table from the CREATE DATABASE and CREATE TABLE lesson. The examples below deliberately use the same columns and data in every foundation page.

The most reliable basic form is:

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

Columns and values match by position. Text and date literals use single quotes. Numeric literals do not. SQL keywords can be uppercase for readability.

Insert One Row

USE coding_school;

INSERT INTO students
  (full_name, class_name, marks, status)
VALUES
  ('Aarav', 'X-A', 86.50, 'Active');
Typical result Query OK, 1 row affected

student_id is omitted because MySQL generates it with AUTO_INCREMENT. created_at is omitted so its CURRENT_TIMESTAMP default runs. The explicit column list makes both decisions visible.

SELECT LAST_INSERT_ID() AS new_student_id;

Run LAST_INSERT_ID() in the same session immediately after the successful insert. Application drivers normally expose the generated ID directly.

Insert Multiple Rows Efficiently

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');
Typical result Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0

A multi-row statement reduces client/server round trips and clearly shows that the rows belong to one load. Every parenthesized group must contain the same number of values in the same column order.

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

Defaults, NULL and Omitted Columns

These three forms are related but not identical:

-- status is omitted, so its default 'Active' is used
INSERT INTO students (full_name, class_name, marks)
VALUES ('Ishaan', 'XI-A', 82.00);

-- marks explicitly has no known value
INSERT INTO students (full_name, class_name, marks, status)
VALUES ('Naina', 'XI-A', NULL, 'Active');

-- DEFAULT explicitly requests the declared default
INSERT INTO students (full_name, class_name, marks, status)
VALUES ('Vihaan', 'XI-B', 79.50, DEFAULT);
  • Omitted column: MySQL uses its default, an automatic value or NULL if permitted.
  • DEFAULT: explicitly requests the column default.
  • NULL: stores no known value and fails if the column is NOT NULL.
  • Empty string: is a known zero-length string, not NULL.
Avoid this fragile form: INSERT INTO students VALUES (...). It depends on the physical column order and requires values for every column. Always name the target columns in maintainable code.

Insert Rows from a SELECT Query

INSERT ... SELECT copies query results into a compatible destination. It is useful for archive, summary or 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';

The SELECT output column count and order must match the INSERT column list. If this job may run again, decide how duplicate keys should be handled instead of assuming the second run is safe.

Production rule: preview the SELECT by itself, check the row count and run large data moves inside a controlled transaction or migration process.

Verify Results and Understand Errors

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

SELECT ROW_COUNT() AS rows_affected;

Many clients already display affected rows. ROW_COUNT() must be called immediately after the relevant data-changing statement in the same session.

ProblemLikely causeCorrect response
Column count does not match value countA row group has too few or too many valuesCompare each value position with the column list
Duplicate entryPrimary or unique key already existsFind the existing row and choose insert or update intentionally
Cannot be NULLNULL or no usable default for a NOT NULL columnSupply a valid value or correct the data model
Data too long / out of rangeValue does not fit the declared type or CHECK ruleFix the input; do not weaken constraints blindly
Incorrect string valueCharacter set cannot represent the inputUse a correct utf8mb4 connection and schema

Application Safety: Never Build SQL by Concatenation

Values typed by users must not be joined directly into an INSERT string. Use a prepared statement with placeholders through your language's MySQL driver:

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

The application binds values separately. Prepared statements prevent input from being interpreted as SQL syntax and also make type handling clearer. Validate business rules in the application, keep database constraints, use least-privilege credentials and do not expose database errors to visitors.

Practice

  1. Insert one new student while allowing status and created_at to use defaults.
  2. Insert three books in one statement using an explicit column list.
  3. Insert a student whose marks are unknown; explain why NULL is more accurate than zero.
  4. Preview and then write an INSERT ... SELECT that archives inactive students.
  5. Cause a CHECK or duplicate-key error in a disposable database and explain what protected the data.

Next, read the stored rows with the SELECT command.

Quick Summary

  • INSERT INTO ... (columns) VALUES (...) adds rows.
  • Always prefer an explicit column list.
  • Multi-row INSERT reduces round trips and uses one consistent column order.
  • Omitted columns, DEFAULT, NULL and empty strings have different meanings.
  • Constraints protect quality; prepared statements protect applications.

Official References

References reviewed on 14 August 2026. Test imports on a copy and back up valuable data first.

Frequently Asked Questions

Should I always write the column list in INSERT?
Yes, as a best practice. An explicit column list documents intent, survives many schema-order changes and prevents values from silently going into the wrong columns.
How do I insert multiple rows in one MySQL statement?
Write one INSERT INTO with a column list, followed by VALUES and comma-separated parenthesized row groups. MySQL treats it as one statement.
What is the difference between NULL and DEFAULT in INSERT?
NULL stores no known value when the column allows it. DEFAULT requests the column default. Omitting the column also uses its default when one is defined.
How do I get the AUTO_INCREMENT value after an insert?
In the same MySQL session, use LAST_INSERT_ID(), or use the equivalent method provided by your database driver. Do not calculate the next ID with MAX(id)+1.
Why does INSERT fail with a duplicate key error?
A supplied value conflicts with a PRIMARY KEY or UNIQUE constraint already stored in the table. Find the correct existing row or choose the intended update workflow instead of deleting the constraint.
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.