显示 MySQL 中的表命令约束?
借助 SHOW 命令,您可以显示表中的约束。以下是语法 −
show create table yourTableName;
上述命令将显示所有约束以及表引擎。使用此命令,您甚至可以看到所有列名和相应的数据类型。
为了理解上述 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)
广告