While implementing mysql in my project it is found that if we restart mysql service AUTO_INCREMENT of that table is reset to value less than the current maximum value in the primary key column.
According to this, suppose one of table having ID (As primary key) up to 100 records and if DELETE is performed on 100th ID and restarted mysql service and performed INSERT then it will again add new row with ID 100. INSERT without restart of mysql service will add 101 ID.(like 1,2,3,4...99,101).
How to avoid this? Means after next restart it must start with 101; Is it possible so?
Related
I Use Delphi 10.2, MySQL. I have a table that has about 50,000 records and has an Auto_Increment primary key. It has suddenly, and on it's own with no help from me, started trying to re-insert old key values. As a matter of fact, it started over with the value 1. I have no idea how to fix this and I hope you might be able to help.
Thanks,
Jim Sawyer
If the MySQL table is defined with an auto increment primary key then you should never specify the key value. MySQL should not re-use old key values, but you may want to check if there is any table corruption. You can also reset the table's auto-increment value using an ALTER TABLE command. (There's a tutorial on this here: https://www.mysqltutorial.org/mysql-reset-auto-increment)
You can use the Firedac monitoring to confirm whether or not you are sending the primary key to MySQL - set you connection to be monitored using the FireDAC component - they supply a monitoring tool that you can setup to see all of the SQL being transferred. Normally the Firedac layer would do an insert with no primary key and then use LAST_INSERT_ID to update the TField to have the actual value inserted.
If you are sending the wrong key then alter your logic so you don;t send the primary key on an insert.
you can reset the autoincrement value to any value you want with the following command
ALTER TABLE <table_name> AUTO_INCREMENT = <new value>;
so if new value is 100, the next inserted record receives a value of 100.
I am creating a Django app where the primary keys are AutoFields. i.e. I am not manually assigning any field as primary key in my models.
I need to use mySQL.
I will need to export all the data to excel or perhaps another django app from time to time. Therefore the primary keys must be unique to be able to identify new records or records to be deleted in excel/other app.
However, I have read that mySQL autoincrement counter resets to the max key when database restarts. This will result in reassignment of keys if the latest records were deleted.
I need to avoid this. No key should be reassigned.
How can this be done?
MySQL 8.0 now keeps the last auto-increment per table persistently. So it remembers between restarts, and does not reset the auto-increment.
https://www.percona.com/blog/2018/10/08/persistence-of-autoinc-fixed-in-mysql-8-0/
On azure I created a new MySQL Database instance. In this db I create a table using this script:
CREATE TABLE ROLES(
ID INTEGER PRIMARY KEY AUTO_INCREMENT,
ROLE_NAME VARCHAR(30) NOT NULL
);
Then I insert values using this script:
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('admin');
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('owner');
INSERT INTO `beezzy`.`roles` (`ROLE_NAME`) VALUES ('consultant');
after execution table contains such rows:
Why DB generates IDs like '11' and '21'?
I run the same script on my local machine and everything works fine. IDs was '1', '2', '3'
Please run the following query.
SELECT ##auto_increment_increment
If the value is more than 1 then set it to 1 by the following query:
SET ##auto_increment_increment=1;
Note: This change is visible for the current connection only.
EDIT:
In order to set it globally so that other connections can also see the change you need to set it for global and session too.
SET ##GLOBAL.auto_increment_increment = 1;
SET ##SESSION.auto_increment_increment = 1;
So other connections can see this change now.
More:
This value will be reset if you restart your MySQL server. In order to make this change permanent you need to write this variable under [mysqld] secion in your my.cnf [for linux] or my.ini [for windows] file.
[mysqld]
auto-increment-increment = 1
Your autoincrement is probably 10, however this is probably by design. Azure uses ClearDB which uses an autoincrement of 10 with a reason: namely replication.
When I use auto_increment keys (or sequences) in my database, they
increment by 10 with varying offsets. Why?
ClearDB uses circular replication to provide master-master MySQL
support. As such, certain things such as auto_increment keys (or
sequences) must be configured in order for one master not to use the
same key as the other, in all cases. We do this by configuring MySQL
to skip certain keys, and by enforcing MySQL to use a specific offset
for each key used. The reason why we use a value of 10 instead of 2 is
for future development.
You should not change the autoincrement value.
cleardb faq
Hy!
I have the following stupid question?
First an example to understand my point of view.
I have a mysql db, Innodb tables with foreign keys between them,
I currently work on a localhost machine
When I delete the last inserted record from a table let's say with primary auto-increment key set to
100 then the next primary key given by mysql is 101 but if I restart the mysql server (Apache server) then in the same table the primary key for the next record is reset to 100.
I have to mention that I set a trigger for the table I deleted the record from, to copy the deleted record to archive table before delete.
Now after mysql server was restarted if a new record is inserted it will get the primary key 100,and when I try to delete it a conflict appears because in the archive table there is already a primary key 100.
I have to keep the references between deleted key.
Now this happens only if mysql server is restarted.
The question is if this problem can be solved in case let's say that after the web application is deployed to a shared server host a server restart appears(I suppose).
I want to mention that the data base is moe complex the one table. I absolutely need to keep the integrity between archive tables after data is moved.
We are using in-memory tables in MySQL with an auto-incremented primary key.
After restarting MySQL the in-memory tables are emptied as expected.
However we would like to keep the auto increment value, so that the next inserted row will have the ID after the one that was used in the last session.
Is that possible?
After the restart you can use this statement to change the next value to use for AUTO_INCREMENT columns.
ALTER TABLE mytable AUTO_INCREMENT = new_value;
The CREATE TABLE syntax is similar, but since memory tables are recreated after the restart, it won't be much use to you.
If the timing of this call is important I would recommend adding it to the startup script; I wouldn't know any other way.
We could not use the ALTER TABLE statement because a function was necessary to calculate the initial auto increment.
The solution was to:
my.cnf file was changed, after the header [mysqld] we added init-file = [file_path]
The file would execute a insert on the table with a dummy value (e.g. INSERT INTO mytable(id) VALUES (UNIX_TIMESTAMP()). After this, the line can be deleted if wished.