MySQL can I insert specific ids into an auto-increment column - mysql

I accidentally deleted some rows in my database, but I have a copy of the data in a backup.
Is it possible to insert the rows back in with the same id (auto-increment field). Or do I have to use the auto-increment generated id then update all the relationships manually?

You can, just by setting the field when INSERTing. Autoincrement only works if no value is specified for the field.

Related

Inserting auto increment value on duplicate key?

This seems like it should be simple, but I couldn't figure out a way to do it. Let's say I have a table with 5,000 rows, each with an ID (primary key) of 1ā€“5000. I am blindly inserting a new value with an existing ID, and it could be something like 2677. What I want to happen is that if the ID already exists, it will use the auto_increment value, in this case 5001. That or the maximum existing value + 1.
Most importantly, I can't use PHP (or anything else other than SQL) to do this, because the output is a query that needs to be directly importable without errors.
I have looked at two similar questions on SO:
Can you use aggregate values within ON DUPLICATE KEY
ā€“ the problem here is that they're selecting from an existing table which I can't do.
on duplicate key update with a condition? ā€“ the problem here is that I have no information on the table I'm importing to (except the basic structure), and don't know what the maximum value is.
INSERT INTO table (column1,column2) VALUES (1,2) ON DUPLICATE KEY UPDATE id=VALUES(id)
Obviously this requires an id column with AUTO_INCREMENT.
Moreover if you later need to select the inserted id just like if it was a new Insert, you do:
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(VALUES(id));

How to achieve unique auto-incremented id for rows across multiple tables?

I've got several tables in a database, let's say they're called table1, table 2, etc.
All tables have a primary key column 'id' with auto-increment.
In my current configuration it happens that when inserting into table1, the generated id is 1.
Afterwards when inserting into table2, the generated id happens to be 1 as well.
How to force absolutely unique ids across all tables in a database? I want when inserting into table1, generated id to be 1, and if afterwards inserting into table2, generated id be 2?
I used mysql server on some machine and did not have this problem, but when I installed mysql on my local machine, it started to occur. So I guess it must be some kind of a setting that is applied to the mysql configuration?
Thank you
you can use UUID.
INSERT INTO mytable(id, name) VALUES(SELECT UUID(), 'some data');
Read more about UUID: http://mysqlbackupnet.codeplex.com/wikipage?title=Using%20MySQL%20With%20GUID%20or%20UUID
You can create SEQUENCE which can be used globally.
CREATE SEQUENCE serial
INCREMENT 1
MINVALUE 0
MAXVALUE 200
START 364
CACHE 1;
Edit: Sequences are supported in Postgres but this can be achieved in MySql by setting value for AUTO_INCREMENT and one can use LAST_INSERT_ID(). link

Adding serial number to each record inserted in a MySQL table

My need is very simple. How to add a serial number or id(probably an auto_increment value I guess) for each row being inserted in a MySQL table?
To be more specific, I've a CSV file from which I store the separate field values into the database using LOAD DATA. What all I need is, if there are totally 2000 rows being loaded from the CSV file, each row has to be automatically inserted with a unique serial number like 1,2,3,etc...
Please help me with the exact query that I'll be needing rather than a syntax.
Thanks in anticipation.
Add AUTO_INCREMENT to your id column (create it if you need to). It will then do this automatically for you:
ALTER TABLE tablename MODIFY id INTEGER NOT NULL AUTO_INCREMENT;

mysql AUTO_INCREMENT skips valid range of numbers after INSERT....SELECT on table

My INSERT... SELECT query inserts 2,546 records into a table which brings my ID range for all inserted records from 176,439 to 178,984. All records are successfully inserted (178984 - 176439 + 1 = 2,546). Now if launch my application and add a single new record, my next ID appears as 180534. I don't understand mysql didn't set it to 178985. Similar behavior happens if I repeat the process.
My innodb_autoinc_lock_mode is set to 1, the default, which stands for "consecutive".
Any thoughts?
The number of records does not have to match the auto-increment id. Records may have been deleted. The auto-increment was set manually or for other reasons. You can set the next id with this command
ALTER TABLE tbl AUTO_INCREMENT = 178985;
After that the IDs should be generated above that number.
Yes, you ran some other insert query, rows were rejected probably due to unique constraints but auto-increment still incremented, or they were deleted after that. Alternatively if someone else (or you in another session) started a transaction, inserted some rows but didn't commit yet... the auto_increment value would also be larger.

Does dropping a SQL table reset its ID value?

Will the ID auto-increment value be reset if I drop (wipe) a MySQL table? And, if I delete (for example) the entry NĀ° 535, will this entry number be filled again later?
I don't want that ID to be filled with other new entries if I wiped old data. If this is not the behavior, then what's the solution to avoid this?
Which DBMS are you using? MySQL does reset the auto-increment value when you TRUNCATE a table. You can use the (much slower) DELETE FROM tablename to avoid this.
The auto_increment value doesn't change if you DELETE a line, but it is reseted if you do a TRUNCATE TABLE. And the next ID is always the current auto_increment value ("gaps" aren't filled again).
You can change the auto_increment value with ALTER TABLE table AUTO_INCREMENT = num
Yes. The solution would be to not DROP your table. Instead use DELETE FROM ...
If you drop a table, it will be gone along with any identity seed values.