the number of rows in my mysql TB (As shown in TB info) is 11093 where as the auto increment id (starting from 1) is 11361. Why is that so?
Deleted rows do not reset the AI index. Row count is the number of entries currently in your table, while the AI value is the number of rows you ever added to the table.
Related
i'm deleting previous data and trying to insert new list of data,id values are keep on increment because of auto-increment. is it possible to have new auto increment id with 1 ?
and i tried with ALTER TABLE table AUTO_INCREMENT = 1; its not working for me.
Use this query while deleting your old 20 Records.
truncate table YourTableName;
It will reset the database structure and if you insert new record it will start from 1(one) id again.
You cannot reset the counter to a value less than or equal to the
value that is currently in use. For both InnoDB and MyISAM, if the
value is less than or equal to the maximum value currently in the
AUTO_INCREMENT column, the value is reset to the current maximum
AUTO_INCREMENT column value plus one.
And also read this article Link
Just visit this question
Let's say I have have a MySQL table with many rows that I want to limit in a specific way. Each row has a column, userID, that is indexed but is not unique. Each row also has a datetime column for createDate.
I'd like for the table to never contain more than X number (say, 1000) of rows for a given userID. So, anytime a row is inserted, if that means that there are now greater than X rows where userID = Y then the DB would delete a row based on the value of createDate.
Is this possible to achieve inside the database only? What performance concerns should I have about this kind of approach?
So, I have a table which has 3 columns.
Customer_number, login_hash and some_hash
The customer_number is not the auto increment id and the table is being indexed on login_hash.
Now the table is updated every hour and new entries are being added. I have to get the new entries, use them for some calls and then store the resulting data.
My plan is to always store the last row number in some last_row environment variable and then retrieve values after that row number till the last record. Then update the last_row number.
How do I achieve this? And is there any better approach to this problem?
I know this is a bad table design but I have to deal with this and can't change it.
Let's say I have a table defined by:
CREATE TABLE People (
id SERIAL,
name TEXT
);
If I first find the maximum id in the table and then run the following query:
SELECT (id, name)
FROM People
WHERE id <= [maximum id found before];
I'll get a list of people. If I run the same query again with the same maximum id:
Am I guaranteed to get the same results?
Or is it possible that the first query returned a list with gaps which were filled in before the second query was executed, causing the second query to have more rows?
Assume that no changes are made to the database except sequential insert operations from any number of concurrent connections.
EDIT:
I'll try to clarify the specific case I'm concerned about. Let's say MySQL gets five transactions at around the same time. Transactions A and B both insert a person into People. Transaction C finds the maximum id. Transactions D and E both perform the query written above.
Is it possible for this sequence of events:
A is assigned an id
B is assigned the next id
B is committed
C is committed and returns the id of B
D is committed and returns a list that does not include the row inserted by A
A is committed
E is committed and returns a list that does include the row inserted by A
EDIT:
I'm thinking this scenario is impossible due to atomicity, but I'm hoping for confirmation from someone who understands ACID a little better than I do.
I think you are guaranteed to get the same list, gaps should never be filed in unless you're manually inserting them somehow. (although, if there are gaps i assume rows can be deleted, so it may not be exactly the same list because more may have been deleted.)
from How to reset AUTO_INCREMENT in MySQL?
You can reset the counter with:
ALTER TABLE tablename AUTO_INCREMENT = 1 For InnoDB you cannot set the
auto_increment value lower or equal to the highest current index.
(quote from ViralPatel):
Note that you cannot reset the counter to a value less than or equal
to any that have already been used. For MyISAM, if the value is less
than or equal to the maximum value currently in the AUTO_INCREMENT
column, the value is reset to the current maximum plus one. For
InnoDB, if the value is less than the current maximum value in the
column, no error occurs and the current sequence value is not changed.
See How to Reset an MySQL AutoIncrement using a MAX value from another
table? on how to dynamically get an acceptable value.
I have a table which contains an auto incremented primary key id. If I delete the last row (highest id, for example id = 22) and insert a new row, the new id starts at 23. What should I do to start it with 22 again?
Example Table:
PrimaryKeyID Column 2 Column 3
Row 1 (auto-incr.) x x
2 x x
deleted row 3 x x
inserted row 4 (should be 3) x x
This is from MYSQL developer comment. See More https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
In order to reset the auto_increment, in a situation where some of the
most recently added rows were deleted, use:
ALTER TABLE your_table_name AUTO_INCREMENT=1234 //this is a demo number
Then future insertions will be numbered from 1234 again (unless you still had rows numbered greater than 1234, and then the future insertions will start from the greatest number + 1 ).
Execute a query to check that your auto_increment (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE table_schema='database' AND table_name='table') is larger than max(audited_id) and perform a fake insert. You can put that logic with --init-file or on a trigger, please refer this article.
http://dev.mysql.com/doc/refman/5.6/en/innodb-auto-increment-handling.html#c12158