Then I understand the messy code.
You can of course do it as esukf suggests, and it will work relatively good. But there is room for error with that approach. I was going to write something like "if you don't understand it then read up on it", but since I didn't find anything with a quick search I guess that the manual doesn't have enough information on it. So here is a crash course to UNIQUE:
The command UNIQUE makes sure that everything inside the paranthesis is unique together. Let's say that you have a table with id, username and mail and that id is the primary key. You decide that you don't want duplicate usernames, so you add the following:
UNIQUE (username)
That makes it impossible for two users to have the same username. If you don't want the users to be able to have the same username or mail then you would add:
UNIQUE (username),
UNIQUE (mail)
Then it checks that the username doesn't exist before and that the mail address doesn't exist before. If you instead want to make sure that noone have the same combination of username and mail you do this:
UNIQUE (username, mail)
After a while you have the following table:
id username mail
1 Piranha piranha@none.com
2 SoulAssassin sa@none.com
3 esukf esukf@none.com
4 TheOnlyPiranha piranha@somethingelse.com
Then I try to join. I first try to use Piranha and piranha@none.se, and get an error message since it is unique. Then I try to use Piranha and piranha@abc.com, and it works since it is the combination that is unique. I could even have used Piranha and piranha@somethingelse.com. Both values are already taken, but because there is not one row with both values it is possible.
I suggest that you try it out with a few quick examples, you will understand it pretty quick.