JasperFennet JasperFennet - 6 months ago 11
SQL Question

Mysql Unique record 2 fields

I am making an application wich insert users into a mysql Database.

i know you can make a field value unique, but i want a combination of 2 collums in a record to be unique.

for example:

id, firstname, lastname, creationdate

1, John, Doe, (today)

now what i want to archieve is that i can enter a record like :

2, John, Deo, (today)

but i cannot enter:

2, John, Doe, (today)

with wat sql querry can i make an combined unique value

Answer

use the below query

ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (FirstName,LastName)

http://www.w3schools.com/sql/sql_primarykey.asp

Comments