I have a table with an ID AUTO INCREMENT PRIMARY KEY. When I delete an entry and re-add one, the new entry does not take the ID of the previous one instead it increments again by one.
How to change this behaviour to get the id of deleted record to newely add record?
This is intended behaviour and can't be changed.
Don't misuse the primary key as indicator of your record order. You can use another colum for that like a datetime with a default value like current_timestamp
First of all, you shouldn't have to care about this; if you want to be really sure that you don't run out of numbers, use BIGINT UNSIGNED for your primary key instead.
Be warned that doing the below is not recommended.
ALTER TABLE mytable SET AUTO_INCREMENT = 123;
This set the number to be used for the next record at 123, so in your case you would set it to the deleted record's identifier.
Related
In MySQL, I understand below
AUTO_INCREMENT: used to make INT value to increase automatically every time a user creates a row.
PRIMARY KEY: used to make value unique in that table.
However, I oftentimes see them used together when defining id.
I don't understand the point of using AUTO_INCREMENT with PRIMARY KEY since AUTO_INCREMENT itself would make id unique.
Is there any reason they are used together when creating id?
The primary key has three properties:
It is unique.
It is non-null.
There is only one per table.
Defining the key as a primary key means that it should also be used for foreign key references.
In addition, MySQL clusters the data by the primary key. So the declaration instructs new rows to go at the "end" of the table -- meaning adjacent to the most recent inserts on the data pages.
In addition, duplicate values for the auto-incremented id could be created in various ways. One way is that the increment counter can be reset, causing duplicates. MySQL should be pretty thread-safe on duplicates for concurrent updates, but bugs have been reported. As a primary key, no duplicates will be allowed into the table.
You understand it correctly, but they are doing different things.
PRIMARY KEY with AUTO_INCREMENT means we want this column isn't duplicate on the value and it will be auto increase if we didn't set the value.
but how about we only set AUTO_INCREMENT it only means it will be auto increase if we didn't set the value. but didn't make sure the value is unique.
AUTO_INCREMENT doesn't make the column uniqe. It only "automatically" fills a value when creating a row, if missing. But you can later update the values, or also create a row by explicitly providing the value.
PRIMARY KEY denies any modification sql statement that would cause 2 different entries storing equal values. So it guarantees you, that your DB is in a correct state.
I need to store a build number which will start with zero and autoincrement if nothing is provided.
One solution would be to make it only a primary key and not autoincrement. However,I want it to be primary key auto increment and start with zero. Is this possible?
Per the docs, you could try this:
ALTER TABLE tbl AUTO_INCREMENT = 0;
If that doesn't work then I can't imagine what would. Personally, though, I would maintain build numbers externally instead of using auto_increment, if only because auto_increment will re-use values if you remove the row(s) with the largest keys.
Right now I have my ID field as the primary key in MySQL and have AUTO_INCREMENT on. What I want to know is how to make the ID represent the number of that row in the table rather than giving it a number when it's inserted, then sticking with that number? Because when I delete something, then that number isn't used. I want them all to be unique based on row count.
Always have a primary key. Either a basic auto increment int or a composite key of multiple fields. It helps your DB do it's job and comes in handy when you want to have relationships. Add a field called RowIndex and renumber it when you delete anything.
When you create a table don't add the AUTO_INCREMENT key word
For existing table, use
ALTER TABLE <Table_Name> MODIFY COLUMN <Column_Name> INTEGER;
to remove the AUTO_INCREMENT and the Primary key will be kept.
I'm using MySQL in PHPMyAdmin. I would like to create table where one can find distinct id's for every user. Can this be done by auto_increment, like
`user_id` SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY
or what does the auto_increment mean?
Yes, that would work. AUTO_INCREMENT means that every time you add a new row, it automatically makes the id the previous + 1. You also don't specify a user_id when you add a new user.
auto increment means that mysql automatically creates the value in the field and increments it by 1 every new record.
You don't have to manage that at all.
Hmmm...maybe I misunderstood the question. If you make the drop down for the "extra" field in phpMyAdmin "auto_increment" it will make it auto increment the field value as described above.
That should cover it :o)
AUTO INCREMENT means that you don't need to fill this field, it will be filled automatically.
If you want find distinct id you should make it PK (primary key) or unique. Both of them make this values distinct
I am using SQLite3. I load a table with say 30 rows using integer as Primary ID and it auto-increments.
Now I delete all the rows from the table and then, reload some new information onto the table.
Problem is: the row count (my PrimaryID) now starts with 31. Is there any way that I can start loading new rows from the number 1 onwards?
SQLite
Use:
DELETE FROM your_table;
DELETE FROM sqlite_sequence WHERE name = 'your_table';
Documentation
SQLite keeps track of the largest ROWID that a table has ever held using the special SQLITE_SEQUENCE table. The SQLITE_SEQUENCE table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes.
Found the answer on SO: SQLite Reset Primary Key Field
MySQL
Use:
ALTER TABLE tbl AUTO_INCREMENT = 1;
In either case, the database doesn't care if the id numbers are sequencial - only that the values are unique. If users never see the primary key value (they shouldn't, because the data can change & won't always be at that primary key value), I wouldn't bother with these options.
For MySQL:
Use TRUNCATE TABLE tablename to empty the table (delete all records) and reset auto increment count.
You can also use ALTER TABLE tablename AUTO_INCREMENT = 0; if you just want to reset the count.
For SQLite:
DELETE FROM tablename;
DELETE FROM SQLITE_SEQUENCE WHERE name='tablename';
References
SQLite AutoIncrement
MySQL AutoIncrement
For SQLite use (not need to delete and create the table)
UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='table_name';
For MySql use
ALTER TABLE table_name AUTO_INCREMENT = 1;
You should not use AUTOINCREMENT in this case. Simply define your primary key as INTEGER PRIMARY KEY and the count will be reset to 1 after a DELETE FROM query. Without AUTOINCREMENT, the default behaviour will still be an automatic increment of the primary key as long as you don't run out of space in your table (in that case, old - deleted - values will be reused).
More information available in the SQLite Autoincrement document.
ALTER TABLE tbl AUTO_INCREMENT = 0;