SQL SQL Tutorial SQL Database



SQL Insert Into

Structured Query Language (SQL) is a programming language used to manage and manipulate relational databases. One of the most commonly used SQL statements is the INSERT INTO statement, which is used to add new data to a table in a database.

The INSERT INTO statement is used to insert new rows into a table. The syntax for the INSERT INTO statement is as follows:

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

The table_name is the name of the table where the data will be inserted. The column1, column2, column3, etc. are the names of the columns in the table where the data will be inserted. The VALUES keyword is used to specify the values that will be inserted into the table.

For example, let's say we have a table called "customers" with the following columns: customer_id, first_name, last_name, and email. We can use the INSERT INTO statement to add a new customer to the table:

INSERT INTO customers (customer_id, first_name, last_name, email)
VALUES (1, 'John', 'Doe', 'johndoe@example.com');

This statement will insert a new row into the "customers" table with the values 1 for customer_id, 'John' for first_name, 'Doe' for last_name, and 'johndoe@example.com' for email.

We can also use the INSERT INTO statement to insert multiple rows into a table at once. To do this, we simply separate each set of values with a comma:

INSERT INTO customers (customer_id, first_name, last_name, email)
VALUES (2, 'Jane', 'Doe', 'janedoe@example.com'),
       (3, 'Bob', 'Smith', 'bobsmith@example.com'),
       (4, 'Sara', 'Johnson', 'sarajohnson@example.com');

This statement will insert four new rows into the "customers" table with the values specified for each row.

It's important to note that the order of the columns in the INSERT INTO statement must match the order of the values specified in the VALUES clause. If we don't specify a value for a column, it will be set to NULL by default.

Additionally, we can use the INSERT INTO statement with a subquery to insert data from one table into another. For example, let's say we have a table called "orders" with the following columns: order_id, customer_id, order_date, and total_amount. We can use a subquery to insert data from the "customers" table into the "orders" table:

INSERT INTO orders (order_id, customer_id, order_date, total_amount)
SELECT order_id, customer_id, NOW(), 100
FROM customers
WHERE customer_id = 1;

This statement will insert a new row into the "orders" table with the values specified in the SELECT statement. The NOW() function is used to insert the current date and time into the "order_date" column, and the value 100 is inserted into the "total_amount" column.

In conclusion, the INSERT INTO statement is a powerful tool for adding new data to a table in a database. It allows us to insert single or multiple rows at once, and can be used with subqueries to insert data from one table into another.

References:

Activity