Which datatype is should I use to set a column for 5-star rating?


You can use ENUM datatype for this. Let us first create a table −

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 records in the table using insert command −

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)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+----+------------+
| Id | UserRating |
+----+------------+
| 1  | 5          |
| 2  | 3          |
| 3  | 1          |
| 4  | 3          |
| 5  | 4          |
+----+------------+
5 rows in set (0.00 sec)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements