sql - Making a column having an "unique" value depending on the value of another column -
let's have simple table called "characters":
realm_id | character_name | xp ---------|----------------|---------- 1 | "mike" | 10 1 | "lara" | 25 2 | "mike" | 40
what want have unique names depending on realm_id
. so, example, while having 2 "mikes"
different realm_id
s allowed, it's not allowed have 2 "mikes"
within same realm_id
. possible?
if you're looking perform select statement on data you'll looking (assuming highest xp wins);
select realm_id character_name max(xp) xp characters
however, if want table not allow duplicates in first place you're best looking @ making teh realm_id , character_name composite primary key. stop duplication happening in first place although you'll have consider happens when tries insert duplicate, it'll throw error.
Comments
Post a Comment