Hello friends, I had doubts about something about the AUTO_INCREMENT in mysql
I was reading the documentation but they don't mention it so I assume that it is not possible to set my auto_increment to increase NOT by 1 by 1 but by 15 by 15 for example.
try to do it like in SQLServer by setting in my:
CREATE TABLE table(
id int not null primary key auto_increment=40
);
However, it shows me an error in the simple fact of declaring it like this.
My question is: Is there a way to make my INSERTS in each table with id AUTO_INCREMENT other than one by one? Is there an alternative to set it?
Greetings Friends!
Do you really want all tables to increase their auto-incremental value at a certain default rate? If that were the requirement (which would be weird), be aware that if you change the configuration at the handler level for all tables, all inserts that occur after you've made that change will assume that increment rate, in the active session. if you make a change at the current connection or session level, or worse, on all future inserts if you change the global setting.
If you want all present and future tables 1 of that database to have this characteristic, it is evident that it would be best to make a change at the configuration level.
Now, if you only want that to happen in a specific table or in some tables (which would be the most normal), it would be better to program one
TRIGGER
that does it.Something like that:
Thus, every time an insert occurs in that table , it will
TRIGGER
set the value ofid
to the value ofMAX(id) + 1
.If currently
id
equals1
, on the next inserts:Grades
id
altered ones. What if there are several million records when you come to realize the error? Personally, I don't think it's a good idea to change the auto-increment rate at the database configuration level.Please run the following query.
The value should be 1, set it to whatever value you require.
To configure it globally so that other connections can also see the change, you need to configure it.
Now other connections can see this change.
Plus:
This value will be reset if you restart your MySQL server. To make this change permanent, you must write this variable to the [mysqld] section of your my.cnf [for linux] or my.ini [for windows] file.
you can try with: