How to update existing AUTO NUMBER Values in MYSQL - mysql

I have created a MYSQL table with autoincrement id, and inserted appx 10K values, so now id starts from 1 onwards.
Now I would like to change my existing ID from 10001 onwards.. i.e. my existing id 1 should starts from 10001..next 10002...
So how it possible without affect other data's
Thanks,
Laxmilal Menaria

Try this:
UPDATE your_table
SET id = id + 10000

To "correct" old values do this:
UPDATE TABLE `tbl` SET `id` = `id` + 10000;
If you want all new autoincrementing values to start at 10001, do this (it will automatically skip numbers that are already set; so, if you already have 10001, it will start with 10002):
ALTER TABLE `tbl` AUTO_INCREMENT = 10000;

Related

Insert and fill a non restricted field with the new unique key autoincrement id value

I have a table with two fields:
Unique Id autoincrement -> Id
int -> copyOfId
Id can be written manually, or can be assigned by database but copyOfId.
To set the same value to Id and copyOfId, I can do this:
set #nextId = (select id from table order by id desc limit 1) + 1;
insert into table (Id,...,...,...,copyOfId,...,...,...
) VALUES (
#nextId,...,...,...,#nextId,...,...,...);
Is it possible to set the copyOfId with the same Id on an Insert automatically?
Is it possible to set the copyOfId with the same Id on an Insert automatically?
No. Neither BEFORE trigger nor generated column may refer to autoincremented column.
But you may remove autoincrementing, create separate table with autoincremented column, and set both id and its copy from BEFORE trigger.
Example fiddle with some explanations.
Pay attention - the values for id and its copy provided in INSERT will be overwritten unconditionally. If you need to set them manually to some N then insert a row with N-1 value (which must be above current maximal value) into additional table explicitly then insert into working table.
Do not update PK value in working table with the value above current maximal value.

mysql auto increment from 00001 - 99999 and reset the value every year

My first aim is to generate customer reference code automaticaly everytime when I insert a new customer
so when it shown in my nodejs it should be : "MS2200001"
So my idea is set id from customer table (mysql) with auto increment and zerofill (int)
length = 5
So I can get id 00001
and insert to another column named as "customer reference"
with
("MS" + (2022)+ "00001")
And I am trying to reset the counter to 00001 again if become 2023,2024,2025 etc.
How can I archive this in phpmyadmin or I should chnage my idea?
Use trigger-generating technique and additional MyISAM table with secondary AUTO_INCREMENT column in PRIMARY KEY.
An example:
-- base table for complete identifier generation
CREATE TABLE base_for_complete_id (
`year` YEAR,
id INT AUTO_INCREMENT,
PRIMARY KEY (`year`, id)
) ENGINE = MyISAM;
-- create trigger which will generate complete identifier
CREATE TRIGGER generate_complete_id
BEFORE INSERT ON maintable
FOR EACH ROW
BEGIN
DECLARE tmp INT;
-- insert row into base table
INSERT INTO base_for_complete_id (`year`) VALUES (YEAR(NEW.created_at));
-- store id generated for current year
SET tmp = LAST_INSERT_ID();
-- save generated complete identifier into main table
SET NEW.complete_id = CONCAT('prefix_', YEAR(NEW.created_at), '_', tmp);
-- clear excess rows from base table
DELETE FROM base_for_complete_id WHERE `year` = YEAR(NEW.created_at) AND id < tmp;
END
DEMO fiddle
If you need to format id part of generated value with leading zeros then use LPAD() function, for example SET tmp = LPAD(LAST_INSERT_ID(), 5, 0);.
Caution! If the value for generated number exceeds 99999 then it will be truncated, and only 5 leading digits will be stored.

A command in MySql to increment by one the ID column

This is my case:
What I want is to increment all rows after the raw of 135th by one, for e.g: 136,137.
I have been trying to make some updates but none of them works.
Can you please help me?
Thanks in advance
It fails because the consecutive ID's already exist. What you could do is first increasing you ID's to non-existing values, then decrease them to the values you want.
Example
Increase the ID's with a bigger amount than the highest ID, i.e.:
UPDATE tablename SET id = id + 100000 WHERE id >= 135;
Then decrease them to the values you wanted (in this case 100.000 minus 1)
UPDATE tablename SET id = id - 99999 WHERE id >= 100000;
Reset the auto increment
ALTER TABLE tablename AUTO_INCREMENT = 1;
Try
UPDATE `tablename` SET `id` = `id` + 1 WHERE `id` >= 135 ORDER BY `id` DESC

set default values in mysql Id column?

I want to start my mysql table id as todaysdate+000001 like 20120602000001 OR 20120602/000001 and that when next entry made then ID should be 20120602000002 OR 20120602/000002 (means it should get auto_incremented also).
HOw can we do this?
my table structure is
table abc(id int auto_increment,name varchar(50),company varchar(50),contact varchar(10),persontomeet varchar(30),intime TIMESTAMP CURRENT_TIMESTAMP,primary key(id));
but id starts with 1 obiviously..
I want to start it as 20120602000001 and when next time INSERT triger will trigger then it should get set to 20120602000002
please help me...
Use:
ALTER TABLE tableName AUTO_INCREMENT = 20120602000001

MySQL: Auto increment with even or odd numbers only?

Is it possible to have auto increment field which generates even or odd numbers (skips the opposite)? The reason I am asking is because I want to share auto increment between two tables. Other ways for achieving it are welcomed.
Thanks in advance!
As mentioned by Juergen you could do that at system level instead of session level by making following change (or adding) in my.ini file:
auto-increment-increment = 2
auto-increment-offset = 1
or
auto-increment-increment = 2
auto-increment-offset = 2
Basically this is much used in Master-Master replication setup.
try
SET ##auto_increment_increment=2;
SET ##auto_increment_offset=2;
You can use triggers to achieve custom autoincrement functionality.
Create table to store custom autoincrement values and insert one row with initial values:
CREATE TABLE autoincrement_id (id_even INT, id_odd INT);
INSERT INTO autoincrement_id VALUES (0, 1);
Create triggers, that will modify inserted row's id value accordingly:
CREATE TRIGGER set_id_in_sometable_with_odd_id BEFORE INSERT ON `sometable_with_odd_id`
FOR EACH ROW
BEGIN
SET NEW.id = (SELECT id_odd FROM autoincrement_id LIMIT 1);
UPDATE autoincrement_id SET id_odd = id_odd + 2;
END;
CREATE TRIGGER set_id_in_sometable_with_even_id BEFORE INSERT ON `sometable_with_even_id`
FOR EACH ROW
BEGIN
SET NEW.id = (SELECT id_even FROM autoincrement_id LIMIT 1);
UPDATE autoincrement_id SET id_even = id_even + 2;
END;
You can offset one table's auto increment field from the other, i.e. one table starts ids from 1 while the other starts from 1000000 (or some other value chosen depending on your usage pattern).
CREATE TABLE table1 (id BIGINT UNSIGNED AUTO_INCREMENT);
CREATE TABLE table2 (id BIGINT UNSIGNED AUTO_INCREMENT) AUTO_INCREMENT = 1000000;
You can also choose your autoincrement column type according to your needs. BIGINT UNSIGNED's range is 0..18446744073709551615, which should cover most cases.