如何在 MySQL 中根据现有表生成“create table”命令?
你可以使用 SHOW CREATE 命令来根据 MySQL 中的现有表生成一个创建表命令。
语法如下
SHOW CREATE TABLE yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下
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)
现在使用上述语法来生成一个创建表命令。
查询如下
mysql> SHOW CREATE TABLE StudentInformation;
输出如下
+--------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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)
广告