应该使用哪种数据类型为 5 星级评定设置列?
可以使用 ENUM 数据类型来实现这一点。 让我们先创建一张表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserRating ENUM('1','2','3','4','5') ); Query OK, 0 rows affected (0.54 sec)
使用 insert 命令将记录插入表中 -
mysql> insert into DemoTable(UserRating) values('5'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(UserRating) values('3'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserRating) values('1'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(UserRating) values('3'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(UserRating) values('4'); Query OK, 1 row affected (0.18 sec)
使用 select 命令从表中显示记录 -
mysql> select *from DemoTable;
将产生以下输出 -
+----+------------+ | Id | UserRating | +----+------------+ | 1 | 5 | | 2 | 3 | | 3 | 1 | | 4 | 3 | | 5 | 4 | +----+------------+ 5 rows in set (0.00 sec)
广告