JasperFennet JasperFennet - 6 months ago 17
SQL Question

Mysql Unique record 2 columns

I am making an application which inserts users into a MySQL database.

I know you can make a column value unique, but I want a combination of 2 columns in a record to be unique.

For example:

id firstname lastname creationdate
-----------------------------------------
1 John Doe (today)


What I want to achieve is that I can enter a record like :

2, John, Deo, (today)


but I cannot enter:

2, John, Doe, (today)


What SQL statement can I use to make a combination of multiple columns unique?

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