Insert

The SQL INSERT statement is used to add new data records into a table in a relational database. The INSERT statement is one of the fundamental SQL commands and is essential for adding new data to a database.

Syntax

The basic syntax for the INSERT statement is as follows:

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

In this syntax, table_name is the name of the table where the new data will be inserted, and column1, column2, column3, etc. are the column names where the data will be inserted. The VALUES keyword is followed by the actual data values that will be inserted into the table.

Example

For example, suppose we have a table named “students” with columns named “name”, “age”, and “gender”. To insert a new record into this table, we could use the following INSERT statement:

INSERT INTO students (name, age, gender)
VALUES ('John', 22, 'Male');

This statement would insert a new record with the name “John”, age 22, and gender “Male” into the “students” table.

It’s important to note that the data types of the values being inserted must match the data types of the columns in the table. Additionally, if a column has a constraint that disallows null values, a value must be provided for that column in the INSERT statement.

The INSERT statement can also be used to insert multiple records into a table at once. To do this, multiple sets of values can be included in the VALUES clause, separated by commas:

INSERT INTO students (name, age, gender)
VALUES ('John', 22, 'Male'),
       ('Sarah', 19, 'Female'),
       ('David', 25, 'Male');

This statement would insert three new records into the “students” table, one for each set of values provided in the VALUES clause.

In summary, the SQL INSERT statement is a powerful tool for adding new data to a database. By specifying the table and column names, along with the corresponding values, new records can be easily and efficiently added to a database.