Search code examples
sqlmysqlstored-proceduresinsertmysql-error-1264

specify what columns not to insert values into mysql table


I have a stored procedure that inserts a row into a table with an autoincremented column. It gives me a warning but succeeds if i simply put "" for the value of the autoincremented column. The way I get rid of the error currently is to specify which columns the insert values are for... "insert into app_table(app_id,name,...) values(...)" but the autoincremented column is the only one that isn't inserted into. Is there a cleaner way to do this? Like specifying which columns not to insert values into?


Solution

  • You're probably getting this warning:

    mysql> show warnings;
    +---------+------+------------------------------------------------------+
    | Level   | Code | Message                                              |
    +---------+------+------------------------------------------------------+
    | Warning | 1264 | Out of range value adjusted for column 'id' at row 1 |
    +---------+------+------------------------------------------------------+
    1 row in set (0.00 sec)
    

    Instead of "" for the auto_increment column, use NULL and the warning will go away. The auto_increment value will get incremented correctly. No need to list out all of the columns in the insert statement (although many consider it a good practice).