create a new table with auto_increment of 10 each time - mysql

how do you create a table with an auto_increment_increment of 10 instead of the default of 1
I am using mySQL and mySQL workbench as well.
After creating the table either with the workbench gui or by statements, I have tried this in workbench and it works only when I add a new record from the workbench but not thru my web app. If I use the web app it starts to auto increment by one again...I just want to create the table and set its increment to 10 every time so first item is 10 then second is 20 then 30 and so on.
SELECT Auto_increment FROM information_schema.tables WHERE table_name='items';
SET ##auto_increment_increment=10;
Thanks.

You would need to set the auto_increment_increment=10 globally. The SET command you show only sets it for the current session.
Setting it globally makes it affect every table, not just your items table.
There's no support in MySQL for changing the increment size to a different value for each table.
You can change a global option in MySQL with SET GLOBAL. To make the change persist when the MySQL server is restarted, you must edit the options file. Read:
https://dev.mysql.com/doc/refman/5.6/en/using-system-variables.html
https://dev.mysql.com/doc/refman/5.6/en/option-files.html

Related

Setting auto_increment_increment = 1 effect on existing tables

I created a new MySql table and saw that the auto-increment field starts with a value of 3 and increments by 2. I do a SHOW VARIABLES LIKE 'auto_inc%'; and get this-
auto_increment_increment 2
auto_increment_offset 2
I did some research and found that I need to use-
SET ##auto_increment_increment=1; and
SET ##auto_increment_offset=1;
But my question is, will setting both these values to 1 affect how rows are inserted on other tables with auto-increment fields? Will the new rows inserted on those tables start with an auto-increment id of 1 now? Or will this only affect new tables going forward?
The effect of auto_increment_increment and auto_increment_offset is not per-table, it applies to all tables you insert into.
If you use SET in your own session, the variable will be changed in your session only. The behavior in other sessions will not change. Also if you disconnect and reconnect, your session settings are reset to the global settings.
To make the change global, you need to use SET GLOBAL. But the change will be undone the next time MySQL Server restarts.
To make the change global and persistent, either edit the my.cnf file so the variable is set every time MySQL Server starts, or else in MySQL 8.0 they added a feature so you can now use SET PERSIST so you can change global variables and it will retain the setting after a restart.
You can read more about this:
https://dev.mysql.com/doc/refman/8.0/en/using-system-variables.html
https://dev.mysql.com/doc/refman/8.0/en/set-variable.html

Changing auto-increment ID in MySQL table of Rails application

I have read this:
Changing the current count of an Auto Increment value in MySQL?
The max ID in one of my tables is 1382, and I would like the next entity created to start at 5000.
I could do ALTER TABLE tablename AUTO_INCREMENT = 5000
Is there any danger of doing this in a Rails Application?
This needs to be run on database ,on rails application we just use adapters to connect to database .I don't see anything danger doing this.

auto increment not starting from 1

I am inserting some data to a database and I have an id column which has auto increment. I updated my xampp server yesterday and now the auto increment is starting from 4, 3, 5 in different tables. It used to work fine before. I did not delete any rows from the table it just starts from those numbers. What is wrong?
ALTER TABLE tablename AUTO_INCREMENT = 1
This will reset your auto increment to start from 1
If you really want to reset this, in phpMyAdmin, open this table, go to Operations and change the value for AUTO_INCREMENT.
You can reset your auto increment ID from the following Command:
ALTER TABLE TABLE_NAME AUTO_INCREMENT = 1;
This will start from 1.
OR
go to LOCALHOST/PHPMYADMIN enter:
select your DB
after choose the specific table than go to Operations an set a number to the Auto increment.
If you really want to reset this, in phpMyAdmin, open this table, go to Operations and change the value for AUTO_INCREMENT.----- this is working for me, i have just done this,
This can be a consequence of using a Galera cluster. By default, it will automatically adjust the auto_increment_increment and auto_increment_offset variables according to the size of the cluster, and when the cluster size changes. This avoids replication conflicts due to auto_increment.
The short answer is not to depend on auto_increment numbers being subsequent, without gaps.

Trying to reset the auto increment value of a table

First off, I've gone through the normal steps to try and reset a value, GUI and CLI, it says x rows affected.
I have all the permissions I would need to do this(any amazon specific ones i don't know about aside), locally the queries work fine.
Is there a way to modify my tables auto increment number so that it starts at say 19, and not 1,000,021?
Queries I've tried.
ALTER TABLE X AUTO_INCREMENT = 19
I have all permissions granted to all tables, and I have even tried granting ALTER specifically to my user on the specific db.table.
Any insight or a push in the right direction would be lovely.
Your command:
ALTER TABLE X AUTO_INCREMENT = 19
will work only if your table does NOT have row with id 19 present. If you try to insert new row which would cause insertion conflict, it will automatically reset to maximum existing value of your auto-increment column + 1. In other words, if you have rows with id 19 and 1000020 (maximum), it will automatically reset to 1000021 on any attempt to insert.

How to configure the structure of a mysql database

Is it possible to restore the previous state of a mysql database through phpmyadmin?
I have one column in 4 of the tables that have the auto increment.
And my problem is how to begin again in the count of number 1 when I try to add a data.
I tried deleting all the records then add a record but it doesn't start with 1.
How do I do this without building the database over again by typing and selecting the data types.
What I want to do is to start the counting from 1 again. Is it possible?
Open phpMyAdmin and then select the DB you wish to change from the top left panel.
Now click the "query" button also top left to open a query window where you can then run the sql code below to reset the auto increment count for your table.
ALTER TABLE your_table_name AUTO_INCREMENT=1
If you dont care about the data in the table you can just hit the empty button (make sure you are on the right table). That will tuncate your table and anything you add after that will start at 1. If you set a field to auto increment then each item after that will increase by 1.
Have you tried ...
ALTER TABLE t2 AUTO_INCREMENT = 1;
It will reset the value to the smallest value allowed. Other than that you are out of luck.