Suleman khan
Suleman khan

Reputation: 1068

how can i check two columns while inserting values?

I have one table with following columns

Id fksur_id name email

I want to do like this..and it is fine.

fksur_id email
2        [email protected]
2        [email protected]

this is not ok

fksur_id email
2        [email protected]
2        [email protected]

I would like to know how shall i have to restrict if same email address is being inserted along with same fksur_id. here i want to have duplicate fksur_id with different email or duplicate email with different fksur_id. any help would be appreciated.

Upvotes: 2

Views: 152

Answers (4)

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7668

here is SQLFiddle

ALTER TABLE table_name ADD PRIMARY KEY(FKSUR_ID, EMAIL);

If a primary key already exists then you want to do this

 ALTER TABLE table_Name ADD CONSTRAINT CONSTRAINTNAME UNIQUE (FKSUR_ID, EMAIL);

Upvotes: 2

Mangoose
Mangoose

Reputation: 922

Create a unique key using columns fksur_id and email

Upvotes: 4

Rowman Pirce
Rowman Pirce

Reputation: 423

Use Unique Index with 2 columns in. http://dev.mysql.com/doc/refman/5.7/en/create-index.html

Upvotes: 1

Scott Helme
Scott Helme

Reputation: 4799

Why not just do a SELECT, see if it returns any results and if not, then perform the INSERT?

Upvotes: 0

Related Questions