MySQL - SHOW CREATE TABLE Statement

This query shows/displays the statement used to create the specified table. This displays the create statements along with the clauses.
Following is the syntax of the SHOW CREATE TABLE statement −
SHOW CREATE TABLE [IF NOT EXISTS] table_name

Where, table_name is the name of the table.
Suppose we have created a database as shown below −
mysql> CREATE TABLE Employee(
   Name VARCHAR(255),
   Salary INT NOT NULL,
   Location VARCHAR(255)
);
Query OK, 0 rows affected (1.34 sec)
The following query displays the query used to create the database −
mysql> SHOW CREATE TABLE Employee;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                                                                                                         |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Employee | CREATE TABLE `employee` ( `ID` int NOT NULL, `Name` varchar(255) DEFAULT NULL, `Salary` int NOT NULL, `Location` varchar(255) DEFAULT NULL, `Address` varchar(50) DEFAULT NULL, `Phone` int DEFAULT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `con` (`Phone`), CONSTRAINT `MyPrimaryKey` FOREIGN KEY (`ID`) REFERENCES `test` (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci                    |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.07 sec)
If you try to create a table with an existing name an error will be generated −
mysql> CREATE TABLE Employee(ID int);
ERROR 1050 (42S01): Table 'employee' already exists
If you use the IF NOT EXISTS clause along with the CREATE statement as shown below a new table will be created and if a table with the given name, already exists the query will be ignored.
mysql> CREATE TABLE IF NOT EXISTS Employee(ID int);
Query OK, 0 rows affected, 1 warning (0.10 sec)
If the CREATE TABLE statement has an IF NOT EXISTS clause in it, then the result of the SHOW CREATE TABLE Statement of the respective database also contains the IF NOT EXISTS clause.
mysql> Show create table TestTable;
+-----------+-----------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                          | 
+-----------+-----------------------------------------------------------------------------------------------------------------------+
| TestTable | CREATE TABLE `testtable` (`ID` int DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci    |
+-----------+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page