DEV Community

Discussion on: How to generate random IDs that are not individually unique, but unique across 2 fields (columns) in a MySQL database table?

Collapse
 
vasilvestre profile image
Valentin Silvestre

Hey, take a look at Unique index on these links :
mysqltutorial.org/mysql-unique/
stackoverflow.com/questions/635937...

This solution works, afaik the only downside is coming from null value.

Collapse
 
kp profile image
KP

@vasilvestre thank you for your response. Adding a unique constraint to a combination of columns in MySQL is useful. But how do you generate a unique ID for one of those fields (commentID in this case) during insert, such that userId + commentID is unique? Is there a way to do this in MySQL?