0

I am making a naive ratings platform db and I want to restrict a rating to be whole numbers from 1 to 5. To clarify I do not want to round or truncate, I want it to show a constraint violation if anything except 1,2,3,4,5 is entered.

The data type I'm using is smallint for rating. If I input 2.7, say, it truncates to 2 and proceeds to add the relation instance to the table. Which I don't want. How can I add a constraint to prevent this?

1 Answers1

0

This is what the CHECK keyword is for (https://www.w3schools.com/sql/sql_check.asp)

If you had a table

CREATE TABLE Reviews (
     Rating smallint CHECK (Rating IN (1,2,3,4,5)
); 
DownloadPizza
  • 3,307
  • 1
  • 12
  • 27