Q:

Prevent users from joining the survey multiple times using MYSQL

For example, we have a Web site. We have added surveys to our site.
Surveys will be attended by users on our website.
We have a database as follows.
There are 3 tables as users, surveys and surveyParticipation.
users
->userID         (int)
->username       (varchar(45))
->password       (varchar(45))
->email          (varchar(45))
->firstname      (varchar(45))
->lastname       (varchar(45))

surveys
 ->surveyNumber       (int)
 ->questionNumber     (int)
 ->question           (varchar(500))

surveyParticipation
 ->surveyNumber       (from the surveys table)
 ->userID             (from the users table)

I want to do the following operations only with mysql (phpmyadmin) without using any programming language;
1-) one member can participate in more than one questionnaire (but each member can participate only once in a survey)
for example ;
surveyParticipation table
 surveyNumber:1   userID:     1
 surveyNumber:1   userID:     2
 surveyNumber:1   userID:     3
 surveyNumber:1   userID:     4
 surveyNumber:1   userID:     5
 surveyNumber:1   userID:     6
 surveyNumber:1   userID:     7
 surveyNumber:2   userID:     1
 surveyNumber:2   userID:     2
 surveyNumber:2   userID:     3
 surveyNumber:2   userID:     4
 surveyNumber:2   userID:     1  (This user has joined more than once! How do I prevent it)

do you help me solve my problem?

A:

Add a unique constraint on surveynumber and userid in surveyparticipation.
ALTER TABLE surveyparticipation
            ADD UNIQUE (surveynumber,
                        userid);

Or, since the table apparently has none but should have one, make it a primary key.
ALTER TABLE surveyparticipation
            ADD PRIMARY KEY (surveynumber,
                             userid);

Primary keys have to be unique too, so this also works. And nulls don't seem to makes sense in surveynumber nor userid, so the implicit not null constraint coming with it doesn't harm either.

