Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force MySQL to take 0 as a valid auto-increment value

Long story short, I have a SQL file that I want to import as a skel style file, so this will be done repeatedly, programmatically. I can edit the SQL file however I want, but I'd rather not touch the application itself.

This application uses userid = 0 to represent the anonymous user. It also has a relevant (blank) entry in the database to represent this 'user'. Hence, the line in my skel.sql looks something like this:

INSERT INTO `{{TABLE_PREFIX}}users` VALUES (0, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL); 

The problem with this is that uid is a auto_increment field, for which, technically, 0 is an invalid value. Or atleast, if you set it to 0, you're basically telling MySQL, "Please insert the next id into this field."

Now, I suppose I could put an INSERT then an UPDATE query into my SQL file, but is there a way of telling MySQL in general that yes, I actually want to insert 0 into this field?

like image 676
Matthew Scharley Avatar asked Jul 17 '09 10:07

Matthew Scharley


People also ask

How do I set auto increment to zero?

In MySQL, the syntax to reset the AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = value; table_name. The name of the table whose AUTO_INCREMENT column you wish to reset.

Does SQL auto increment start at 0?

By default, the AUTO_INCREMENT column begins at 1. You can also explicitly assign 0 to the column to generate the next sequence number unless the NO_AUTO_VALUE_ON_ZERO SQL mode is enabled. Assigning NULL to the column will also generate the next sequence number, as long as the column was declared NOT NULL.

Can we change auto increment value in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.

Does auto increment need not NULL?

You don't have to specify NOT NULL on the column definition with AUTO_INCREMENT . You can leave it off, and MySQL will make the column NOT NULL . And if you specify NULL in place of NOT NULL , MySQL will accept the syntax, but it will ignore that, and make the column NOT NULL anyway.


2 Answers

From the answer I got here:

You can use:

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO' 

Which as described here, will prevent MySQL from interpreting an INSERT/UPDATE ID of 0 as being the next sequence ID. Such behaviour will be limited to NULL.

It is what I'd consider pretty bad behaviour from the application though. You'll have to be real careful that it's used consistently, especially if you choose to implement replication at a later date.

like image 129
Matthew Scharley Avatar answered Oct 05 '22 20:10

Matthew Scharley


Check your sql DB mode with:

SELECT @@[GLOBAL|SESSION].sql_mode; 

If it's empty or not set, use:

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO' 

BE CAREFUL! If you use GLOBAL, it's not an immediate change, you need to restart your connection to apply the setting.

So, if you're restoring data from one DB to another, for example, and you're not sure if this setting is applied, use SESSION for an immediate change (it resets when closing the connection). When done, insert 0 value and it won't change even if the sql_mode is changed.

To reset this mode (and others) use

SET [GLOBAL|SESSION] sql_mode='' 

Zero auto-increment values are not recommended because they're not set as default in MySQL databases.

For more info check mysql dev page topic on this

Update

For MariaDB use the command pointed out in this comment

SET sql_mode='NO_AUTO_VALUE_ON_ZERO' 
like image 20
I.G. Pascual Avatar answered Oct 05 '22 19:10

I.G. Pascual