Introduction The INSERT statement is used in SQL to insert new records or data into a table. It allows you to add one or multiple rows of data into a specific table.
Syntax The basic syntax of the INSERT statement is as follows:
sqlINSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Explanation
- The
INSERT INTO
clause is used to specify the name of the table where you want to insert the data. - In parentheses, you specify the columns in which you want to insert values.
- The
VALUES
keyword is used to specify the actual values that you want to insert into the corresponding columns. - Each value is enclosed within parentheses and separated by commas.
Example Let's assume we have a table called "employees" with the following structure:
sqlCREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
salary DECIMAL(10,2)
);
To insert a new employee into the table, you can use the following INSERT statement:
sqlINSERT INTO employees (id, name, age, salary)
VALUES (1, 'John Doe', 30, 5000.00);
In this example, we inserted a new row with values for the id
, name
, age
, and salary
columns. The id
is set to 1, the name
is set to 'John Doe', the age
is set to 30, and the salary
is set to 5000.00.
You can also insert multiple rows at once by specifying multiple sets of values separated by commas:
sqlINSERT INTO employees (id, name, age, salary)
VALUES (2, 'Jane Smith', 28, 4500.00),
(3, 'Mike Johnson', 35, 6000.00),
(4, 'Emily Davis', 32, 5500.00);
In this example, we inserted three new rows into the "employees" table.
Conclusion The INSERT statement is a fundamental SQL operation used to add new data into a table. By specifying the table name, columns, and corresponding values, you can effectively insert one or multiple rows of data into your database.
0 Comments