
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to display all constraints on a table in MySQL?
To display all constraints on a table, you can try any of the following methods −
Method 1 − Using SHOW command
You can check with the help of show command. The syntax is as follows −
SHOW CREATE TABLE yourTableName;
Method 2 − Using information.schema
You can use information.schema. The syntax is as follows −
select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME from information_schema.KEY_COLUMN_USAGE where TABLE_NAME = 'yourTableName';
To display all constraints on a table, implement the above syntax. Let’s say we already have a table ‘ConstraintDemo’.
The query is as follows −
mysql> select COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_COLUMN_NAME, REFERENCED_TABLE_NAME −> from information_schema.KEY_COLUMN_USAGE −> where TABLE_NAME = 'ConstraintDemo';
The following is the output displaying the constraints −
+-------------+-----------------+------------------------+-----------------------+ | 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)
Now let us check using the show command. The query is as follows −
mysql> show create table ConstraintDemo;
The following is the output −
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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)
- Related Articles
- Show constraints on table command in MySQL?
- Display all fields of a table in MySQL?
- How do I show unique constraints of a table in MySQL?
- What all constraints can be added to a PostgreSQL table?
- Use TRIM on all records in a MySQL table?
- How to display the Engine of a MySQL table?
- sp_help for MySQL to display field types and foreign key constraints?
- How to display MySQL Table Name with columns?
- How to display some columns (not all) in MySQL?
- How to find all uppercase strings in a MySQL table?
- MySql how to display the records with latest ID in a table?
- Display all products having the highest amount in a MySQL table with product details?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- MySQL query to display structure of a table
- How to display all the tables in MySQL with a storage engine?

Advertisements