Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:49:30+00:00 2026-05-29T17:49:30+00:00

I want to limit the datatype value that can be stored within a field

  • 0

I want to limit the datatype value that can be stored within a field to a specific range of integer values: [0,10].

On user input within a PHP script I validate and sanitise the data to make sure it is within the range 0 to 10. However, is there a way to ensure this remains true within the votes table itself via some sort of datatype or constraint?

At the moment I store the int value within an UNSIGNED TINYINT which of course has the range of 0-255. I am aware of ENUM as an option. However, I have read that it is not advisable when using numbers: http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

The SQL:

CREATE TABLE votes (
vote_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
article_id MEDIUMINT UNSIGNED NOT NULL,
user_id MEDIUMINT UNSIGNED NOT NULL,
user_vote TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (vote_id),
FOREIGN KEY (article_id) REFERENCES articles (article_id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT article_uservoted UNIQUE KEY (user_id, article_id),
INDEX vote_value (article_id, user_id, user_vote)
) ENGINE=INNODB;

I hope someone can assist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:49:32+00:00Added an answer on May 29, 2026 at 5:49 pm

    You can create a table of allowed vote values and add a foreign key in your votes table, so when you try to insert a vote with user_vote value other than existing in your allowed_votes table you get a constraint fail error:

    CREATE TABLE allowed_votes (
      vote_rank TINYINT UNSIGNED NOT NULL,
      PRIMARY KEY (vote_rank)
    ) ENGINE = InnoDB;
    
    INSERT INTO allowed_votes( vote_rank ) VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
    
    ALTER TABLE votes
    ADD FOREIGN KEY (user_vote) REFERENCES allowed_votes (vote_rank);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to limit port 80 so that only one user can use it.
I want to limit the length of a textfield, so that a user can
I have an input field and I want to limit it to alphanumerical (A-Z,
I want to limit how many songs a user can download in my FREE
I want to limit number of chars in input text field. Code I'm using:
i just want limit a query by parameter inside a stored, but I get
I want to limit my SWF file so that it cannot be stolen and
I want to limit the what characters the user should be allowed to type
hey, hi i want put limit on object creation means a class can have
I want to limit my varchar columns to have only ascii characters within a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.