mysql help

cashmaster

Junior Member
Joined
Dec 25, 2010
Messages
146
Reaction score
17
While creating a table in a database, is it possible to specify the upper and lower limit for a variable ? For example, if there is a variable called "id" in the table, and i want the value of it between 100 and 1000 only, how is that possible ?
 
Easier to code the limits via your coding language. That way it's easier to deal with validation/errors.
 
it would be best if you check those limits via your code. that way you can have check on strings also using expressions.

let me know if you need any help there.
 
CREATE TABLE tblname
(
Id int NOT NULL,
CONSTRAINT cnst_id CHECK (Id between 100 and 1000)
)

CREATE TABLE tblname
(
Id int NOT NULL,
CONSTRAINT cnst_id CHECK (Id >99 AND ID < 1001)
)
 
As said before, try to use your actual code to limit rather than the query. In php for example:

Code:
<?php

$max = 100;
$min = 1;
$var = (int) $var;


if ($var > $max) {
  $var = $max;
} elseif ($var < $min) {
  $var = $min;
}

$result = mysql_query("insert into...");

?>
 
Last edited:
why do you want to restrict value between 100 and 1000?
 
Hi

Thanks for all the help. I do know how to code it using php. Its a matter of a simple if-statement. But i didnt remember the CHECK constraint in mysql. Thats what i needed.
 
Back
Top