How do I change the amount auto_increment fields in MySQL increment by from the default (1) to n?
If you want to change autoincrement step from 1 to N then there is a solution.
It could be done on MySQL server side:
look for '--auto-increment-increment' startup option or use following command SET ##auto_increment_increment=2;, but be warned that this is a server wide change (all tables will increment by 2).
Unortodox solutions could that could be considered:
Launch two MySQL servers on same machine, with different ports (one with auto_increment_increment=1 other with auto_increment_increment=2)
Use some serverside magic (PHP, ASP ,???) combined with turning off tables auto_increment to manually calculate (simple peek at last id and +=2 would be ok) and provide id in INSERT query.
Some official MySQL FAQ
You can change it using ALTER TABLE:
ALTER TABLE table AUTO_INCREMENT = n;
Or if you want to do set it from start:
CREATE TABLE table (...) AUTO_INCREMENT = n;
You can also use
ALTER SEQUENCE sequence_name INCREMENT BY N where N is the new incremnent value.
alter table <table name> auto_increment=n
where n is the number you want to start
Related
I have a table with two columns:
caseId, referring to a foreign table column
caseEventId, int, unique for a given caseId, which I want to auto-increment for the same caseId.
I know that the auto-increment option based on another column is not available in mySql with InnoDb:
MySQL Auto Increment Based on Foreign Key
MySQL second auto increment field based on foreign key
So I generate caseEventId into a trigger. My table:
CREATE TABLE IF NOT EXISTS mydb.caseEvent (
`caseId` CHAR(20) NOT NULL,
`caseEventId` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`caseId`, `caseEventId`),
# Foreign key definition, not important here.
ENGINE = InnoDB;
And my trigger:
CREATE DEFINER=`root`#`%` TRIGGER `mydb`.`caseEvent_BEFORE_INSERT` BEFORE INSERT ON `caseEvent` FOR EACH ROW
BEGIN
SELECT COALESCE((SELECT MAX(caseEventId) + 1 FROM caseEvent WHERE caseId = NEW.caseId),0)
INTO #newCaseEventId;
SET NEW.`caseEventId` = #newCaseEventId;
END
With this, I get my caseEventId which auto-increments.
However I need to re-use this new caseEventId in further calls within my INSERT transaction, so I place this id into #newCaseEventId within the trigger, and use it in following instructions:
START TRANSACTION;
INSERT INTO mydb.caseEvent (caseId) VALUES ('fziNw6muQ20VGYwYPW1b');
SELECT #newCaseEventId;
# Do stuff based on #newCaseEventId
COMMIT;
This seems to work just fine but... what about concurrency, using connection pools etc...?
Is this #newCaseEventId variable going to be shared with all clients using the same connection, can I run into problems when my client server launches two concurrent transactions? This is using mysql under nodejs.
Is this safe, or is there a safer way to go about this? Thanks.
Edit 2020/09/24
FYI I have dropped this approach altogether. I was trying to use the db in a way it isn't meant to be used.
Basically I have dropped caseEventId, and any index which is supposed to increment nicely based on a given column value.
I rely instead on properly written queries on the read side, when I retrieve data, to recreate my caseEventId field...
That is no problem, the user defined variables a per client.
That means every user has its own use defined varoables
User-defined variables are session specific. A user variable defined by one client cannot be seen or used by other clients. (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits.
see manul
I have a SQL database with a table inside called members and inside that there are some columns, one being an ID which auto-increments.
However I have done a few tests, and the auto-increment does work. But even after deleting the tests the auto-increment will not start from 0 again.
How do I make it will start back from 0 rather than carry on from about 17 or something...
EDIT:
I have worked out the answer:
In the "Operations" tab in phpMyAdmin there is a section called Table Options.
In there you can edit where the auto-increment continues from.
Assuming You're using MySQL:
To reset the next value of *auto_increment* column, you need to use ALTER TABLE statement in the following form:
ALTER TABLE my_table AUTO_INCREMENT=123
(Where "123" is the new next value)
If u are using postgresql u have following sql statement to alter sequence
ALTER SEQUENCE table_name_id_seq RESTART WITH 1
for example, if u have table called users, then u should do
ALTER SEQUENCE users_id_seq RESTART WITH 1
where 1 is the new sequence.
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.
I have a django script which loads data, the beginning of the script deletes all datas in database.
So when I execute 1st time this script, the auto increment primary keys begin to 1 to 15 (if 15 objects) and if I want to reload data, I reexecute the script.
My issue is when I execute it again, pks numbers begin to 16 (for 2nd launch), I would like each time auto_increment begins to 1, is it possible whitout regenerating tables structure each time ?
Thanks
You could use ALTER TABLE, but I'm not sure that is that much better than just regenerating the schema.
ALTER TABLE table AUTO_INCREMENT = 1;
When you delete rows from the database, you're not really:
Freeing up the disk space
Resetting the auto_increment values as you've noticed.
As such, it might actually be a better idea to drop the table and re-create it as required. Failing that you can use just either:
TRUNCATE <table name>; (Depending on your storage engine, this will actually drop/re-create as mentioned above for you.)
ALTER TABLE <table name> SET AUTO_INCREMENT = X;
Of these, I'd recommend using the truncate approach.
I'm running into a really, really, really weird problem with mysql.
I have a primary key, "id". It's set to auto increment. Problem is, first entry started at "3". And every new entry increases by 5, so the next entry's id is 8, the next is 13, then 18, so on. This is stupid. Why isn't it just incrementing by 1, like it should be? And why is it starting at 3???
Is there some setting somewhere I'm missing? I'm using phpmyadmin, if that helps.
There's a my.cnf configuration for that: auto_increment_increment. It's used for master-master server setups to prevent the same key from being defined twice by two different servers. So using that coupled with auto_increment_offset, it allows each server to always generate unique ids...
So, from what you're describing, it sounds like you have this:
auto_increment_increment = 5
auto_increment_offset = 3
The auto increment is probably set to 5. Try:
ALTER TABLE YourTable AUTO_INCREMENT = 1;
You can retrieve the current setting with:
SHOW TABLE STATUS LIKE 'YourTable'
See the MySQL docs for more details.
it appears the table was created with an increment set to 5. you can change it back to one with the following:
ALTER TABLE tbl AUTO_INCREMENT = 1;