您可以借助 SHOW 命令显示表上的约束。语法如下 -
show create table yourTableName;
上述命令将显示表 ENGINE 的所有约束。使用它,你甚至可以看到所有的列名和相应的数据类型。
为了理解上面的MySQL语句,让我们首先创建一个表 -
mysql> create table ShowConstraintsDemo
-> (
-> BookId int not null,
-> BookName varchar(200) not null,
-> BookAuthor varchar(200) Unique not null,
-> Primary key(BookId,BookName)
-> );
Query OK, 0 rows affected (1.04 sec)
现在您可以应用上述语法来显示表上的约束。查询如下 -
mysql> show create table ShowConstraintsDemo;
以下是显示所有约束的输出 -
+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ShowConstraintsDemo | CREATE TABLE `showconstraintsdemo` ( `BookId` int(11) NOT NULL, `BookName` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, `BookAuthor` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`BookId`,`BookName`), UNIQUE KEY `BookAuthor` (`BookAuthor`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.05 sec)