两个字符串列作为主键

时间:2022-05-26 11:28:17

I have two different entity types that are identified by a string. I want to write an SQL database to store the relationship between two elements, one of each type.

我有两个不同的实体类型,它们由字符串标识。我想编写一个SQL数据库来存储两个元素之间的关系,其中一个是每种类型。

I don't need to store any information about the elements itself, only its identifier, so I want to write an SQL table that have two string (the related elements' identifiers) as primary keys.

我不需要存储关于元素本身的任何信息,只需要存储它的标识符,因此我想编写一个SQL表,其中包含两个字符串(相关元素的标识符)作为主键。

I have read Strings as Primary Keys in SQL Database and I'm not sure if it's a good idea to have two strings as primary keys of a table.

我在SQL数据库中将字符串作为主键,我不确定是否应该将两个字符串作为表的主键。

The other option is to store in one table the identifiers of one type of entity and in another table, the identifiers of the other type, and then my "relationship" table will have two integers as primary keys... but it will made me to re-write the persistence part of my program...

另一个选项是在一个表中存储一种实体的标识符,在另一个表中存储另一种类型的标识符,然后我的“关系”表将有两个整数作为主键……但它将使我重新编写我的程序的持久性部分……

All the advices are welcome

欢迎大家提出建议

1 个解决方案

#1


2  

There is no problem with using two strings as keys in your database. There is no need to go through all that trouble just to get integers keys, because it technically could be faster. This is basically also what Kevin says in the selected answer on the question you link.

在数据库中使用两个字符串作为键是没有问题的。不需要为了获得整数键而经历那么多麻烦,因为从技术上讲,它可能更快。这基本上也是Kevin在你链接的问题的选定答案中所说的。

#1


2  

There is no problem with using two strings as keys in your database. There is no need to go through all that trouble just to get integers keys, because it technically could be faster. This is basically also what Kevin says in the selected answer on the question you link.

在数据库中使用两个字符串作为键是没有问题的。不需要为了获得整数键而经历那么多麻烦,因为从技术上讲,它可能更快。这基本上也是Kevin在你链接的问题的选定答案中所说的。