
- 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
Prevent a combination of items from being inserted twice in MySQL?
To prevent a combination of items from being inserted twice, alter the table and set UNIQUE for the column as shown in the below syntax −
alter table yourTableName add constraint yourConstraintName unique(yourColumnName1,yourColumnName2,....N);
Let us first create a table −
mysql> create table DemoTable ( Value1 int, Value2 int ); Query OK, 0 rows affected (0.47 sec)
Here is the query to prevent a combination of items from being inserted twice −
mysql> alter table DemoTable add constraint Value1_Value2_ConstraintKey unique(Value1,Value2); Query OK, 0 rows affected (0.80 sec) Records : 0 Duplicates : 0 Warnings : 0
Insert some records in the table using insert command. Here, when we try to insert duplicate combination, an error “Duplicate entry” is visible and the duplicate insertion won’t work −
mysql> insert into DemoTable values(10,10); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(10,20); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(10,10); ERROR 1062 (23000) : Duplicate entry '10-10' for key 'Value1_Value2_ConstraintKey' mysql> insert into DemoTable values(20,10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20,20); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(20,10); ERROR 1062 (23000) : Duplicate entry '20-10' for key 'Value1_Value2_ConstraintKey'
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 10 | | 10 | 20 | | 20 | 10 | | 20 | 20 | +--------+--------+ 4 rows in set (0.00 sec)
- Related Articles
- How to prevent form from being submitted?
- WebGL: Prevent color buffer from being cleared in HTML5
- How to Prevent Twitter Accounts from being Hacked
- How can I prevent a window from being resized with Tkinter?
- How to prevent raw rice from being destroyed by termites?
- How to prevent a background process from being stopped after closing SSH client in Linux?
- Set how auto-placed items are inserted in the CSS grid
- Access last inserted row in MySQL?
- How to prevent a user from accessing a specific schema in MySQL?
- How do I stop a MySQL decimal field from being rounded?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- How to prevent numbers being changed to exponential form in Python Matplotlib?
- Prevent negative numbers in MySQL?
- Using LIKE clause twice in a MySQL query
- Prevent having a zero value in a MySQL field?

Advertisements