
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How do I clone the structure of a table in MySQL?
To clone in MySQL, use LIKE clause. Let us first create a table −
mysql> create table DemoTable1 -> ( -> StudentId int, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(100,'John'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1 values(101,'Sam'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1 values(102,'Carol'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 100 | John | | 101 | Sam | | 102 | Carol | +-----------+-------------+ 3 rows in set (0.00 sec)
Following is the query to clone a table structure in MySQL −
mysql> create table DemoTable2 LIKE DemoTable1; Query OK, 0 rows affected (0.53 sec)
Let us check the structure of newly created table −
mysql> show create table DemoTable2;
Output
This will produce the following output −
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | DemoTable2 | CREATE TABLE `demotable2` (`StudentId` int(11) DEFAULT NULL, `StudentName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 OLLATE=utf8_unicode_ci | +--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.03 sec)
- Related Articles
- How can we make a MySQL clone table?
- How do I get the creation date of a MySQL table?
- How do I show the schema of a table in a MySQL database?
- How to clone a MySQL table, indexes, and data?
- How do I show unique constraints of a table in MySQL?
- How do I list all the columns in a MySQL table?
- How do I detect if a table exist in MySQL?
- How do I alter a MySQL table column defaults?
- How do I view the auto_increment value for a table in MySQL?
- How do I add a check constraint to a table in MySQL?
- How can we automatically define the structure of MySQL table same as the structure of another table?
- How do I get the current AUTO_INCREMENT value for a table in MySQL?
- How can we get the structure of a MySQL view as we can get the structure of a MySQL table?
- How do I remove a uniqueness constraint from a MySQL table?
- MySQL query to display structure of a table

Advertisements