Generate table DDL via a query on MySQL and SQL Server?


The DDL stands for Data Definition Language. To generate the table DDL via query, you can use show create command.

The syntax is as follows

SHOW CREATE TABLE yourTableName;

The above syntax is MySQL specific. Suppose, we have a table with the name ‘DDLOfTableStudent’.

First, create a table with the name ‘DDLOfTableStudent’. The query to create a table is as follows

mysql> create table DDLOfTableStudent
   -> (
   -> StudentId int,
   -> StudentFirstName varchar(100),
   -> StudentLastName varchar(100),
   -> StudentAddress varchar(200),
   -> StudentAge int,
   -> StudentMarks int
   -> );
Query OK, 0 rows affected (0.84 sec)

Use the above syntax to know the DDL of a table. The query is as follows

mysql> SHOW CREATE TABLE DDLOfTableStudent;

The following is the output displaying the DDL

+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table             | Create Table                                                                                                                                                                                                                                                                                                                                                   |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DDLOfTableStudent | CREATE TABLE `ddloftablestudent` (`StudentId` int(11) DEFAULT NULL,`StudentFirstName` varchar(100) DEFAULT NULL,`StudentLastName` varchar(100) DEFAULT NULL,`StudentAddress` varchar(200) DEFAULT NULL,`StudentAge` int(11) DEFAULT NULL,`StudentMarks` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements