MySQL - SHOW CREATE TABLE Statement



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.

Syntax

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.

Example

Suppose we have created a database as shown below −

CREATE TABLE Employee(
   Name VARCHAR(255),
   Salary INT NOT NULL,
   Location VARCHAR(255)
);

The following query displays the query used to create the database −

SHOW CREATE TABLE Employee;

Output

After executing the above, it generates the following output −

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

The IF NOT EXISTS clause

If you try to create a table with an existing name an error will be generated −

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.

CREATE TABLE IF NOT EXISTS Employee(ID int);

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.

Show create table TestTable;

Output

Following is the output of the above query −

Table Create Table
TestTable CREATE TABLE `testtable` (`ID` int DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Advertisements