The INSERT INTO statement is used to add new rows of data into an existing table. It is one of the most fundamental commands in SQL, and is part of Data Manipulation Language (DML).
Here's the basic syntax of the INSERT INTO
statement:
INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3);
Explanation: This syntax is used when you want to insert data into specific columns of a table.
Let's assume you have a table called Employees with columns EmployeeID, EmployeeName, and Department.
-- Inserting a new employee record INSERT INTO Employees (EmployeeID, EmployeeName, Department) VALUES (1, 'John Doe', 'HR');
This statement inserts one record with the values (1, 'John Doe', 'HR') into the Employees table.
You can also insert multiple rows of data into the table at once. Here's an example:
-- Inserting multiple employee records INSERT INTO Employees (EmployeeID, EmployeeName, Department) VALUES (2, 'Jane Smith', 'Finance'), (3, 'Emily Davis', 'IT'), (4, 'Michael Brown', 'Marketing');
This will insert three records into the Employees table in a single statement.
Letβs simulate an interactive example where you can experiment with inserting data into a table.
Imagine you are managing a Books
table. The columns are:
Now, your task is to insert a new book record with the following details:
Write the INSERT INTO
statement below:
INSERT INTO
statement matches the order of values in the VALUES
clause.INSERT INTO
statement by separating each set of values with commas.NULL
values, you can omit the value in the VALUES
clause.The INSERT INTO statement is essential for adding new data to your database. You can insert single or multiple records with a simple syntax. Make sure to follow the correct column order and data types when inserting records!
Quick Tip:
Use the VALUES
clause with caution. Always ensure that the number of values matches the number of columns you're inserting into!
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!