Give an int table column in MySQL a range of allowed values from 1 to 9

六眼飞鱼酱① 提交于 2019-12-12 17:16:42

问题


I am new in MySQL and I was wondering if there is a way of giving a column a range of accepted values let's say from 1 to 9; I was trying the following but it does not work.

CREATE TABLE rooms (
    `rNumber` INT( 1 -9 ) NOT NULL AUTO_INCREMENT ,
    PRIMARY KEY ( `hNumber` )
)

Any help is appreciated.


回答1:


Depending on exactly what you are driving at you might consider using the MySQL Enum Type.




回答2:


ENUM and SET columns are MySQL specific.

The most capable and cross platform means of doing what you ask requires you to:

  1. Create a table to contain all the values you want:

    CREATE TABLE rValidation (
       rNumber INT AUTO_INCREMENT
    )
    
  2. Relate the appropriate column in the rooms table to the column to use for validation in the table created in Step 1, using a foreign key constraint




回答3:


CHECK constraints are not enforced in MySQL. They are parsed during the CREATE TABLE statements but are ignored.

One way to enforce such a constraint is to use a FOREIGN KEY constraint to a reference table, which has exactly 9 rows and allowing only SELECT privileges to it (so nobody adds, changes or removes rows from it):

CREATE TABLE NineNumbers (
    i INT NOT NULL ,
    PRIMARY KEY (i)
)

INSERT INTO NineNumbers (i)
  VALUES
  (1),(2),(3),(4),(5),(6),(7),(8),(9) ;

CREATE TABLE rooms (
    rNumber INT NOT NULL ,            --- AUTO_INCREMENT or not
                                      --- PRIMARY KEY or not
    FOREIGN KEY (rNumber)
      REFERENCES NineNumbers (i) 
)



回答4:


If your range doesn't include zero then an enum will work, MySQL reserves zero for certain error conditions:

The index value of the empty string error value is 0.

You'll also want strict mode enabled if you really only want 1-9 in your column:

If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a “normal” empty string by the fact that this string has the numeric value 0.
[...]
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.

Alternatively, you could add an INSERT and UPDATE trigger that would check that the new rNumber was within the desired range and raise an exception if it wasn't. This approach is more or less equivalent to a CHECK constraint in other databases.




回答5:


The check should be performed in PHP, what happens if you insert a value which is not in range it will insert a NULL value

Try this

$rNo=$_POST['rNumber'];
if($rNo >=1 && $rNo <=9)
{
// Execute Insert query
}
else
{
//display error message saying value not in range
}



回答6:


Use BETWEEN n AND y. Example:

SELECT id FROM mytable WHERE id BETWEEN 1 AND 20;


来源:https://stackoverflow.com/questions/9287784/give-an-int-table-column-in-mysql-a-range-of-allowed-values-from-1-to-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!