Is there a way to set the start index of autonumber field in ms access?
It's start on 1 and i want it start at 1000
You can manually add an ID column with a specific seed. This also can be used in CREATE TABLE statements. Note that, for existing records, the ID doesn't start at 1000 but at 1.
ALTER TABLE A Add Column ID AutoIncrement(1000,1) PRIMARY KEY
Use INSERT SQL query to add one record with ID=999 and delete it. Next autonumber ID will be 1000
Related
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.
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;
I have a table: test
id int(10) auto-increment
name char(36)
Now let us say my WHOLE table it filled from ID 1000 => max unique id number.
Id 1 - 1000 = deleted previously.
Question 1; WILL mysql re-use these deleted id's?
Question 2; If not, how to I go about having auto-increment or whatever to re-use unique identifier that does not exist in table?
The reason I am asking, is that my table will consist of alot of entries, and that alot of entries will be deleted all the time. What happens when I "run-out-of-id" when using auto-increment?
Thanks for any enlightment on this :)
-Tom
WILL mysql re-use these deleted id's?
When mysqld starts, it determines the next value for every AUTO_INCREMENT column by finding the maximum of the incumbent records (and adding 1). Therefore, if you delete the record with the highest value and restart the server, the deleted id will indeed be reused.
Otherwise, values will only be reused if you manually alter the next AUTO_INCREMENT value (this is not recommended as it is not concurrency-safe):
ALTER TABLE foo AUTO_INCREMENT = 12345;
If not, how to I go about having auto-increment or whatever to re-use unique identifier that does not exist in table?
Generally speaking, you don't: consider redesigning your data structure so that inserts/deletes do not happen in this fashion, or else use a larger integer type (BIGINT UNSIGNED is 8 bytes, so can go up to 2^64 or ~10^19).
What happens when I "run-out-of-id" when using auto-increment?
As stated in the manual:
Use the smallest integer data type for the AUTO_INCREMENT column that is large enough to hold the maximum sequence value you will need. When the column reaches the upper limit of the data type, the next attempt to generate a sequence number fails.
No, MySQL won't reuse the IDs from deleted records
Do you really need to? If the type of your autoincrement column is BIGINT, you've got 18446744073709551615 possible IDs
you need to reset the autoincrement, Autoincrement just keep incrementing and won't go back at least if you don't set it.
alter table tablename auto_increment=value
like this
mysql> alter table t1 auto_increment=200;
Query OK, 202 rows affected (0.04 sec)
Records: 202 Duplicates: 0 Warnings: 0
EDIT:
if you delete some records, the auto_increment will be "last value+1", it doesn't matter what you do, only if you delete the full table you'll be able to 'start over'.
The best would be with a trigger, but triggers can't alter tables (http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html)
So your last option is a store procedure take a look here: Is it possible to alter a table in mySql via stored procedures?
Which is not recommended.
I'm aware that you can create a unique column in your MySQL table, but I'm actually looking to compare TWO columns.
So if I had records like:
Time User Table
10:00pm Fred 29
11:00am Bob 33
I COULD insert a new record with the time 10:00pm and table 33 but not 10:00pm table 29.
I know I could run a query and then nullify my ability to insert a new record if I had results from that query based on comparing those two fields, but I'm wondering if there is a more elegant solution wherein I can get a duplicate entry error from MySQL on the INSERT and save myself a few lines of code.
You can create a unique index that incorporates both columns:
CREATE UNIQUE INDEX idx_time_and_table ON reservations (`Time`, `Table`);
This will block any inserts for the same pairing provided both values are not NULL. Having a NULL value side-steps the unique checking.
You're also using reserved SQL keywords for your column names which you might want to change.
Try using a composite unique constraint across both columns:
ALTER TABLE your_table ADD UNIQUE(`Time`, `Table`);
Now any rows attempting to be added that have matching values will force MySQL to throw an error, which you can test for in your app.
Create an unique index based on the columns you want to be unique:
CREATE UNIQUE INDEX index_name ON table_name ( column1, column2,...);
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.