<small id='kNBOo'></small><noframes id='kNBOo'>

      <bdo id='kNBOo'></bdo><ul id='kNBOo'></ul>

  • <i id='kNBOo'><tr id='kNBOo'><dt id='kNBOo'><q id='kNBOo'><span id='kNBOo'><b id='kNBOo'><form id='kNBOo'><ins id='kNBOo'></ins><ul id='kNBOo'></ul><sub id='kNBOo'></sub></form><legend id='kNBOo'></legend><bdo id='kNBOo'><pre id='kNBOo'><center id='kNBOo'></center></pre></bdo></b><th id='kNBOo'></th></span></q></dt></tr></i><div id='kNBOo'><tfoot id='kNBOo'></tfoot><dl id='kNBOo'><fieldset id='kNBOo'></fieldset></dl></div>

    <tfoot id='kNBOo'></tfoot>

      <legend id='kNBOo'><style id='kNBOo'><dir id='kNBOo'><q id='kNBOo'></q></dir></style></legend>

        将 MySQL 数据类型的值限制在特定范围内(最好不是 ENUM)

        时间:2024-04-15

            <i id='MJBk1'><tr id='MJBk1'><dt id='MJBk1'><q id='MJBk1'><span id='MJBk1'><b id='MJBk1'><form id='MJBk1'><ins id='MJBk1'></ins><ul id='MJBk1'></ul><sub id='MJBk1'></sub></form><legend id='MJBk1'></legend><bdo id='MJBk1'><pre id='MJBk1'><center id='MJBk1'></center></pre></bdo></b><th id='MJBk1'></th></span></q></dt></tr></i><div id='MJBk1'><tfoot id='MJBk1'></tfoot><dl id='MJBk1'><fieldset id='MJBk1'></fieldset></dl></div>

            <small id='MJBk1'></small><noframes id='MJBk1'>

              • <bdo id='MJBk1'></bdo><ul id='MJBk1'></ul>
                    <tbody id='MJBk1'></tbody>
                  <legend id='MJBk1'><style id='MJBk1'><dir id='MJBk1'><q id='MJBk1'></q></dir></style></legend>

                  <tfoot id='MJBk1'></tfoot>
                  本文介绍了将 MySQL 数据类型的值限制在特定范围内(最好不是 ENUM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将字段中可以存储的数据类型值限制为特定范围的整数值:[0,10].

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

                  在 PHP 脚本中的用户输入时,我验证并清理数据以确保它在 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?

                  目前我将 int 值存储在一个 UNSIGNED TINYINT 中,当然它的范围是 0-255.我知道 ENUM 作为一种选择.但是,我读过使用数字时不建议使用:http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

                  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/

                  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;
                  

                  希望有人能帮忙.

                  推荐答案

                  您可以创建一个允许投票值的表并在您的投票表中添加一个外键,因此当您尝试使用 user_vote 值而不是现有值插入投票时在您的 allowed_votes 表中,您会收到一个约束失败错误:

                  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);
                  

                  这篇关于将 MySQL 数据类型的值限制在特定范围内(最好不是 ENUM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server 2005 是否具有等效于 MySql 的 ENUM 数据类型? 下一篇:如何在 Oracle 中使用枚举?

                  相关文章

                • <i id='tn7Xp'><tr id='tn7Xp'><dt id='tn7Xp'><q id='tn7Xp'><span id='tn7Xp'><b id='tn7Xp'><form id='tn7Xp'><ins id='tn7Xp'></ins><ul id='tn7Xp'></ul><sub id='tn7Xp'></sub></form><legend id='tn7Xp'></legend><bdo id='tn7Xp'><pre id='tn7Xp'><center id='tn7Xp'></center></pre></bdo></b><th id='tn7Xp'></th></span></q></dt></tr></i><div id='tn7Xp'><tfoot id='tn7Xp'></tfoot><dl id='tn7Xp'><fieldset id='tn7Xp'></fieldset></dl></div>
                • <tfoot id='tn7Xp'></tfoot>

                  <small id='tn7Xp'></small><noframes id='tn7Xp'>

                  <legend id='tn7Xp'><style id='tn7Xp'><dir id='tn7Xp'><q id='tn7Xp'></q></dir></style></legend>
                    <bdo id='tn7Xp'></bdo><ul id='tn7Xp'></ul>