简体   繁体   中英

Prevent id field to be 0 in mysql

I'm creating a table with an id field. This is the part of the query which creates the column.

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

When I insert data, I just set id to NULL and mysql set a progressive id number for me. How can I prevent this id to be 0? I'm writing my code so that 0 represent an exception: quite a bad coding style but there's nothing I can do to prevent this without rewriting everything.

So, is there any way to tell mysql never to store zero on the id field?

Thanks!

As auto_increment column only increasing numbers get stored in the column starting from 1. So don't worry about it. The DB handles that without any further action needed.

You could set the initial AUTO_INCREMENT value to a positive integer - but I believe it starts with 1 anyway. Just to be sure:

ALTER TABLE table_name_here AUTO_INCREMENT = 1000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM