在 MySQL 和 SQL Server 上通过查询生成表 DDL?
mysqlmysqli database
DDL 代表数据定义语言。要通过查询生成表 DDL,可以使用 show create 命令。
语法如下
SHOW CREATE TABLE yourTableName;
上述语法是 MySQL 特有的。假设我们有一个名为"DDLOfTableStudent"的表。
首先,创建一个名为"DDLOfTableStudent"的表。创建表的查询如下
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)
使用上面的语法可以了解表的DDL。查询如下
mysql> SHOW CREATE TABLE DDLOfTableStudent;
以下是显示 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)