如何基于MySQL中的现有表生成“创建表”命令?
您可以借助SHOWCREATE命令基于MySQL中的现有表生成createtable命令。
语法如下
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 - > );
现在,使用以上语法生成一个createtable命令。
查询如下
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)