如何在MySQL中的表上显示所有约束?
要在表上显示所有约束,可以尝试以下任何一种方法:
方法1-使用SHOW命令
您可以在show命令的帮助下进行检查。语法如下-
SHOW CREATE TABLE yourTableName;
方法2-使用information.schema
您可以使用information.schema。语法如下-
select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME from information_schema.KEY_COLUMN_USAGE where TABLE_NAME = 'yourTableName';
要在表上显示所有约束,请实现以上语法。假设我们已经有一个表“ConstraintDemo”。
查询如下-
mysql> select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME −> from information_schema.KEY_COLUMN_USAGE −> where TABLE_NAME = 'ConstraintDemo';
以下是显示约束的输出-
+-------------+-----------------+------------------------+-----------------------+ | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_COLUMN_NAME | REFERENCED_TABLE_NAME | +-------------+-----------------+------------------------+-----------------------+ | Id | PRIMARY | NULL | NULL | | Id | Id | NULL | NULL | +-------------+-----------------+------------------------+-----------------------+ 2 rows in set, 2 warnings (0.04 sec)
现在,让我们使用show命令进行检查。查询如下-
mysql> show create table ConstraintDemo;
以下是输出-
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ConstraintDemo | CREATE TABLE `constraintdemo` (`Id` int(11) NOT NULL,`Name` varchar(100) NOT NULL, PRIMARY KEY (`Id`), UNIQUE KEY `Id` (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)