|
|
Question : MAke auto-increment column NOT primary key??
|
|
Hello all,
Imagine an online survey page. A user returns to the page every day to answer the same questions. The purpose is to track how their answers change from day to day.
They are stored in a mysal database in a table with columns:
record_id, user_id, answer_1, answer_2, ..., answer_n
When I run a report on that table, I want to select by user_id to collect all the records for user-x. Therefore, I want the user-Id column to be the Primary Key for the table. To ensure unique record id's, I want to make the record_id column auto_increment.
BUT, when I do that, I get an error message from mysql that an auto_increment column MUST be the Primary Key.
Is there a way to do what I want to do?
Thanks.
|
Answer : MAke auto-increment column NOT primary key??
|
|
You can make a column auto-increment that is not a primary key:
mysql> create table test_pk ( -> id int auto_increment not null, -> userid int primary key, -> UNIQUE KEY (id)); Query OK, 0 rows affected (0.25 sec)
That being said, I still don't see the justification here for doing so. Putting a primary key index on user_id will prevent a user from submitting multiple records. It would be far better to leave the id column a primary key and create a UNIQUE index on the field (or more likely, combination of fields) that must truly be unique.
|
|
|
|
|