Notes

`INSERT INTO` Query in DBMS (MySQL) [ English ]

< Prev Next >

Introduction

In a Database Management System (DBMS), tables are used to store data in rows and columns. After creating a table, the next step is to add records to it. The INSERT INTO command is used to insert new data into a table.

This command allows users to add one or more rows of data to a table by specifying the values for each column.

Definition

The INSERT INTO query is a Data Manipulation Language (DML) command used to add new records (rows) into a table in a database.

Syntax

Method 1: Inserting Data by Specifying Column Names

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Method 2: Inserting Data Without Specifying Column Names

If values are provided for all columns in the correct order, column names can be omitted.

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

How It Works

When the INSERT INTO command is executed:

  1. The DBMS checks whether the specified table exists.
  2. It verifies the column names and data types.
  3. The provided values are inserted as a new row in the table.
  4. The table is updated with the new record.

Example

Assume we have a table named students.

id name age course

Insert a new record:

INSERT INTO students (id, name, age, course)
VALUES (1, 'Rahul', 20, 'BCA');

After inserting the record, the table will look like this:

id name age course
1 Rahul 20 BCA

Inserting Multiple Rows

MySQL also allows inserting multiple rows in a single query.

INSERT INTO students (id, name, age, course)
VALUES
(2, 'Anita', 21, 'BBA'),
(3, 'Rohit', 19, 'BCA'),
(4, 'Priya', 22, 'BCom');

After executing the command, the table will contain multiple records.

Important Rules

Example:

'Rahul'

Key Points

Summary

The INSERT INTO command is used to add new records to a table in a database. It allows users to insert data into specific columns or all columns of a table. This command plays an important role in storing and managing data within a DBMS.

< Prev Next >