I created database table with query:
"CREATE TABLE downloads(id BIGINT PRIMARY KEY AUTO_INCREMENT,...
When i insert anything into table first time ID thats generated for entry is 1
Second time its 100% of the time random value 1,33,65,97,129,161,193,225,226,227 etc
Is there a way to set my database to auto increment normally? 1 ,2 ,3 ,4 etc, To me the ID number doesnt matter bud i have to be able to predict next ID that will be present for an entry beforehand.If there is a way to predict or get id thats gonna be generated for my entry i might as well use that,IF IT DOESNT CREATE ANY GREAT OVERHEAD.
So my quesiton is why im gettin random values for id?
Is there a way to set autoincrement to ordinary +1?
Can i get id value before its generated if there is no way to set autoincrement to +1 style?
Only option that i use for my connection is ;DATABASE_TO_UPPER=false
Query to insert data
getConnection().prepareStatement("INSERT INTO downloads(state,name,size,source,added,completedOn,downloadDir) VALUES(?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
EDIT:After creating new database, numbers are exactly the same every time so these values aint random.First 3 values i entered had id's 1,33,65
Found out that problem was in PRIMARY KEY part of the query...
After removing it from there and making yet another db, everything is working now.
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?
Just a quick question about locking tables in a postgres database using JDBC. I have a table for which I want to add a new record to, however, To do this for the primary key, I use an increasing integer value.
I want to be able to retrieve the max value of this column in Java and store it as a variable to be used as a new primary key when adding a new row.
This gives me a small problem, as this is going to be modelled as a multi-user system, what happens when 2 locations request the same max value? This will of course create a problem when trying to add the same primary key.
I realise that I should be using an EXCLUSIVE lock on the table to prevent reading or writing while getting the key and adding a new row. However, I can't seem to find any way to deal with table locking in JDBC, just standard transactions.
psuedo code as such:
primaryKey = "SELECT MAX(id) FROM table1;";
primary key++;
//id retrieved again from 2nd source
"INSERT INTO table1 (primaryKey, value 1, value 2);"
You're absolutely right, if two locations request at around the same time, you'll run into a race condition.
The way to handle this is to create a sequence in postgres and select the nextval as the primary key.
I don't know exactly what direction you're heading and how your handle your data, but you could also set the column as a serial and not even include the column in your insert query. The column will automatically auto increment.
I have several databases ,and need exchange data between them. When I export from db A import into db B, Id confliction will happen. I think out two approach, no one satisfy me.
select max(id) then create new id to avoid confliction ,but one column store json structure and contains id too! (history reason). So I need create new Id (primary key) and modify all ids in that json column.
or I can add a batch info for each data import. When I import data, I find out every id in sql and add batch id before them. Such as:
The original db like:
ID COL_JSON
11 {id:11,name:xx ...}
I want to insert a new record :11 ,after insert I add a batch info "1000" before id
now db looks like
ID COL_JSON
11 {id:11,name:xx ...}
100011 {id:100011,name:xx ...}
the next batch will be 1001,1002 1003 ..., so if a new 11 record need to be insert the db looks like
ID COL_JSON
11 {id:11,name:xx ...}
100011 {id:100011,name:xx ...}
100111 {id:100111,name:xx ...}
Although the two approach can resolve conflict, I feel the two approach is stupid. Is there some graceful scheme?
For legacy system, there is no better approach except to align with it. We cannot change it more on legacy system, so your 2th approach seems good. Frankly, tt's not stupid and just the right way to go.
I don't understand exactly what your databases exchange.
If you need data from both databases in both of them you could use something similar to your batch but using characters - A11 and B11 ids.
This way you won't have conflicts even if your database grows a lot.
Edit: You could make also a primary key with two fields: the integer ID with autoincrement and a varchar for the original database name.
When I have a table that should be synchronized (not on the fly), I use this approach :
The main table that will be overwritten has a big autoincrement (ie : autoincrement = 100000)
The secondary table that will be merged to the main one has a normal autoincrement starting to 1.
The only requirement is that you ensure that the main table has a big enough autocrement set up, so the secondary table id will never reach the main table first ID
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.
I have a database in which I need some IDs to be autoincremented, but I also need that ID that gets auto incremented to be the Foreign Key for another table. Is there a way to recover that number within the same transaction to insert values into the second table?
As in:
1) Create a user
2) Retrieve the ID number generated by the auto increment ( for example: AI:5 )
3) Insert values into a table called Doctor, that needs that number retrieved by that user, all within same transaction...
I know that JSP has some function to recover that ID generated but not sure about MySQL.
Another thing is, I can't just send a query to recover the last generated ID because for example if 10 accounts got created at the same time I might not get the supposed number.
For a pure MySQL solution:
SELECT LAST_INSERT_ID();
For a Java way:
ResultSet rs = stmt.getGeneratedKeys();