
- 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
Using UNIQUE for varchar columns with some conditions in MySQL?
For this, you can use UNIQUE constraint on one or more columns −
alter table yourTablleName add unique(yourColumnName1,yourColumnName2,...N);
Let us first create a table −
mysql> create table DemoTable1598 -> ( -> EmployeeId int, -> EmployeeName varchar(20), -> EmployeeCountryName varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
Here is the query to implement UNIQUE on varchar columns −
mysql> alter table DemoTable1598 add unique(EmployeeName,EmployeeCountryName); Query OK, 0 rows affected (0.55 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable1598 values(101,'Adam','AUS'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1598 values(102,'John','US'); Query OK, 1 row affected (0.64 sec) mysql> insert into DemoTable1598 values(103,'Adam','US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1598 values(104,'Adam','AUS'); ERROR 1062 (23000): Duplicate entry 'Adam-AUS' for key 'EmployeeName'
Display all records from the table using select statement −
mysql> select * from DemoTable1598;
This will produce the following output −
+------------+--------------+---------------------+ | EmployeeId | EmployeeName | EmployeeCountryName | +------------+--------------+---------------------+ | 101 | Adam | AUS | | 102 | John | US | | 103 | Adam | US | +------------+--------------+---------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Set conditions for columns with values 0 or 1 in MySQL
- Using MySQL IN() for some column values with underscore
- How to create conditions in a MySQL table with multiple columns?
- Set different IDs for records with conditions using a single MySQL query
- Count values based on conditions and display the result in different columns with MySQL?
- How to find the unique rows based on some columns in R?
- How to update a MySQL column by subtracting a value with some conditions?
- How to sort varchar numeric columns by DESC or ASC in MySQL?
- How to make a pair of columns unique in MySQL?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- How to display some columns (not all) in MySQL?
- Find “greatest” between two columns and display with some records already null in MySql
- Order by date set with varchar type in MySQL
- Order VARCHAR records with string and numbers in MySQL
- MySQL If statement with multiple conditions?

Advertisements