The id's in one of my MySQL tables actually start from 34567 (34568, 34569, 34570…).
How can I reset all these id's and set 123 as start value: 123, 124, 125, 126…?
Or is there a way to make every id -34444: 34567-34444=123?
P.S. ALTER TABLE table_name AUTO_INCREMENT = 124; isn't what I'm looking for.
If you have deleted all table records and you want to start from 123 for the auto-increment field, then use the following command:
ALTER TABLE table_name AUTO_INCREMENT=123;
As others have said, you probably shouldn't worry about the values of the ids. By definition, they shouldn't have any real meaning anyway.
Having said that, you should be able to just update them as you would any other column:
update table_name
set id = id - 34444
You could then reset the starting AUTO_INCREMENT value so that the next row inserted has a sequential id:
ALTER TABLE table_name AUTO_INCREMENT = <whatever the maximum id is + 1>;
Be aware that doing that will rebuild the table, so it could take a while if it has many rows.
Is this what you are looking for:
select (id-34444) as id from your_table;
Related
I am trying to change the auto increment value in MySQL via a query
ALTER TABLE log_visit AUTO_INCREMENT = (
SELECT `AUTO_INCREMENT` + 500000
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME = 'visit'
);
but this query is not working at all.
Please let me know what needs to be done in case we want to change auto increment based on the last inserted value.
You're getting the SQL error, it's due to the fact that the column isn't named "AUTO_INCREMENT" (and you also can't do a select inside an ALTER query). The reason is why is because AUTO_INCREMENT is not a column, but a MySQL attribute that, obviously, automatically increments.
If you'd like AUTO_INCREMENT to start at a certain value, in this case, 500000, you'd need to modify the table like so:
ALTER TABLE `yourtable` AUTO_INCREMENT=500000;
But it's recommended that if you're going to be changing the ID column at all, you should do so during table creation.
Your syntax is incorrect.
If you look at the docs it's clearly stated:
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
this example sets the auto_increment to start at 100 from now on:
ALTER TABLE tbl AUTO_INCREMENT = 100;
I have a table with primary key (its name is "id") defined as auto_increment. I use NULL in INSERT statements to "fill" the id value. It works, of course. However now I need to "move" an existing record to a new primary key value (the next available, the value is not so much important, but it must be a new one, and the last one if ordered by id). How can I do it in an "elegant" way? Since the "use NULL at INSERT" does not work too much with UPDATE:
update idtest set id=NULL where id=1;
This simply makes the id of the record zero. I would expect to do the same thing as with INSERT, but it seems my idea was incorrect.
Of course I can use "INSERT ... SELECT" statement, then a DELETE on the old one, or I can use something like MAX(id) + 1 to UPDATE the id of the old record in one step, etc, but I am curious if there is a finer solution.
Also, the MAX(id) solution doesn't seem to work either by the way:
mysql> update idtest set id=max(id)+1 where id=3;
ERROR 1111 (HY000): Invalid use of group function
mysql> update idtest set id=(select max(id)+1 from idtest) where id=3;
ERROR 1093 (HY000): You can't specify target table 'idtest' for update in FROM clause
This is the way I believe:
UPDATE users SET id = (SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME = 'users') WHERE id = 2;
select * from users;
I used by own tables substitute yours.
test is database name, users is table name and id is AUTO_INCREMENT in my case.
EDIT: My Query above works perfect but its side effects are somewhat 'dangerous', upon next insert as AUTO_INCREMENT value will collide with this recently updated record so just next single insert will fail. To avoid that case I've modified above query to a transaction:
START transaction;
UPDATE users SET id = (SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME = 'users') WHERE id = 2;
#renew auto increment to avoid duplicate warning on next insert
INSERT IGNORE INTO users(username) values ('');
COMMIT
Hope this will help someone if not OP.
The way you are trying to update same table is wrong but you can use join on same table
update idtest t
join (select id +1 as id
from idtest order by id desc
limit 1) t1
set t.id=t1.id
where t.id=3;
or
update idtest t
join (select max(id) +1 as id
from idtest ) t1
set t.id=t1.id
where t.id=3;
You can use the REPLACE INTO clause to do the trick.
From the manual:
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 13.2.5, "INSERT Syntax".
EDIT
My mistake (in the comments) that you have to have two unique constraint to achieve this:
When you use the auto_increment value to REPLACE the record, the record will be replaced with the give ID and will not change (however the AI value will increment).
You have to exclude the AI column from the query. You can do that if you have one more UQ constraint.
Check this SQLFiddle demo: http://sqlfiddle.com/#!2/1a702e
The first query will replace all the records (but the id's value will not change).
The second one will replace it too, and the new AI value will be used. (Please note, that the second query does not contain the id column, and there is a UQ constraint on the some column).
You can notice, that the second query uses higher AI values than it is excepted: this is because the first replace incremented the AI value.
If you do not have two unique keys (one for the AI and one for another columns), the REPLACE statement will work as a normal INSERT statement!
(Ofcourse you can change one of the UNIQUE KEYs with a PRIMARY KEY)
I am a Linux admin with only basic knowledge in Mysql Queries
I want to delete many table entries which are ip address from my table using id,
currently i am using
DELETE from tablename where id=1;
DELETE from tablename where id=2;
but i have to delete 254 entries,so this method is going to take hours,how can i tell mysql to delete rows that i specify,coz i want to skip deleting some entries out of this 254.
Deleting whole table and importing needed entries is not an option.
The best way is to use IN statement :
DELETE from tablename WHERE id IN (1,2,3,...,254);
You can also use BETWEEN if you have consecutive IDs :
DELETE from tablename WHERE id BETWEEN 1 AND 254;
You can of course limit for some IDs using other WHERE clause :
DELETE from tablename WHERE id BETWEEN 1 AND 254 AND id<>10;
how about using IN
DELETE FROM tableName
WHERE ID IN (1,2) -- add as many ID as you want.
if you need to keep only a few rows, consider
DELETE FROM tablename WHERE id NOT IN (5,124,221);
This will keep only some records and discard others.
Something like this might make it a bit easier, you could obviously use a script to generate this, or even excel
DELETE FROM tablename WHERE id IN (
1,
2,
3,
4,
5,
6
);
If you have some 'condition' in your data to figure out the 254 ids, you could use:
delete from tablename
where id in
(select id from tablename where <your-condition>)
or simply:
delete from tablename where <your-condition>
Simply hard coding the 254 values of id column would be very tough in any case.
Others have suggested IN, this is fine. You can also use a range:
DELETE from tablename where id<254 and id>3;
If the ids to delete are contiguous.
Use IN Clause
DELETE from tablename where id IN (1,2);
OR you can merge the use of BETWEEN and NOT IN to decrease the numbers you have to mention.
DELETE from tablename
where (id BETWEEN 1 AND 255)
AND (id NOT IN (254));
DELETE FROM table_name WHERE id BETWEEN 1 AND 256;
Try This.
Hope it helps:
DELETE FROM tablename
WHERE tablename.id = ANY (SELECT id FROM tablename WHERE id = id);
DELETE FROM tablename WHERE id > 0;
OR
DELETE FROM tablename WHERE id <255;
It deletes id from 1 to 254
I have a table with 1000 rows.
I need to delete rows from 5 to 1000, so the first 4 remain intact and eveythin after 4 is deleted.
However if I write something like:
DELETE FROM table_name WHERE id > 4
the numbering of the next record after INSERT is 1001, which I do not want.
How to delete rows, but force the numbering of all next rows to the number of my last id?
P.S. I can not use ALTER TABLE to drop all the table.
Thanks in advance.
Can you do this?
ALTER TABLE theTableInQuestion AUTO_INCREMENT=5
If your id - auto_increment field you need to use ALTER TABLE to change it's value...
But if it's impossible you can try not to delete rows but to set there value to NULL or "" or 0 and then just to update there value
not INSERT but UPDATE ... WHERE id = 5
With MySQL? You don't. AUTO_INCREMENT never... decrements unless you call ALTER TABLE. Have you considered use of a trigger DELETE, however?
(You may need to debug this)
delimiter |
CREATE TRIGGER testref AFTER DELETE ON theTableInQuestion
FOR EACH ROW BEGIN
IF SELECT COUNT(*) FROM test1 < 5 THEN
ALTER TABLE theTableInQuestion AUTO_INCREMENT=5
END IF;
END;
|
DBCC CHECKIDENT ( 'table_name', RESEED, new_reseed_value ) ==> Works with MS-SQL
whereas
ALTER TABLE table_name AUTO_INCREMENT = 5 ==> Works with MySQL
Think the table is in auto_increment . If you cant ALTER TABLE you need to manualy give the id in each query, something like
INSERT INTO table (id,value) VALUES ((SELECT MAX(id)+1 FROM table), value);
(see comment vis-a-vis lock table)
I have two mysql tables, one needs to start its auto-increment column id with the last value of the last inserted row in the other table (plus 1).
According to mysql manual you can restart the value of an auto increment column like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;
However, this is not possible:
mysql> ALTER TABLE tb2 AUTO_INCREMENT = (SELECT MAX(id) FROM tbl1);
I need to perform something like this because I'm filling the tables using a script. Is there another way to achieve it?
I think you are trying to use the auto-increment column for something it is not well suited to. If you care about the exact values that get inserted then it shouldn't be auto-increment.
IT IS USING THIS SCRIPT
ALTER TABLE `people` AUTO_INCREMENT =1
where people is my table, or you can do this through GUI (operation tab of phpmyadmin with current table selected)
What about create a dummy record and increment?
-- insert dummy forcing id
INSERT INTO 'tb2' (ID, NAME, ...)
VALUES (SELECT MAX(id) FROM tb1, "dummy", ...);
-- automagically increment to last id + 1
ALTER TABLE `tb2' AUTO_INCREMENT = 1; -- only myISAM
-- delete dummy
DELETE FROM 'tb2' WHERE NAME="dummy";