Adjust MySQL ID column +1 - mysql

I have about 1000 rows, and want to increment all of the rows' ID number by 1. I was thinking something like this might work
UPDATE table
SET id = id+1
I tried in phpMyAdmin, but got this: Duplicate entry '2' for key 'PRIMARY'
This makes perfect sense, but how can I get around it?

Isn't it much easier to order it backwards?
update table set id = id +1 order by id desc
this works for me...

Follow the steps:
crate a temp table
copy all data to this temp table with desired id
delete original table
At the end rename the temp table to original table.
That's all.

use a big enough number that wont create duplicates
update table set id = id + 1000001;
update table set id = id - 1000000;
keep in mind this will increase the auto increment key if you are using one

Related

Update the path column in MySQL database for specific records

I have a column in MySQL database that store the images names/IDs something like "523523525.jpg".
I want to update this column by adding the folder before the image name/ID to be like "101/523523525.jpg". I want to update specific records not all the column, for example update from record 1 to 1000 by adding "101/...." and records from 1001 to 2000 by adding "102/....".
Look for your ideas.
Add an id column.
ALTER TABLE your_table ADD
COLUMN id INT AUTO_INCREMENT
PRIMARY KEY FIRST;
use concat to update rows based on id
UPDATE your_table SET
Column=CONCAT("101/",column)
WHERE id BETWEEN 1 AND 1000;
for first 1 to 1000 to use LIMIT in update statement.
For next 1001-2000 you need to write anonymous block or procedure using cursor to update records.
How about using the CONCAT function? Assuming you have a seperate column for the id, you can execute an update query to add the path to the existing value.
UPDATE your_table SET path_col=CONCAT('101/', path_col) WHERE id between 1 AND 1000;
Hope it helps! Feel free to ask if you need to know anything.

How to update the nth row in a SQL database table?

How can I update only the nth row from a table?
To update the second row for example, i tried using UPDATE and LIMIT, but it is giving me an error.
UPDATE database_table
SET field = 'new_value'
LIMIT 0, 1
Is there a way to do it?
If you have a primary key and a column you'd like to order by (to get the nth row), you could do something like:
UPDATE database_table
SET field = 'new_value'
WHERE primary_key IN (
SELECT primary_key
FROM database_table
ORDER BY some_date_column
LIMIT n - 1, 1
)
Edit: I should probably add a caveat. This answer might be technically correct, but you're likely wrong to use it. I can't imagine too many scenarios where you'd actually want to update the nth row of a table. You should generally only be updating tables based on primary keys. Updating the nth row will likely break your app if multiple users (or even multiple sessions with the same user) are using it at the same time.
The real answer is you should probably change your code to update based on primary key.
You would need some sort of id, and then do something like this:
UPDATE database_table SET field = 'new_value' WHERE id = 2

Reset the table Auto Increment and make it apply to the table

I have a table with around 10k rows which I've imported. The ID is a significant column to my application, and it has to be ordered. Currently, I got something like: 1,2,3,4,5....5789,9275,9276.....
It jumped from 5789 to 9275. Is there any way I can reset the Auto Increment but also make it apply to the table? which means, now it will start giving them IDS all over again from 1 to 10k
Thanks!
ALTER TABLE <tablename> AUTO_INCREMENT=<new_value>;
Of course you need to fix the high IDs and all references to them manually.
However, why do you care? Does it really matter if there's a hole in the IDs? If yes, you might want to use a separate column that's always set to MAX(col) + 1 instead of an AUTO_INCREMENT column.
You can certainly reset the auto_increment value to be whatever you want by simply issuing this query:
ALTER TABLE <tbl> AUTO_INCREMENT = <n>;
where tbl is your table name and n is the value to start it at. However, if you have existing IDs in that table already, I believe it will simply set the next inserted items ID to be max(id) + 1 of the ID column

Increment autoincrement id field by one

I have a MySQL 5 server and a table in it with an autoincrement on an id field (primary key). Now I want to add a record in between and so I have to increase all other ids by one. This is what I tried:
UPDATE myTable SET id=id+1 WHERE id >= 53
This doesn't work because for example a record with id = 52 already exists. How can I do this? If he would start at the last entry and makes the updates it should work I think. But how?
I see no good reason for this. Only problems. Before running the folowing statement, check if you have FOREIGN keys defined, that reference this id. Are they set to ON UPDATE CASCADE? Also, do you have any triggers that are related to this table?
But first consider, why you (think you) need this. Is it going to be used for ordering the table? In that case, as #Mark pointed, you should use a separate column to specify your desired order.
If, however, you decide you really want this, use:
UPDATE myTable
SET id = id + 1
WHERE id >= 53
ORDER BY id DESC ;
Quick and dirty you do it in 2 steps.
increase the id to a number higher that all others
decrease all ids to the number you want
Like that
UPDATE myTable SET id=id+10000 WHERE id >= 53
UPDATE myTable SET id=id-9999 WHERE id >= 53
I had faced same problem. And also tried with query by OP and #Romain Hoog . But not succeeded.
Finally exported data of whole table in excel and done it in excel (not one by one but using trick that makes it very fast).
then taken backup of original table recreated new table and imported data from updated excel .
I guess something like that works :
UPDATE myTable SET id=id+1 WHERE id >= (select id from myTable order by id DESC)

mysql change the column id to start from 1 and auto increment from there

I was looking at my table in phpmyadmin and noticed that the id starts at 13410 and increase by 1. I would like to reset and start from one. I've read many people say its better to leave it alone or its going to get complicated if you messed with it but I still need a solution to at least start the id at 1.
Thanks
ALTER TABLE table AUTO_INCREMENT = 1
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
I hope you know what you do!
update *tablename* set id=id-13409;
then find the highest id: select id from tablename order by id desc limit 1;
then reset the auto_increment: alter table tablename auto_increment=12345; (change 12345 to the highest ID plus one)
You can reset the auto increment to desired value using the following statement ,
eg : mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;