
- 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 to generate a “create table” command based on an existing table in MySQL?
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)
- Related Articles
- Create MySQL query to create a table from an existing table?
- How can we create a table from an existing MySQL table in the database?
- How to create a MySQL table based on JDBC Result Set?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- Create Primary Key on an existing table in PostgreSQL?
- How can we add multiple columns, with single command, to an existing MySQL table?
- How to rename a column in an existing MySQL table?
- How to add columns to an existing MySQL table?
- How to add current date to an existing MySQL table?
- Show constraints on table command in MySQL?
- How to write PHP script to update an existing MySQL table?
- How can we create a MySQL view based on another existing view?
- Rename a table in MySQL using RENAME TABLE command
- Update a table based on StudentId value in MySQL?
- Create index on create table in MySQL?

Advertisements