How to Generate a CREATE TABLE Script for an Existing Table in MySQL

In MySQL, we can use the SHOW CREATE TABLE statement to generate a CREATE TABLE script for existing tables. This enables us to recreate the table without having to manually type out the table’s definition.

Example

Here’s an example to demonstrate:

SHOW CREATE TABLE Orders;

Result:

+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Orders | CREATE TABLE `Orders` (
  `OrderId` int NOT NULL,
  `OrderDate` datetime NOT NULL,
  `CustomerId` int NOT NULL,
  PRIMARY KEY (`OrderId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Any CHECK constraints are displayed as a table constraint. In other words, a CHECK constraint originally specified as part of a column definition displays as a separate clause not part of the column definition.