In SQL,SELECT INTO
is a common operation used to insert query results into a new table. Its main purpose is to quickly copy table structure and data, and it is suitable for backup, data migration, temporary table creation and other scenarios. Different database management systems (DBMS) pairsSELECT INTO
The support methods are different, this article will discussSELECT INTO
The usage and support situation in different databases.
1. Basic usage of SELECT INTO
SELECT INTO
Syntax is usually used to insert query results into a new table. If the target table does not exist,SELECT INTO
The table will be automatically created and the result of the query is inserted into it.
1. Create a new table and insert data
SELECT column1, column2, ... INTO new_table FROM old_table WHERE condition;
-
new_table
It is a new table to be created, which will automatically create the corresponding structure based on the columns of the query result. -
old_table
It is the source table, and the query will extract data from this table. -
condition
is an optional filter condition to filter data that needs to be inserted into a new table.
2. No filtering conditions
If not specifiedWHERE
clause,SELECT INTO
All data in the source table will be copied to the new table.
SELECT * INTO new_table FROM old_table;
This query willold_table
All data in copy tonew_table
and automatically create the table structure.
2. Application scenarios of SELECT INTO
SELECT INTO
Very useful in the following scenarios:
Quickly create backups of tables: You can quickly copy the structure and data of one table into another table as a backup or for disaster recovery.
SELECT * INTO backup_table FROM original_table;
Temporary data storage: You can use it when performing complex queries or processing.SELECT INTO
Store intermediate results in temporary tables to avoid repeated calculations.
SELECT customer_id, COUNT(*) AS order_count INTO temp_orders_summary FROM orders GROUP BY customer_id;
Data migration: Migrate data from one table to another, especially when filtering, transforming, or cleaning up the data.
SELECT * INTO new_customers FROM customers WHERE registration_date > '2024-01-01';
Data Cleaning and Transformation: BySELECT INTO
Filter and convert the data in the original data table into a new table. For example, remove unwanted fields or format data when data is cleaned.
SELECT product_id, UPPER(product_name) AS product_name_upper INTO clean_product_names FROM products WHERE product_name IS NOT NULL;
3. Support of SELECT INTO by different databases
AlthoughSELECT INTO
is a common SQL operation, but different database management systems vary in support level and syntax details. Here are some common database pairsSELECT INTO
Support status.
1. SQL Server
SQL Server is rightSELECT INTO
Supports one of the most complete databases. It allows users to use it directlySELECT INTO
Create a new table and insert the query results.
grammar:
SELECT * INTO new_table FROM old_table;
Features:
- Create a new table directly and insert data.
- The indexes, constraints, and triggers of the table are not copied, only the table structure and data.
- Supports large-scale data insertion, suitable for fast backups and data migration.
2. PostgreSQL
PostgreSQL does not supportSELECT INTO
Create a new table, but it provides similar functionality usingCREATE TABLE AS
Syntax instead. andSELECT INTO
different,CREATE TABLE AS
Allows more precise control and supports data filtering and conversion.
grammar:
CREATE TABLE new_table AS SELECT * FROM old_table;
Features:
-
CREATE TABLE AS
Support andSELECT INTO
Same function. - Allows users to create new tables through queries and insert query results into new tables.
- You can add it in the query
WHERE
clause to filter data.
3. MySQL
MySQL is not supportedSELECT INTO
Used to create new tables, but useCREATE TABLE ... AS
grammar. These two syntaxes are functionally equivalent, the difference is thatCREATE TABLE ... AS
More general.
grammar:
CREATE TABLE new_table AS SELECT * FROM old_table;
Features:
- Suitable for quickly creating new tables and inserting data.
- The constraints, indexes, etc. of the table will not be copied, only the data and column structure will be copied.
- It is more efficient for large-scale data processing.
4. SQLite
SQLite SupportSELECT INTO
Syntax, but it is usually recommended to useCREATE TABLE AS
Syntax to achieve the same function.
grammar:
CREATE TABLE new_table AS SELECT * FROM old_table;
Features:
- Same as PostgreSQL and MySQL, SQLite uses
CREATE TABLE AS
to create a new table and insert data. - Suitable for lightweight database and small-scale data operations.
5. Oracle
Oracle does not support it directlySELECT INTO
Grammar, but useCREATE TABLE AS
to create a new table and insert data.
grammar:
CREATE TABLE new_table AS SELECT * FROM old_table;
Features:
- The syntax is similar to PostgreSQL and MySQL.
- The created new table will not contain the index, constraints, etc. of the original table.
4. Limitations and precautions of SELECT INTO
- No copying of indexes and constraints:
SELECT INTO
The created table does not include the index, foreign key constraints, triggers, etc. of the original table. For tables with complex constraints, indexes and constraints need to be created manually. - Data type support: Different databases support different data types.
SELECT INTO
The behavior may be limited by the data type. - Big data set performance: for big data sets,
SELECT INTO
Operations may affect performance, especially if no optimization is done (such as no indexing is disabled or batch processing is performed).
5. Summary
SELECT INTO
It is a powerful SQL syntax that can quickly copy data and table structures, and is suitable for backup, data migration, temporary data storage and other scenarios. Different database pairsSELECT INTO
The support for different SQL Server is the most direct, while PostgreSQL, MySQL and Oracle useCREATE TABLE AS
Syntax to implement similar functions.
In useSELECT INTO
At the time, developers should select appropriate syntax based on the characteristics of the specific database and consider factors such as indexing and constraints to improve operational efficiency and stability. For large-scale datasets, performance optimizations may be required, such as batch insertion or using batch data import tools.
This is the end of this article about the usage of SELECT INTO and the supported databases. For more related content on SELECT INTO usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!