Search code examples
mysqlauto-incrementunsignedsigned

AUTO_INCREMENT in mysql can be signed by default?


i understand,below column will be signed int by default.

   id INT(6);

Can an auto increment column specified below be signed by default? Mysql starts the value from 1 for an auto increment column.

id INT(6) AUTO_INCREMENT PRIMARY KEY

Solution

  • Yes, you can create an auto increment primary key with a signed int. Try this:

    CREATE TABLE mytable( id int(6) AUTO_INCREMENT PRIMARY KEY);
    

    Then the following queries are both valid

    INSERT INTO mytable values();
    INSERT INTO mytable values(-10);
    

    This will result in the table having a row with -10 and another with 1 as values. But you will run into problems if you try this:

     ALTER TABLE mytable AUTO_INCREMENT=-10;
    

    yes, you cannot have auto increment values that are negative numbers.