Insert Query in MySQL

Introduction

After creating a table, the next step is to add data to it.

In MySQL, this is done using the INSERT query.

The INSERT query allows you to add one or more records (rows) into a table.


What is an INSERT Query

The INSERT INTO statement is used to insert new data into a table.

You can:

  • Insert a single row
  • Insert multiple rows
  • Insert data into specific columns

Basic Syntax

INSERT INTO table_name (column1, column2, column3)

VALUES (value1, value2, value3);


Example

INSERT INTO Students (id, name, age)

VALUES (1, 'Rahul', 20);

This inserts a new row into the Students table.


Insert Without Specifying Columns

You can also insert data without mentioning column names (not recommended):

INSERT INTO Students

VALUES (2, 'Aisha', 21);

Important:
Values must be in the same order as table columns.


Insert Multiple Rows

You can insert multiple records in a single query:

INSERT INTO Students (id, name, age)

VALUES

(3, 'Aman', 22),

(4, 'Neha', 19);


Insert Partial Data

You can insert values into specific columns:

INSERT INTO Students (id, name)

VALUES (5, 'Riya');

Other columns will take NULL or default values.


Example Scenario

Suppose you have a table:

CREATE TABLE Students (

id INT,

name VARCHAR(50),

age INT

);

You can insert data like:

INSERT INTO Students (id, name, age)

VALUES (1, 'Rahul', 20);


Common Mistakes

  • Missing column values
  • Wrong data types
  • Not matching column order
  • Forgetting quotes for text values

Key Points to Remember

  • INSERT INTO is used to add data
  • You can insert one or multiple rows
  • Always match columns with values
  • Prefer specifying column names
  • Helps populate tables with data