INSERT Command
The INSERT command in SQL is used to add new records (rows) into a database table.
Whenever you want to store new data in a table, such as adding a new student, employee, or product, the INSERT command is used.
Example: Student Table
Syntax
Inserting Data into Specific Columns
Result
A new row is added to the table.
Inserting Data Without Column Names
If values are provided in the exact order of columns, column names can be omitted.
Inserting Multiple Rows at Once
SQL allows inserting multiple records in a single query.
Rules for Best Practices
Important: Unlike the SELECT command, INSERT modifies the data by adding new rows.
Understanding INSERT with a Simple Concept
A database table can be compared to a register or spreadsheet.
- Each row represents one complete record
- Each column represents a specific field
Using INSERT is similar to adding a new entry at the bottom of a register.
Example: Student Table
| id | name | marks |
|---|---|---|
| 1 | Rahul | 85 |
| 2 | Anjali | 90 |
Why Use the INSERT Command?
- The INSERT command is used to:
- Add new records to a table
- Store user input data
- Save form or application data
- Populate tables with initial data
Syntax
INSERT INTO table_name (column1, column2, column3)VALUES (value1, value2, value3);- INSERT INTO specifies the table where data will be added
- Column names define where values will be inserted
- VALUES contains the actual data
Inserting Data into Specific Columns
INSERT INTO students (id, name, marks)VALUES (3, 'Amit', 88);Result
A new row is added to the table.
| id | name | marks |
|---|---|---|
| 1 | Rahul | 85 |
| 2 | Anjali | 90 |
| 3 | Amit | 88 |
Inserting Data Without Column Names
If values are provided in the exact order of columns, column names can be omitted.
INSERT INTO studentsVALUES (4, 'Neha', 92);Note: This method is not recommended in real projects because it depends on column order.
Inserting Multiple Rows at Once
SQL allows inserting multiple records in a single query.
INSERT INTO students (id, name, marks)VALUES(5, 'Ravi', 80),(6, 'Pooja', 95);Rules for Best Practices
- The number of values must match the number of columns
- Data types of values must match column data types
- Text values must be enclosed in single quotes
- Use column names for better readability and safety
Two Minute Drill
- INSERT is used to add new records to a table
- INSERT INTO specifies the target table
- VALUES keyword contains actual data
- Columns should be specified for safety
- Multiple rows can be inserted in one query
- INSERT permanently changes table data