How to adjust Auto Increment? - mysql

Good day to all. So I have lots of data in excel that i needed to insert in the db but I didn't notice I inserted a duplicate value (cause hr didn't sort it out).
So anyways, here is the deal
[id][name]
[1][name1]
[2][name2] // I want to delete this row
[3][name3]
[4][name4]
I want to delete row 2. I want to make the id auto adjust like,
[id][name]
[1][name1]
[2][name3] // The id will adjust
[3][name4]
Is this possible to achieve? Or if not is there a work around I can do?
PS: My data is already at 50k and I want to delete the rows in the 7k

In general, you should avoid manually intervening with an auto increment column. This means, among other things, that when you insert data into your table, you should omit the auto increment column, thereby allowing MySQL to automatically assign a value to it.
Perhaps the best solution here would be to have a third timestamp column which records when each record were inserted. Then, you may use ROW_NUMBER to generate the sequence you want, e.g.
SELECT
ROW_NUMBER() OVER (ORDER BY ts_column) id,
name
FROM yourTable
ORDER BY
id;
You might be able to also order ROW_NUMBER using the id column, and get the same result. If you are using a version of MySQL earlier than 8+, then you may simulate ROW_NUMBER using user variables.

Related

MySQL - checking max rows during insert

We have a table where we limit the number of rows per user to a certain number. Before insert, we check if user has exceeded the storage capacity (number of rows) and then insert as appropriate. For example,
select count(id) from items where user=123;
say, if count < 10,
insert into items set user=123, a=xyx;
However, this approach requires two queries. Is there a way in which is can be done in a single query.
Thanks
Pretty much any approach you take will require two queries. Whether you do this in a trigger or in application code is a matter of choice.
If you use your method, then add an index on items(user). This will make it cheap to count the number of rows.
An alternative is to increment a value in the user table for each item. So, when a user inserts an item, then increment users.itemcount. Remember, though, to reduce the count when you delete items.
If you just want to have 1 query in a code you can try to use conditional insert with approach described here MySQL Conditional Insert

Will all numbers be 100% unique in a table if to use this simple algorithm?

My goal is to insert a new and unique(unique is very important) number into a MySQL table on the server every time upon an event on a user's machine, using ajax.
So, server's part on user's event is doing this (using php):
Finds a maximum value from the column in the db,
Adds 10 to a maximum value,
this is a new and unique (bigger than a maximum) value, we insert insert into a table.
Will all numbers be unique and go like 1, 11, 21, 31, if it starts from 1? I'm curious if inserting into the Table finishes before it starts performing another queue and coule be like 1, 11, 21, 21, 31, 41?
If it theoretically works like this (ordered by time)
find max value from the column for the first user
find max a value from the column for the second user (it will be the same)
insert a (max+10) for the first user into the same table
insert a (max+10) for the second user into the same table (it will be the same), then the results will be the same, and 1 value could be repeated twice or even more...
So, the question is: will all numbers be 100% unique?
Depending on this I have to choose which algorithm to use for creating unique numbers.
Added:
Is it possible to be sure with this algorythm and without using autoincrements? Autoincrement is used for another column. Holes between numbers are OK. The only requirement is that numbers should be different, but with some "delta" that is more than one. Sorry I didn't notice about that in my question. Thank you.
Unless you have a very specific reason against it, I recommend using AUTO_INCREMENT - it will scale much better and actually leave fewer "holes" in the sequence of numbers than your approach.
And you are correct - your approach will not actually guarantee uniqueness in concurrent environment. One way to make your algorithm work is to have a UNIQUE constraint on your field (if it is not already PRIMARY KEY) and then repeatedly attempt to insert a new value - if it fails just generate a new value and try again and it will eventually succeed.
Use an auto incrementing column in the db
It sounds like you want to use autoincrement.
If you're using auto-increment on a separate column, you can still emulate it with something like the following.
insert into mytable (
column1,
column2,
fake_auto_incr
) select
'value for column1',
'value for column2',
max (fake_auto_incr) + 1
from mytable
Because the insert is a transactional statement, ACID databases will ensure the max+1 trick is always one greater then the current top value.
Keep in mind you'll need a slight adjustment for that to work on an empty table, since the query will return NULL in that case. This should suffice:
insert into mytable (
column1,
column2,
fake_auto_incr
) select
'value for column1',
'value for column2',
coalesce (max (fake_auto_incr), 0) + 1
from mytable
This forces the initial value to 1 on an empty table, otherwise it uses the next available value.

mysql prevent insert row auto sorting

I use Codeigniter to perform insert to mysql (not sure if relevant), I have table column and some data like this after I insert:
[invoice_id][product_id][unit_cost][quantity]
[42][1][50][2]
[42][2][100][3]
[42][5][45][1]
The problem is mysql auto sort it by invoice_id first then product_id like the above.
Before I insert them, my invoice item-list position was :
[42][5][45][1]
[42][1][50][2]
[42][2][100][3]
I do not want any of this auto-sort because when I retrieve them, they went like the list in database not as in the invoice. I dont think I can use sort for a particular column because they are all random in the first place.
I can only start thinking to add another column [position] contain number just for the sake of sorting it later, or is there a better way without it?
Thanks in advance for any reply.
You are not guaranteed the order of MySql resultsets unless you specify an ORDER BY clause. The only way to do it if you cannot sort with the parameters you already have would be to add another column to the table. Typically this would be an autoincrement integer field. You would then be able to order by id, and return the rows in the order they were entered.

storing records in mysql db in sorting order of date

I want to store some records in mysql database. records have a date column. i want to store them in sorting order of that date column.
For example, record having date 27/sep/2011 get stored as first row on the top of record having date 26/sep/2011 as:
id_1,name_1,27/sep/2011
id_2,name_2,26/sep/2011
if new records come on future dates they would get inserted on the top.
I DONT want to order them while using select by using order by desc .
i want they get inserted into db directly in sorted order.
how to do this???
thanks...
I am always surprised when people want to determine physical order of storing records.
Basically, it's a terrible idea for multiple reasons.
1) How the record is physically stored should not be of your concern.
2) How the record is presented should be of your concern. That's why we have ORDER BY built in.
3) Determining physical storage should be done by experts in the field, since it has performance implications - which is a topic in its own and I won't go into details.
Basically, worry about getting the data out in the sorted order, not getting it in in the sorted order.
Reason why it's a bad idea is because you'll be tampering with the primary key which is never, ever a good idea. On top of that, you'll have to reorder the records every time you insert something. Just don't reinvent hot water.
You could do this by adding another table - inserting all of the records into that table (the current and the new ones) then doing and insert as follows :
INSERT into newtable
select * from temptable
order by temptable.date
Why do you need to do this ? why not just use orderby on the query ?
As pointed out in the comments below - you would need to truncate the newtable each time
You cannot choose where to insert your row.
Here's one possible solution: MySQL syntax for inserting a new row in middle rows?

Make my own incrementing mysql id without auto increment

How can I duplicate the function of an auto incrementing Id field without making the field itself auto incrementing? I suppose on my INSERT statement, I would need to somehow grab the last id created, and add +1 on the new entry. But I don't know how. Any help is appreciated.
EDIT: I ended up taking ypercube's advice and keeping the id field as autoincrementing and making my searchstring unique. Thanks!
You'd need to select the highest current ID in one query, then use that when inserting using a second query.
To get round the race conditions, you could create a table lock (nasty) or use transactions (better, but not ideal)