How to generate a “create table” command based on an existing table in MySQL?

MySQLMySQLi Database

You can generate a create table command based on an existing table in MySQL with the help of SHOW CREATE command.

The syntax is as follows

SHOW CREATE TABLE yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table StudentInformation
   - > (
   - > StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   - > StudentName varchar(20),
   - > StudentAge int DEFAULT 18,
   - > StudentRollNo int,
   - > StudentAddress varchar(200),
   - > StudentMarks int,
   - > StudentDOB datetime,
   - > StudentAdmissionDate datetime
   - > );
Query OK, 0 rows affected (0.54 sec)

Now use the above syntax to generate a create table command.

The query is as follows

mysql> SHOW CREATE TABLE StudentInformation;

The following is the output

+--------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| StudentInformation | CREATE TABLE `studentinformation` (
`StudentId` int(11) NOT NULL AUTO_INCREMENT,
`StudentName` varchar(20) DEFAULT NULL,
`StudentAge` int(11) DEFAULT '18',
`StudentRollNo` int(11) DEFAULT NULL,
`StudentAddress` varchar(200) DEFAULT NULL,
`StudentMarks` int(11) DEFAULT NULL,
`StudentDOB` datetime DEFAULT NULL,
`StudentAdmissionDate` datetime DEFAULT NULL,
PRIMARY KEY (`StudentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.04 sec)
raja
Published on 19-Mar-2019 14:43:56
Advertisements