Xem câu lệnh CREATE TABLE (DDL) của bảng hiện có
MySQL cung cấp SHOW CREATE TABLE để sinh ra DDL hoàn chỉnh bao gồm engine, charset, collation, index, khóa ngoại.
Cú pháp MySQL
SHOW CREATE TABLE tenBang;
Ví dụ
Tạo bảng DDLOfTableStudent:
CREATE TABLE DDLOfTableStudent (
StudentId INT,
StudentFirstName VARCHAR(100),
StudentLastName VARCHAR(100),
StudentAddress VARCHAR(200),
StudentAge INT,
StudentMarks INT
);
Xem DDL:
mysql> SHOW CREATE TABLE DDLOfTableStudent;
+-------------------+----------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------+
| DDLOfTableStudent | CREATE TABLE `ddloftablestudent` (`StudentId` int(11) DEFAULT NULL,`StudentFirstName` varchar(100) DEFAULT NULL, ...) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------+
SQL Server: sp_helptext hoặc OBJECT_DEFINITION
SQL Server không có SHOW CREATE TABLE. Dùng:
-- Cách 1: sp_helptext (cho view/proc, không dùng cho table)
-- Cách 2: Script table trong SSMS (Right-click > Script Table as > CREATE To)
-- Cách 3: T-SQL generate script (phức tạp, dùng SMO/PowerShell)
MySQL SHOW CREATE TABLE cực kỳ tiện cho:
- Backup schema.
- Recreate bảng trên server khác.
- Kiểm tra engine, charset, partition, foreign key.
- Migration tool tự động.
Mẹo: Chỉ lấy phần SQL
SHOW CREATE TABLE tenBang\G
Dùng \G để output dễ copy hơn.