how to set default autoincrement value in mysql 8 - mysql

I have users table which autoincremented id automatically increased to high amount like 996165754 and I want to set autoincrement default value should start from 1260
how can I fix it?

MySQL's auto-increment mechanism will not generate values lower than the max id value.
You can insert lower values if you specify them yourself, but you can't get auto-increment to make one. It will always generate a value at least one greater than the highest existing value.
You can try ALTER TABLE mytable AUTO_INCREMENT=1260 but you will find it automatically raises to max(id)+1 even if you specify a lower value.
Your only solution is to eliminate the high values, either by deleting the rows or changing their id values, and then ALTER TABLE to reset the next auto-increment value.

There is no any reason to alter this value. Autoincremented primary key column must be used for referential and integrity checking purposes exclusively. In general nobody must affect it, and even there is no reason to see it. It's not meant for you.
If you need a column with small numbers - then create one more column for this purposes and fill it with the values which you need. Or enumerate rows in a query.
You cannot set autoincrement autoassigned values to a value less than maximal value already present in a table.

Related

constraint a value to be repeated up to 3 times in the same column in mySQL

When i am making the table (A) , i want to add a constraint that wont allow to add more than 3 times the same value on the same column .
I thought i would make another column (COUNT) that the user would have to count how many times they gave the value and insert it into the new column and then use this constraint :
ALTER TABLE A
ADD CONSTRAINT A check(COUNT<=3);
But that would require too much from the user , and it would probably fail .
Is there an easiest way to do it , that doesn't rely on the user?
Here are three options:
(1) Use a trigger to count the number of values in the column. This would be an insert/update trigger that would be sure the 4th value is not added.
(2) Create a user-defined function that counts the number of values in the column and add a check constraint to ensure the value is 3 or less. This is not currently allowed in MySQL but one day it might be.
(3) Add a counter column in a parent table and then add a check constraint to ensure that the counter is never more than 3. This also requires insert/update/delete triggers to keep the count up-to-date.
In general, I think I prefer (3) because having the counter in the parent table is often convenient. And although it uses triggers, the failure logic is clear -- it is not hidden in a trigger, it is in a check constraint.
I should suggest a fourth option. That is to pre-populate the table with three rows per user. Then only allow users to update the rows rather than insert/delete. This requires a flag to indicate whether the row has a value.

Can Autoincrement field ever use the same value twice?

I have a table1 with an id field, type AutoIncrement. I need to copy the entire record from table1 into table2 if there is no record with the same id in table2. Then I delete the record from table1.
I need to know that if table1 gets new records, the id field will never be a number that was ever used before. Does this happen automatically, or do I need to do something to ensure this?
I tried deleting some records and adding new ones, and it really didn't use the same id, but I'm not sure that this is what always happens.
It is possible to duplicate numbers in autoincremet field quite easy, but normally applications don't work this way.
Access remembers last inserted value in autoincrement field and uses it for calculating next value. You cannot insert particular value into autoincrement field using table designer or recordset in VBA, but it's possible if you use INSERT SQL statement. So, if autoincrement field has no unique index, you can insert any value. Also if you insert value less than maximum existing number, Access will generate duplicates automatically.
So I would not recommend rely on unique autoincrement numbers without unique index.
INSERT SQL can be used for resetting numeration without dropping field/table, just run query like this in query builder or using VBA:
INSERT INTO Table1 ( id ) SELECT 1;
This is table with autoincrement field ID I just created:
it is really so, Auto-increment fields in MS Access are always incremental, even if records are deleted, database compacted, etc.
The proposed number can be reset deleting the auto-increment field, perform the copy of the table and then adding the auto-increment field again.
Auto increment never uses the same # even though it's deleted from the table.
It requires complete reset so that it will start from the base and create new #.

How to control auto increment id?

