Duplicate data is inserted with unique index

Skyyy

I have a table called users with 4 unique columns and when I insert data in email it doesn't give me any error and inserts the data even when when the same value already exists in that column.

Here is my database structure:

$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL,
google_id BIGINT UNSIGNED NULL,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL,
username VARCHAR(20) NULL,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id),
UNIQUE(email,username,fb_id,google_id)
)";

But, when I create my table with following structure:

$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL UNIQUE,
google_id BIGINT UNSIGNED NULL UNIQUE,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL UNIQUE,
username VARCHAR(20) NULL UNIQUE,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id)
)";

It gives me an error when there is a duplicate entry.

Creating table with any of those methods doesn't give any error. After creating the tables I have verified with phpmyadmin that all those columns have unique index in both methods.

Drew

Akash, in the 1st create table, the composite (combination) is unique. If you want them individually to be unique, separate them into ... separate UNIQUE key statements, like in the 2nd.

Let's say the bottom of your first table read this

PRIMARY KEY(id),
UNIQUE KEY(email,username,fb_id,google_id)

Then there is nothing wrong with these two rows existing in the composite index:

'[email protected]','Akash',101,102

and

'[email protected]','Akash2',101,102

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Alert if Duplicate entry is inserted in data base on unique condition javascript

Duplicate sets of data inserted into table

Node crashes when a duplicate data is inserted into mongodb

Is it faster to insert duplicate value in unique column or first check is it already inserted

Prevent Unique Data From Being Inserted into Table

Ensuring unique ID for inserted batch of data

How to handle duplicate unique index error?

Duplicate check using SELECT or rely on unique index?

Pandas: Index is duplicate. How to make it unique

Update duplicate data with same unique-identifier

Identifying Duplicate/Unique Teams (and Restructuring Data) in R

Flattening duplicate rows with additional unique data

E11000 duplicate key error even if inserted values for the unique field are different

Guarantee a duplicate record is not inserted?

Duplicate values inserted into database

Python: referring to each duplicate item in a list by unique index

Does DbContext.AddAsync throw an exception given a duplicate unique index?

MongoDB can not create unique sparse index (duplicate key)

Adding a Unique Index When Duplicate Values Exist in the Column

Duplicate key error with mongodb 2dsphere unique index

MySQL update, but delete on duplicate key (due to unique index)

SQL Server : duplicate key from creating unique index

How can I make a dataframe with duplicate datetime index entries unique?

Catching a duplicate error from mongoose unique index and sending a res 409

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found EF

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found

Unique index allows duplicate entries in case that one of entries is NULL

CREATE UNIQUE INDEX statement terminated because a duplicate key was found

Reshaping Pandas Data Frame by Index with Duplicate Indexes

TOP Ranking

HotTag

Archive