
- 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 can I enforce compound uniqueness in MySQL?
You can enforce compound uniqueness in MySQL with the help of UNIQUE keyword. Here is the syntax to add UNIQUE keyword to your table column.
The syntax is as follows
CREATE TABLE yourTableName ( yourColumnName1 datatype, yourColumnName2 datatype, yourColumnName3 datatype, . . N UNIQUE yourConstarintName(yourColumnName2,yourColumnName3) );
To understand the above concept, let us create a table with some columns and add a unique constraint to a table. The query to create a table is as follows
mysql> create table UniqueDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100), -> StudentAge int, -> StudentMarks int -> , -> UNIQUE age_NameConstraint(StudentName,StudentAge) -> ); Query OK, 0 rows affected (0.76 sec)
You can display the constraints name from a table with the help of SHOW command.
The syntax is as follows
SHOW INDEX FROM yourTableName;
To display the unique constraint from a table, use the above syntax. The query is as follows −
mysql> SHOW INDEX FROM UniqueDemo;
The following is the output
+------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | +------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | uniquedemo | 0 | PRIMARY | 1 | StudentId | A | 0 | NULL | NULL | | BTREE | | | YES | | uniquedemo | 0 | age_NameConstraint | 1 | StudentName | A | 0 | NULL | NULL | YES | BTREE | | | YES | | uniquedemo | 0 | age_NameConstraint | 2 | StudentAge | A | 0 | NULL | NULL | YES | BTREE | | | YES | +------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ 3 rows in set (0.33 sec)
- Related Articles
- Can we enforce compound uniqueness in MySQL?
- How do I remove a uniqueness constraint from a MySQL table?
- How to achieve case sensitive uniqueness and case insensitive search in MySQL?
- How can I view cascades in MySQL?
- How can I start MySQL Server?
- How can I shutdown MySQL Server?
- How can I change root username in MySQL?
- How to enforce axis range in Matplotlib?
- How can I delete MySQL temporary table?
- How can I merge two MySQL tables?
- How can I simulate a print statement in MySQL?
- How can I simulate an array variable in MySQL?
- How can I find non-ASCII characters in MySQL?
- How can I see global locks in MySQL (innodb)?
- How can I avoid “repair with keycache” in MySQL?

Advertisements