Changing auto-increment ID in MySQL table of Rails application - mysql

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.

Related

How to Clear all Values in MySQL DB in all tables?

I have an application with a MySQL Database. The application is currently under testing. After testing I want to delete all testing data in the database(but keep all tables as it is). I want to refresh all tables in the DB. If i just delete values from the DB, the AUTO_INCREMENT columns do not start from 1.
How do I refresh the DB so all values are gone?
You just need to truncate all your tables.
after truncating all your Auto Increment starts from 1 instead of where you left.
here is the link for more info
TRUNCATE TABLE table_name;

create a new table with auto_increment of 10 each time

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

Is there a way to stop increasing the auto increment value in mysql when running specs?

I have about 100 specs, In a Sinatra project I'm currently developing.
For testing I'm currently using rspec + DatabaseCleaner with the :transaction strategy.
Some of those specs require creating new records in the database and thus they increase the auto increment table value in mysql. Is there a way I can run my tests with the transaction strategy, without increasing the auto increment value using DatabaseCleaner or another gem?
Edit: I would like for auto_increment to have the value it had before the tests ran.
Reset the value to some known value after tests. Have a look here...
How to reset AUTO_INCREMENT in MySQL?
I am assuming that at the time of test no record will insert from production or outside test.
First get last auto_id from your table by below command-
select max(id) from mytable;
Suppose it was 240125
Now start your test and after completion your test do as per below-
First delete all newly created auto_ids-
delete from mytable where id>240125;
Now set auto_increment from this possition.
alter table mytable AUTO_INCREMENT=240126;

MySQL backend & Access (.accdb, 2013) Frontend Auto Increment Issue

I have a MySQL database that the tables are each set up with an ID field which is an auto increment and primary key. When I use access to link to this database and tables in table design view in Access the fields are listed as the primary key but the type is number, not auto number. This is causing an issue when trying to create a new record because the ID field is not being populated automatically. Please help.
The autonumbering property of the id fields is set at the level of the MySQL database. Access might not be able to identify this property when connected to the back end database. This is why it considers it as a number, and not an autonumber, field.
This said, when inserting new records in the MySQL database, you do not need to send any value for the id field, so the MySQL engine automatically fills it with the updated autonumber value.

Postgresql setting next id to write to

I recently migrated a database from mysql to pgsql 9.
But now when I try to create a new object (in django admin) it tells me that the id I'm trying to use (started at one and has increased each time I tried) is already used.
I'm guessing that there is a pointer or index which needs to be set to the last used id. Am I correct?
When you define your table, the PostgreSQL equivalent to 'auto_increment' is:
CREATE TABLE foo (
id SERIAL,
...
);
If your table is already created (as I suspect it is), you can add this manually:
CREATE SEQUENCE foo_id_seq;
ALTER TABLE foo ALTER COLUMN id SET DEFAULT nextval('foo_id_seq');
Note that if you want to stick with the default name that Pg would have given you use the following format for your sequence name:
<table name>_<column name>_seq
Thus in my example, foo_id_seq.
If the table was migrated and it uses serial to replace the mysql auto increment column, your data was probably migrated without incrementing the serial sequence. Look up the postgresql setval function to set your sequence to a value above the highest existing key in your table.