I have an entity with a strategy to auto generate an id based on an integer column in MySQL. Things work, but while testing exceptions and related rollbacks, I noticed that MySQL does not reset last incremented value.
So a successful save produces entity id 1
An attempted save gets entity id 2 but is rolled back.
Then a successful save of a new entity gets entity id 3.
Consequently, in the table we have two records. One with id 1 and the other with id 3.
Are there any ways to control this? Basically, in the scenario I have just described, I would like to see two entities: one with id set to 1 and the other with id set to 2.
No, you can't change that. That is how it is supposed to be.
An auto-increment id has to be unique. That's all.
Auto-increment numbers have to be unique, but they don't have to be consecutive. They are monotonically increasing only as a coincidence of their implementation.
You can always insert a specific value and bypass the auto-increment mechanism. But you'd have to know what value is the "next" value. To avoid race conditions, you'd have to lock the table, query the MAX(id)+1 and then insert that value.
And that's exactly what MySQL would have to do, too, if it were to do this automatically.
The way auto-increment works now allows maximum concurrency without race conditions. So it is by design that it "loses" some values from time to time, when you rollback an INSERT, or else if you subsequently DELETE a value.
You can handle it using your own auto increment logic.
Have a Max+1 idgenerator or have a table that maintains PK auto generated IDs of such tables.
A table like this
LastKey TableName
1 TableX
5 TableY
Everytime, you will have to query from this table to get the incremented id.

MySQL, autoincrement sequence?

In a MySQL database column that has been set to AUTO_INCREMENT, can I assume that the values will always be created sequentially?
For instance, if 10 rows are inserted and receive values 1,2,3,...10, and then 3 is deleted, can I assume the next row inserted will receive 11?
The reason I ask is that I'd like to sort values based on the order in which they were inserted into the table, and if I can sort based on the auto incremented primary key it will be a little easier.
From what I understand from the manual; yes. Each table has it's own 'next auto increment value' that is incremented by the amount defined in auto_increment_increment (http://dev.mysql.com/doc/refman/5.0/en/replication-options-master.html#sysvar_auto_increment_increment) and that is never automatically reset, even though it can be manually reset. But as #miku said, if possible a timestamp would be preferable.
I've seen auto_increment mainly used for the primary key column. If you want to sort items by say date_added you should create an extra timestamp, date/datetime or int (epoch) column.
This way you make your intent explicit and easier to follow - also you can safely migrate, export and import your DB without the need to worry about how auto_increment is handled.

How does MySQL Auto Increment work?

I was just creating a new table using MySQL Query Browser, and noticed there's a tick under Auto Increment Column. How does that work?
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Everytime a NEW user registers on my site, I want their Customer ID (integer only) to auto increment, so I don't have to try and randomly generate a unique number.
Can this be done simply?
Thank you!
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Yes, that's the way auto_increment works.
The value will be incremented for each new row
The value is unique, duplicates are not possible
If a row is deleted, the auto_increment column of that row will not be re-assigned.
The auto_increment value of the last inserted row can be accessed using the mySQL function LAST_INSERT_ID() but it must be called right after the insert query, in the same database connection
mySQL Reference
1 more,
You can insert your own value also (ie your random value).
Yes. Auto_Increment columns work like they say on the tin. Tips
when INSERT - ing, use NULL or omit the column
Use LAST_INSERT_ID() (or API equivalents) to obtain the last generated value.
for security and business logic reasons, it's usually better form to not directly use a key value for a customer identifier. Consider using Hashed / randomised surrogate customer keys instead.
Ta
Yes, that's the exact purpose of AUTO_INCREMENT. It looks at whatever is the current increment value for that table, and stores that value plus 1 for the new row that comes in, automatically. You can omit that field from your INSERT statements and MySQL will handle it for you for every new row that comes in, giving each row its own unique ID.
When you enable Auto Increment an ID will always get automatically added whenever a new record is made.. Example:
If you have 1 record with ID 1 in your table and you add a new record, the ID will automatically be 2.