餐厅评论系统

huangapple go评论111阅读模式
英文:

Review system for restaurants

问题

我正在为一个小项目创建数据库,其中包括多家餐厅,用户可以为这些餐厅留下一次评论和评分。

我的问题是:

哪种做法更好:

我的 Review 表应该只有一个由 UserId + RestaurantId 组成的复合主键吗?还是应该创建一个自动增量的主键?

我认为复合主键应该是一个不错的选择,因为用户只能在每家餐厅留下一次评论,但我不是100%确定...

英文:

I'm creating a DB for a small project which consists of having multiple restaurant and a user can leave a review (comment and rating) for that restaurant but just once.

My question is:

Which is better practice to do:

Should my table Review have just a composed primary key of UserId + RestaurantId ? Or should I create a primary key auto increment?

I think the composed primary key should be good as the user can only leave 1 review per restaurant but I'm not 100% sure...

答案1

得分: 1

给定这些信息,一个复合主键可以有效地作为一个参考点,并且相对于自动递增的键有一些优势。

虽然一个不相关的自动递增主键确实能够保证数据库的规范化,使您的程序能够按照预期正常工作,但这很可能不会成为一个问题。

使用复合主键的主要优势在于可以更轻松地从程序中访问审查信息本身。我假设UserID和RestaurantID在程序的其他地方也被使用,因此很容易找到它们。自动递增主键的最大问题在于它与任何其他信息都没有关联,除了它是主键。如果没有它,您将很容易从用户那里获取任何特定的审查,而不需要额外的信息。

不管怎样,这两种实现都不算差。如果您对复合键感到困惑,实现自动递增的主键也不是什么大问题。这只意味着您正在存储一些毫无意义的整数(如果这个数据库变得非常大,可能会影响文件大小)。

英文:

Given the information, a compound primary key would work effectively as a point of reference and has several advantages over an automatically incrementing key.

While an unrelated automatically incrementing primary key does absolutely guarantee that the database will be normalised, with your program working as it should that will likely not be an issue anyway.

The main advantage of using a compound primary key is that the review information itself can more easily be accessed from the program. I assume UserID and RestaurantID are used elsewhere within the program and therefore can easily be found. The biggest problem of an automatically-incrementing primary key is that it is totally unrelated to any other information, apart from being that primary key. Without it, you can easily source any particular review from a user without any additional information.

Regardless, neither implementation is bad. If you struggle with the compound key, it is no great failure to implement an automatically incrementing primary key. All it means is that you're storing somewhat pointless integers (which could, if this database gets very large, have an impact on the filesize).

huangapple
  • 本文由 发表于 2023年6月12日 18:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定