114497 114497 - 1 year ago 74
MySQL Question

Can you have a primary key and a unique key in the same table in MySQL?

In what situations will this be beneficial in? If you can.

Answer Source

In same table?

Yes definitely you can have.

In what situation?

That depends, one example would be: say you have a synthetic ID column in your table designated as primary key (AUTO_INCREMENT) but you want FirstName of the customer to be unique always. In that case define a UNIQUE constraint on the FirstName column.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download