Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the attribution of an auto incrementing primary key when inserting a value in a MySQL table?

Tags:

mysql

I have a MySQL table of users whose primary key is an auto-incrementing integer. The table is already populated with records whose key is between 0 and 30.000. Not all records are there, though. Some users have been removed and therefore I have "holes".

Now the client has realised they removed a bunch of users by mistake, and they now want me to reinsert those users keeping the same ID they had, for compatibility with the back-end of the e-commerce, which runs on a different machine altogether, but uses the same ID's for the customers.

At the moment I am:

  1. altering the structure of the table, by removing the auto_increment property from the ID
  2. adding the records I need, specifying their ID
  3. reverting the alterations to the structure of the table.

Is there a better way to achieve this? Is there any SQL override function that would allow me to force MySQL to accept a value that is unique but not necessarily "the next number in the line"?

like image 312
mac Avatar asked Dec 10 '09 22:12

mac


People also ask

Can you override auto increment?

You can insert into an auto-increment column and specify a value. This is fine; it simply overrides the auto-increment generator. If you try to insert a value of NULL or 0 or DEFAULT , or if you omit the auto-increment column from the columns in your INSERT statement, this activates the auto-increment generator.

How do I reset my auto increment primary key?

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.

How do I change auto increment?

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.

Can we insert auto increment value in MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.


1 Answers

You don't have to disable the auto_increment feature. When you insert a row into the table and you do specify the primary key value in the row, the id you want is stored in the database. The auto_increment is only used, when you omit the primary key field.

EDIT: I thought I might give examples for that:

mysql> describe test; +-------+------------------+------+-----+---------+----------------+ | Field | Type             | Null | Key | Default | Extra          | +-------+------------------+------+-----+---------+----------------+ | id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment | | value | varchar(45)      | NO   |     | NULL    |                | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.02 sec)  mysql> insert into test (value) values ('row 1'); Query OK, 1 row affected (0.06 sec)  mysql> select * from test; +----+-------+ | id | value | +----+-------+ |  1 | row 1 | +----+-------+ 1 row in set (0.00 sec)  mysql> insert into test values (15, 'row 2'); Query OK, 1 row affected (0.03 sec)  mysql> select * from test; +----+-------+ | id | value | +----+-------+ |  1 | row 1 | | 15 | row 2 | +----+-------+ 2 rows in set (0.00 sec) 

EDIT 2

mysql> insert into test (id, value) values (3, 'row 3'); Query OK, 1 row affected (0.00 sec)  mysql> select * from test; +----+-------+ | id | value | +----+-------+ |  1 | row 1 | | 15 | row 2 | |  3 | row 3 | +----+-------+ 3 rows in set (0.00 sec) 
like image 178
Dan Soap Avatar answered Sep 24 '22 18:09

Dan Soap