Create a duplicate of the MySQL 'countries' table named 'country_new' with all structure and data
MySQL insert into Statement: Exercise-3 with Solution
3. Write a MySQL query to create duplicate of countries table named country_new with all structure and data.
Here is the structure of the table "countries".
+--------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------------+------+-----+---------+-------+ | COUNTRY_ID | varchar(2) | YES | | NULL | | | COUNTRY_NAME | varchar(40) | YES | | NULL | | | REGION_ID | decimal(10,0) | YES | | NULL | | +--------------+---------------+------+-----+---------+-------+
Sample Solution:
-- Creating a new table 'country_new' if it doesn't already exist
CREATE TABLE IF NOT EXISTS country_new
-- Copying all records from the 'countries' table into the new table
AS SELECT * FROM countries;
Let execute the above code in MySQL command prompt.
Here is the structure of the table:
mysql> SHOW COLUMNS FROM country_new; +--------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------------+------+-----+---------+-------+ | COUNTRY_ID | varchar(8) | YES | | NULL | | | COUNTRY_NAME | varchar(40) | YES | | NULL | | | REGION_ID | decimal(10,0) | YES | | NULL | | +--------------+---------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> SELECT * FROM country_new; +------------+--------------+-----------+ | COUNTRY_ID | COUNTRY_NAME | REGION_ID | +------------+--------------+-----------+ | C1 | India | 1001 | +------------+--------------+-----------+ 1 row in set (0.00 sec)
Explanation:
The above MySQL code creates a new table named 'country_new' if it doesn't already exist and copies all records from the existing 'countries' table into the new table using a SELECT statement.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:Write a MySQL query to create duplicate of countries table named country_new with all structure and data.
Next:Write a MySQL query to insert one row into the table countries against the column country_id and country_name.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mysql-exercises/insert-into-statement/insert-into-statement-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics