Kanishka
Kanishka

Reputation: 363

Prevent Duplicate Inserts SQL in PhpMyadmin

I have a Table called users which structure looks like this:

user_id
google_id
google_name
google_email

When I try to insert a record to this table I need to check both user_id and google_id for duplicate data. When we take one by one these two columns I should be able to insert data with out an issue. I mean that a record can contain the same value in one of user_id or google_id already in the table. But not in both. I tried many things an still could not make this work. Can some one guide me? I am using Phpmyadmin to mange DB

Upvotes: 0

Views: 3442

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328574

Create a unique index on the two columns:

create unique index users_ids on users(user_id,google_id)

If someone tries to insert another row with the same user_id and google_id, the unique index will cause an error.

Upvotes: 1

Related Questions