在MySQL中显示对表命令的约束?

您可以借助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)
-> );

现在,您可以应用上述语法以显示对表的约束。查询如下-

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)