SQL Tables COPY TABLE
The COPY TABLE
command in SQL is used to create a copy of an existing table. It copies the structure and data of the original table to the new table.
Syntax
The basic syntax for copying a table is:
CREATE TABLE new_table_name AS
SELECT * FROM original_table_name;
Here, new_table_name
is the name of the new table you want to create and original_table_name
is the name of the table you want to copy.
Example
Let's say we have a table named employees
with the following data:
employee_id | first_name | last_name | |
---|---|---|---|
1000 | John | Doe | john.doe@example.com |
1001 | Jane | Smith | jane.smith@example.com |
Now, we want to create a copy of this table named employees_copy
. We can use the following query:
CREATE TABLE employees_copy AS
SELECT * FROM employees;
Output
If the query is executed successfully, a new table named employees_copy
will be created with the same structure and data as the employees
table.
Explanation
The CREATE TABLE
statement is used to create a new table, with the name specified after the AS
keyword. The SELECT
statement inside the parentheses copies all the columns and rows from the original table into the new table.
Use
Copying a table can be useful in many scenarios. For example, you may want to make a backup copy of a table before making changes to it. You can also use this command to create a new table with similar data as an existing one.
Important Points
- The new table created using the
COPY TABLE
command will have the same columns and data types as the original table - The new table will also inherit all the constraints, indexes, and other properties of the original table
Summary
COPY TABLE
is a SQL command used to create a copy of an existing table- The syntax for copying a table is
CREATE TABLE new_table_name AS SELECT * FROM original_table_name;
- The new table will have the same columns, data types, and properties as the original table
- This command can be useful for creating backups, testing changes, or creating new tables with similar data.