简体   繁体   English

在MySQL中防止ID字段为0

[英]Prevent id field to be 0 in mysql

I'm creating a table with an id field. 我正在创建一个带有id字段的表。 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. 当我插入数据时,我只是将id设置为NULL,而mysql为我设置了一个渐进的id号。 How can I prevent this id to be 0? 如何防止此ID为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. 我正在编写代码,以便0代表一个例外:这是一种很糟糕的编码样式,但是如果不重写所有内容,我无能为力。

So, is there any way to tell mysql never to store zero on the id field? 那么,有没有办法告诉mysql永远不要在id字段上存储零?

Thanks! 谢谢!

As auto_increment column only increasing numbers get stored in the column starting from 1. So don't worry about it. 由于auto_increment列仅从1开始存储递增的数字,因此不必担心。 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. 您可以将初始AUTO_INCREMENT值设置为正整数-但我仍然相信它始终以1开头。 Just to be sure: 只是要确定:

ALTER TABLE table_name_here AUTO_INCREMENT = 1000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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