📅  最后修改于: 2023-12-03 14:47:35.105000             🧑  作者: Mango
When working with SQL databases, there are two commonly used statements for inserting data into a table: INTO
and INSERT INTO
. While both statements serve the same purpose, there are slight differences between them.
The INSERT INTO
statement is used to add new rows of data to an existing table in the database. It follows the syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Here, table_name
is the name of the table where the data will be inserted, and column1
, column2
, etc. are the names of the columns in the table. You then specify the corresponding values for each column in the VALUES
clause.
For example:
INSERT INTO employees (first_name, last_name, age)
VALUES ('John', 'Doe', 30);
This statement inserts a new row into the employees
table with the values 'John' for the first_name
column, 'Doe' for the last_name
column, and 30 for the age
column.
The SELECT INTO
statement is used to create a new table and populate it with data from an existing table. It follows the syntax:
SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE condition;
Here, column1
, column2
, etc. are the columns you want to include in the new table, new_table
is the name of the new table to be created, and existing_table
is the name of the table from which you want to extract the data. The optional WHERE
clause allows you to specify a condition for selecting specific rows.
For example:
SELECT first_name, last_name, age
INTO new_employees
FROM employees
WHERE age > 25;
This statement creates a new table named new_employees
and copies the columns first_name
, last_name
, and age
from the employees
table where the age is greater than 25.
In summary, INSERT INTO
is used to add new rows to an existing table, while SELECT INTO
is used to create a new table and populate it with data from an existing table. Understanding the differences between these two statements is essential for efficient data manipulation in SQL.
Note: The syntax and usage may vary slightly depending on the specific SQL database system you are using.