How can Oracle save query results as a new table?
In Oracle, you can save the results of a query as a new table using the CREATE TABLE AS SELECT statement.
Here are the specific steps:
- Write a query statement to retrieve the necessary data to be saved. For example, here is a sample query statement:
SELECT * FROM employees WHERE department_id = 100;
- Create a new table by saving the query results with the CREATE TABLE AS SELECT statement. Add the INTO keyword at the end of the SELECT statement, followed by the name of the new table. For example, the following is an example statement that saves the query results as a new table named new_employees:
CREATE TABLE new_employees AS
SELECT * FROM employees WHERE department_id = 100;
- By executing the above statement, the new table new_employees will be created and the query results will be saved.
Important points to note:
- The name of the new table must be unique in the database and cannot be the same as any existing table.
- The structure of the new table will be identical to the query results, including column names, data types, and so on.
- If further operations are required on the new table, such as adding indexes or constraints, you can use the ALTER TABLE statement to perform the corresponding actions.