
- 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 do I show unique constraints of a table in MySQL?
You can show unique constraints of a table in MySQL using information_schema.table_constraints.
The syntax is as follows.
SELECT DISTINCT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = ’yourTableName’ AND CONSTRAINT_TYPE = ’UNIQUE’;
To understand the above syntax, let us create a table with UNIQUE constraint −
mysql> create table UniqueConstraint -> ( -> Id int, -> FirstName varchar(30), -> LastName varchar(30), -> constraint uniqueFirstNameAndLastName UNIQUE(FirstName,LastName) -> ); Query OK, 0 rows affected (0.74 sec)
Implement the above syntax to display the name of the constraint which is a unique constraint from a MySQL table. The query is as follows −
mysql> select distinct CONSTRAINT_NAME -> from information_schema.TABLE_CONSTRAINTS -> where table_name = 'UniqueConstraint' and constraint_type = 'UNIQUE';
The following is the output −
| CONSTRAINT_NAME | +----------------------------+ | uniqueFirstNameAndLastName | +----------------------------+ 1 row in set, 2 warnings (0.01 sec)
If you want to display all the constraints of a MySQL table, use the following query.
mysql> SELECT DISTINCT CONSTRAINT_NAME -> FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS -> WHERE CONSTRAINT_SCHEMA = 'MySQL'\G
The following is the output −
*************************** 1. row *************************** CONSTRAINT_NAME − PRIMARY *************************** 2. row *************************** CONSTRAINT_NAME − name 2 rows in set, 2 warnings (0.01 sec)
- Related Articles
- Show constraints on table command in MySQL?
- How do I show the schema of a table in a MySQL database?
- How to display all constraints on a table in MySQL?
- How do I clone the structure of a table in MySQL?
- How do I detect if a table exist in MySQL?
- How do I show a MySQL warning that just happened?
- How do I alter a MySQL table column defaults?
- How do I get the creation date of a MySQL table?
- How do I list all the columns in a MySQL table?
- How do I add a check constraint to a table in MySQL?
- How to use UNIQUE constraints in Android sqlite?
- How do I remove a uniqueness constraint from a MySQL table?
- How do I use `SHOW COLUMNS` as a valid data source for a table?
- How do I kill all the processes in MySQL “show processlist”?
- How do I view the auto_increment value for a table in MySQL?

Advertisements