â The INSERT statement in SQL is used to add new records (rows) into a database table. It is one of the most commonly used Data Manipulation Language (DML)statements and allows you to insert a single row, multiple rows, or even the result of another query into a table.
đ What is the INSERT Statement?
The INSERT statement creates new records by supplying values for one or more columns in a table. If required columns are omitted, the database uses their default values (if defined) or returns an error if the columns do not allow NULL values.
Information
đ¯ Why Use INSERT?
The INSERT statement is essential for storing new information in a database.
- đ Add new customer, employee, or student records.
- đ Populate newly created tables.
- đ Import data from other tables.
- đ Insert multiple records efficiently.
- đ Store application-generated data.
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Age | Marks |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 20 | 92 |
| 102 | Bob | Mathematics | 21 | 85 |
đ Basic Syntax
INSERT Syntax
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);đĄ Insert a Single Row
Insert one new student into the Students table.
Insert One Record
INSERT INTO Students
(StudentID, Name, Department, Age, Marks)
VALUES
(103, 'Charlie', 'Physics', 22, 78);đ Table After Insert
| StudentID | Name | Department | Age | Marks |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 20 | 92 |
| 102 | Bob | Mathematics | 21 | 85 |
| 103 | Charlie | Physics | 22 | 78 |
đĨ Insert Without Specifying Column Names
If values are supplied for every column in the correct order, the column list can be omitted.
Insert All Columns
INSERT INTO Students
VALUES
(104, 'David', 'Computer Science', 20, 95);Important
đ Insert Multiple Rows
Many SQL databases allow multiple rows to be inserted using a single INSERT statement.
Insert Multiple Records
INSERT INTO Students
(StudentID, Name, Department, Age, Marks)
VALUES
(105, 'Emma', 'Biology', 21, 88),
(106, 'Frank', 'Chemistry', 22, 81),
(107, 'Grace', 'Mathematics', 20, 91);đ Insert Selected Columns
You can insert values into only selected columns. The remaining columns use their default values or NULL, depending on the table definition.
Insert Specific Columns
INSERT INTO Students
(StudentID, Name, Department)
VALUES
(108, 'Henry', 'Physics');đ INSERT INTO ... SELECT
Use INSERT INTO ... SELECT to copy data from one table into another.
Insert Data from Another Table
INSERT INTO GraduateStudents
(StudentID, Name, Department)
SELECT StudentID,
Name,
Department
FROM Students
WHERE Marks >= 90;đ INSERT with Default Values
If a column has a default value, you can omit it or explicitly use the DEFAULT keyword (where supported by your database).
Insert Using DEFAULT
INSERT INTO Students
(StudentID, Name, Department, Age, Marks)
VALUES
(109, 'Ivy', 'Physics', DEFAULT, 84);âī¸ INSERT vs UPDATE
| Feature | INSERT | UPDATE |
|---|---|---|
| Purpose | Adds new rows. | Modifies existing rows. |
| Affects Existing Data | â No | â Yes |
| Creates New Records | â Yes | â No |
đŧ Real-World Example
A university admission system needs to add newly enrolled students to the database every day.
Add a New Student
INSERT INTO Students
(StudentID, Name, Department, Age, Marks)
VALUES
(110, 'Jack', 'Computer Science', 19, 89);Each successful execution adds a new student record to the database.
â ī¸ Common Mistakes
- â Providing values in the wrong column order.
- â Supplying more or fewer values than required.
- â Inserting duplicate values into a primary key column.
- â Omitting required columns that do not allow NULL values and have no default value.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ INSERT adds new rows to a table.
- đ You can insert one row or multiple rows at once.
- đ Specifying column names is recommended.
- đ INSERT INTO ... SELECT copies data from another table.
- đ Default values are used when applicable.
- đ INSERT creates new records, while UPDATE modifies existing ones.