1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

mysql help

Discussion in 'Other Scripting Languages' started by cashmaster, Jan 1, 2012.

  1. cashmaster

    cashmaster Junior Member

    Joined:
    Dec 25, 2010
    Messages:
    146
    Likes Received:
    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 ?
     
  2. woofoo

    woofoo Junior Member

    Joined:
    Oct 19, 2011
    Messages:
    123
    Likes Received:
    17
    make a constraint
    Code:
     http://www.w3schools.com/sql/sql_check.asp 
     
    • Thanks Thanks x 1
  3. xpwizard

    xpwizard Junior Member

    Joined:
    Nov 6, 2010
    Messages:
    198
    Likes Received:
    122
    Easier to code the limits via your coding language. That way it's easier to deal with validation/errors.
     
  4. p4r4d0x

    p4r4d0x Newbie

    Joined:
    Sep 15, 2010
    Messages:
    47
    Likes Received:
    2
    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.
     
  5. pcboy

    pcboy Registered Member

    Joined:
    Feb 20, 2009
    Messages:
    60
    Likes Received:
    53
    Location:
    Largest democracy
    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)
    )
     
    • Thanks Thanks x 1
  6. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    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...");
    
    ?>
    
     
    • Thanks Thanks x 1
    Last edited: Jan 1, 2012
  7. a_z_0_9

    a_z_0_9 Junior Member

    Joined:
    Jul 18, 2011
    Messages:
    110
    Likes Received:
    21
    why do you want to restrict value between 100 and 1000?
     
  8. cashmaster

    cashmaster Junior Member

    Joined:
    Dec 25, 2010
    Messages:
    146
    Likes Received:
    17
    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.