I have a problem with MYSQL Workbench. Let's say I have this table, the user 'x' has index 8. Let's say I delete this user, so now I have only 7 users. If I create a new user with Java Console, the new user will be created with ID 9 because it's in auto increment. Is there a way to tell Workbench to start from where it is the last number? in that specific case, start creating the new user from 8 and not 9?
I tried to check everywhere on the web, but found only some query to recreate the table, but its not what I want because I have some foreign keys linked to that table, so I can't just delete the table and recreate another one.
I'm not familiar with MYSQL Workbench, but I think you might be able to alter your table and to change the value of the AUTO_INCREMENT.
Here's a link that talk about it in more details and even has an example:https://www.w3schools.com/mysql/mysql_autoincrement.asp
I hope this help you.
There is not a way to do that. Using Auto Increment or Identity makes a value no longer usable once it is used.
So a user with an ID of 1 will always have an ID of 1. A possible workaround is to not delete users, but instead put in a column of TinyInt(1) labeled 'Deleted'
New Users will have Deleted set to 0. When a user is deleted, their record gets set to 1. It will keep your IDs in order and then you just need to account for querying users who are not deleted.
Related
I have an application that uses a h2 Database to store records of data. Each record is assigned a unique ID that I have used the auto-increment feature in h2 to do so. I want the lowest number to always be 1, or at least fill up the numbers that are not filled when a record has been deleted. What I mean is if there are 5 records numbered 1-5 and I delete the third record, I want the next record added to be numbered 3 instead of 6. How should I go about achieving this?
So far, I've tried
ALTER TABLE <table_name> ALTER COLUMN <id_column> RESTART WITH 1
Which doesn't have the intended effect that I wanted.
Edit: I'm an idiot, I wrote the SQL Query without actually executing it. The does indeed restart from 1, but throws an exception whenever the increment value is in a value that already exists. How should I fix this?
I am using SQLite in JAVA. I created a table with column id AUTOINCREMENT but I want to reset id after deletion so that no gaps exist between ids. How can I reset it ?
Although I highly recommend agains, like laalto
What problem would that solve? Generally, identifiers should stay stable. Having gaps between identifiers is not a problem.
It's possible by itinerating through all records with an auxiliary variable starting at 1 and incrementing. After reseting all id's you need to change the AUTOINCREMENT seed by running this SQL command DBCC CHECKIDENT('TABLE_NAME', RESEED, LATEST_RECORD)
EDIT
My bad here's the code for SQLite
UPDATE SQLITE_SEQUENCE SET SEQ=NEWSQUENCE WHERE NAME='table_name';
I am developing an android application as my final school project and I came to a problem.
I've created a table in SQLite which stores the information from users like ID, Name, Phone, Email Address etc. but I'd like to insert the data in an ordered way to always insert users using sequential ordering. In my Add_new_user_activity I have an EditText field which I want to be dynamically auto set with the next available ID from the existing ID's in the table, but I don't know how to handle the gaps that could be generated if an user is deleted between two sequential IDS.
Let say that I have this sequential records on the table:
Users from 1 to 50 with it's corresponding ID's.
Then I delete the 27 and 29 user.
The next time I want to add a new user I want the EditText to know that there is a gap between the ID 26 and 28 and take the 27 for the new user ID and do the same if I add new users. In this case if I add 2 more new users their respective ID's would have to be 29 and 51.
Is there a way to solve it efficiently?
Thank you in advance!
What you're trying to do sounds dangerous, as that's not the intended use of AUTO_INCREMENT.
If you really want to find the lowest unused key value, don't use AUTO_INCREMENT at all, and manage your keys manually. However, this is NOT a recommended practice.
Take a step back and ask "why you need to recycle key values?" Do unsigned INT (or BIGINT) not provide a large enough key space?
You need to add one more field in your DB say "deleted" and whenever you delete a user just set it to 1. Now whenever you have to add a new user first you need to get list of user-ids having 1 in "deleted" field in ascending order. The first value of the list will hold the desired ID. Then you may just update the value having that ID. If you got no such field, go for insert with incremented ID.
I have a MySQL database storing values, and using JOOQ to interact with that data. Everything has worked fine for me, until I tried to change the value of a unique key. I have a table that stores groups that people can create, the owner of the group is stored as ownerid. I have ownerid set to be unique since each person can only own 1 group. The problem comes in when I try to change the ownerid (user passes ownership to another user). I set the new ownerid using record.setOwnerId(), and then I call record.store(). Once record.store() has completed, I am calling record.getOwnerId() and it is returning the correct (new) id. I should also note that there is no SQL errors that occur when this happens. The issue though, is that the SQL database never gets updated with the new value. When I do record.refresh(), the ownerid is set back to the previous value. Any idea why this would happen or how I would debug it? I should point out that I have verified that the new ownerid is not already in the table, and there doesn't appear to be any SQL errors being thrown.
Edit: It works completely fine if I modify the value manually, but for some reason record.store() refuses to change it
I am using a field with a prefix + auto increment id. For each instance i am taking the max+1 of ID and adding that to prefix. Can anyone suggest me a way to get this as unique please?
You can try this:
Insert into table1 (id, user_id)
SELECT MAX(id)+1, CONCAT('a',CAST(MAX(id)+1 AS char))
FROM table1;
See this SQLFiddle
The problem with using max(id)+1 is that there may be multiple threads making the same call, and so the result would not be unique. There are several ways to solve this problem. The first is to use a sequence, where the database server will increment the number every time a new id is requested. You can use a table, with a number in it, but you have to lock the table when you update the number. Or you can allow the database to create the key for you when the table is inserted and retrieve the key after the insert. All are valid.
I prefer to use Hibernate and make it determine how to implement the ID for the database I am currently using.