![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements