0

I have a table in my database called Coordinate where every row consists of CoordinateId, X, and Y as well as a foreign key of TileId.

I know using a string as a primary key is generally considered bad practice. But I'm wondering if it could be a good idea in this case. I would make the CoordinateId the value of X and Y. So x = 4 y = -3 would have the CoordinateId = "4,-3". Coordinates will never change, everyone is unique and no values in the table will ever change.

The reason I'm thinking about this is that it'd make it very easy to call. I'd be able to do a Village.Title.Coordinate.Id = "4,4" to quickly find a specific CoordinateId. Not that it would be hard to instead find Coordinate.X && Coordinate.Y. But still, it's a bit nice to have. Otherwise, the CoordinateId would basically be a placeholder, a value I would have no use for.

Using MVC, sqlserver and entity framework.

linkedby
  • 148
  • 1
  • 8
  • What are you trying to achieve? How are you accessing your table? See the answer [here](https://stackoverflow.com/questions/3162202/sql-primary-key-integer-vs-varchar) which explains the difference. – Peter Smith Dec 03 '22 at 17:07
  • 3
    use a combined primary key (x,Y) – nbk Dec 03 '22 at 17:10
  • A composition key sounds like a great solution, thanks @nbk – linkedby Dec 03 '22 at 17:16
  • The problem of combining both values into a single string is that different apps can format the numbers in a different way and in these cases each client may or may not find the data. For example, the value '2.5' can be formatted as `2.5`, `2.50`, `25e-1`, `25E-1`, etc. – The Impaler Dec 04 '22 at 01:53

0 Answers0