How to create a new table from existing table in SQL
How to create a new table from existing table in SQL
With this article, we’ll look at some examples of how to address the "How to create a new table from existing table in SQL" problem.
There are lots of Queries available in SQL to create a new table from another table. You can create a new table structure from an existing table in the database well as you can copy data from the existing table to the new table.-
Create a new table structure with another table in SQL
CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2);
This SQL query will create a new table with the structure of another table in SQL. This Query will only create the table structure but do not copy data from another table that you are used to creating the new one.
It will not copy indexes and keys.
-
Create a new table structure with keys and indexes using another table in SQL
CREATE TABLE new_table LIKE existing_table;
This SQL query creates a new table structure with keys and indexes from another existing table.
-
Create a new table structure with data from another table in SQL
CREATE TABLE new_table LIKE existing_table; INSERT INTO new_table SELECT * FROM existing_table;
This Query will create a new table structure and insert data in it from the existing table in SQL. The first query (CREATE TABLE new_table LIKE existing_table) is used to create the table structure with keys and indexes.
Second query (INSERT INTO new_table SELECT * FROM existing_table;) is used to insert data into the table (newly created table) from the existing table.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Sql
- Create Employee table in MySQL
- SQLSTATE[01000]: Warning: 1265 Data truncated for column 'visibility' at row 1
- Cannot drop index 'fk_role_id': needed in a foreign key constraint
- How to show database tables in mysql using command line
- 1553 Cannot drop index 'posts_user_id_foreign': needed in a foreign key constraint
- How to get a list of MySQL views?
- Check constraint in MySQL
- How to Rename column name in MySql ?
- ERROR 3780 (HY000): Referencing column 'order_id' and referenced column 'id' in foreign key constraint 'order_items_ibfk_1' are incompatible.
- How to create view in MySQL database
- Change existing MySQL table column id to autoincrement
- SQL query to delete records older than 6 months
- Difference between SQL and NoSQL databases
- How to get the list of all constraints in MySQL database
- Get comma separated ids from table using MySQL
- Get full name of a person in SQL using function
- Get number of connections being used by a specific user in MySQL
- MySQL create procedure example
- How to get yesterday's date in MySQL ?
- How to create function in MySQL ?
- Clear the terminal screen in MySQL within the Windows powershell
- How to drop function in MySQL ?
- Create Index in MySQL with example
- Create column after a column in existing table using query in MySQL
- How to check column exists or not in table using MySQL