MySQL 5 星级数据类型?

发布于 2024-08-17 18:10:06 字数 79 浏览 4 评论 0原文

对于必须介于 1 和 5 之间的产品评级,ENUM('1','2','3','4','5') 是否是一个合理的数据类型?

谢谢!

Would ENUM('1','2','3','4','5') be a sensible datatype for a product rating which must be between 1 and 5?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡笑忘祈一世凡恋 2024-08-24 18:10:06

是的,这将是一个合适的数据类型,因为它强制执行您的域。

但是,如果您要将它们相加(或进行任何其他数学运算),那么数字数据类型可能会更好。

Yes, that would be an appropriate data type since it enforces your domain.

If you are going to add (or do any other mathematical operation) them together, however, a numeric data type might be better.

似最初 2024-08-24 18:10:06

我建议使用

TINYINT UNSIGNED NOT NULL

or ,为了更好的 ANSI/SQL 兼容性,使用:

SMALLINT NOT NULL

使用整数类型,计算更容易。 ENUM 还不错,但有可能会搞砸,因为它是一种双字符串/int 类型(在内部,它是一个 int,但从外部看,它是一个字符串)。事实上,假设您确实觉得需要升级到 3 星或 10 星左右,那么迁移的痛苦就会少得多。

I suggest using

TINYINT UNSIGNED NOT NULL

or, for better ANSI/SQL compatibility, use:

SMALLINT NOT NULL

With an integer type, it is much easier to do calculations. ENUM is not bad, but there is a potential to mess up because it's kind of a dual string/int type (beneath the covers, it's an int, but from the outside, it's a string). And indeed, suppose you do feel the need to go to 3 stars, or 10 stars or so, the migration will be much less painful.

过去的过去 2024-08-24 18:10:06

如果您使用的是 Mysql 8+,则使用带有 CHECK 约束的 TINYINT

-- Product reviews
CREATE TABLE product_review (
  product_review_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL,
  product_id INT UNSIGNED NOT NULL,
  unique(user_id, product_id),
  rating TINYINT UNSIGNED NOT NULL CHECK (
    rating > 0
    AND rating <= 5
  ),
  review VARCHAR(2047) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
 -- FOREIGN KEY (product_id) REFERENCES product (product_id) ON DELETE CASCADE ON UPDATE CASCADE.
   -- FOREIGN KEY (user_id) REFERENCES user (user_id) ON DELETE CASCADE ON UPDATE CASCADE
);


它不允许使用除 1、2、3、4、5 之外的值,并且还支持任何类型数学运算,例如通过简单的计算就可以得到产品的平均评分。
输入图片此处描述

If you are using Mysql 8+ then you use TINYINT with CHECK constraint

-- Product reviews
CREATE TABLE product_review (
  product_review_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL,
  product_id INT UNSIGNED NOT NULL,
  unique(user_id, product_id),
  rating TINYINT UNSIGNED NOT NULL CHECK (
    rating > 0
    AND rating <= 5
  ),
  review VARCHAR(2047) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
 -- FOREIGN KEY (product_id) REFERENCES product (product_id) ON DELETE CASCADE ON UPDATE CASCADE.
   -- FOREIGN KEY (user_id) REFERENCES user (user_id) ON DELETE CASCADE ON UPDATE CASCADE
);


It would not allow value other than 1, 2, 3, 4, 5 and also support any kind of mathematical operation, for example you can get average rating of the product with simple calculation.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文