I need a MySql table column which need to reset to starting value - mysql

I am in need of a Mysql Column which will contain values in a certain range. For example, I have a column name called "apiCallCount" value of this column should within a range of 1 to 1000. Also, we needed to make this column as auto incremented(please note the table has a primary key which is auto incremented)
That is, the value of the column("apiCallCount") will be "1" when you insert the first row, it should continue to increment till 1000 as each insertion happen. Next insertion after the value become 1000 should create a row with the value "1" in "apiCallCount" column.

Honourable Dipu, try to use RANGE_MAX column assign it to the number that you wish to be the maximum (which is 1000 according to you query) then make the field to auto increment while you are creating you database.

Related

Mysql : How can i set number of gap 5 on each increment number in auto increment column?

I need to set each increment id +5 from the previous one generated id table wise. It means each auto-increment have to 5 number of the gap. Like this series 2,7,12,17 etc..
Any idea please share.
Please check the link:
https://dev.mysql.com/doc/refman/8.0/en/replication-options-master.html#sysvar_auto_increment_offset
SET ##auto_increment_offset=5;
SHOW VARIABLES LIKE 'auto_inc%';
CREATE TABLE autoinc2 (col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
You should not be demanding this requirement from an auto increment column. The contract of an auto increment column only guarantees that each generated value would be unique. The values would tend to generally be increasing, but if the counter were reset, and certain earlier records were deleted, there could be a value generated smaller than one which already exists. Instead, consider maintaining some sort of timestamp column which keeps track of when each record were added to your table. Then, using ROW_NUMBER, it is easy to generate the sequence you expect, e.g.
SELECT
5*ROW_NUMBER() OVER (ORDER BY ts_column) seq
FROM yourTable;
ORDER BY
ts_column;
If you need to increment by 5, you can set auto_increment_increment=5.
This will apply to all tables, so you should set it as a session variable, not a global variable. The session variable will only apply to INSERT statements made in your current session, not to other sessions.
Then change the option back to 1 before you INSERT to another table.
There is no feature in MySQL to make the auto_increment_increment option apply permanently and for all sessions, but only for a specific table.

MySQL auto_increments always insert 2147483647

I have the following SQL code in PHP
$conn->query("INSERT INTO orders (Name,Email,phone) VALUES
('$name','$email','$phone')");
my database table has id,Name,Email,phone
insertion is successful but the id is always set to 2147483647 which is the max int(11)
so there is only one row in my table
ID-------------Name------- Email-------phone
2147483647--John--Smith--John#John.com--04524524
the id field is set as autoincrement
instead of being 1 or 2 it goe all the way to max (2147483647)
even if i make BIGINT or change the type mysql will insert the max ID increment
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][1]):
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.
If you are just bootstrapping your database, you maybe should consider emptying the table using:
TRUNCATE TABLE tablename;
The command above will also reset any automatic counters.
I have duplicate the table, data and structure,then delete the original one.
then rename table to replace, and voilá...
Autoinsert is OK

Insert after truncate start from 1; but insert after delete resumes from previous value

In my table there is column name id which is auto increment integer. When I truncate table and then try to perform insert query it starts from 1, but if I do delete operation and then try to insert then it just resumes from previous value. Why does insert query performs differently for delete and truncate. It should always start from 1 if there is not entry in table.
Example :
(1) Original Table
(2) delete all rows and then insert
(3) truncate the table then insert
The current max value for the autoincrement function is stored in the table definition (you can see it when you run show create table idle.
When you delete all rows from the table, the autoincrement value will stay the same. When you truncate the table you basically drop the table and recreate it, which resets the the autoincrement value to 0.
Hope this helps.
I think you are about to reset auto increment, run this query after delete query:
ALTER TABLE tablename AUTO_INCREMENT = 1 ;
NOTE: be careful about duplicate keys, if you delete some ids and reset ids to 1, while id=2 or n exists!
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.
some other actions and notes mentioned in this topic
you give AUTO INCREMENT in id that's why it is behave like this.whenever you truncate its reset the identity while in case of delete operation its start from last value of your id. if don't want this behavior remove AUTO INCREMENT index from id field.

Update a MySQL table that has only one row with no id column

I have a MySQL table called settings. It has multiple columns, where every column is an item with a single value. So it has only one row and no id column. The design is final (I don't plan to add more columns).
How can I update the value in a single column (change one setting's value)?
What's the problem with using this --> http://dev.mysql.com/doc/refman/5.0/en/update.html
?
UPDATE table1 SET column1 = value
In case you have more than one row, you can add:
WHERE table1.column = matching_value;
making sure the match criteria is only the row you need.
to update a value of certain item column you must specify the row contains the value to be updated, since you don't have a primary key, you can depend on the nature of item values to act as row identifier and i don't recommend that.. the best way is to update your design and add column for the primary key

Reset MySQL auto_increment when a large number already exists?

I have a table with an auto incrementing column. I need to set one of the rows to 1000 and never touch it again, but now that I've set it to 1000, auto increment keeps starting at 1001 and refuses to start at 1. Is there any way to get around this?
You cannot:
To change the value of the
AUTO_INCREMENT counter to be used for
new rows, do this:
ALTER TABLE t2 AUTO_INCREMENT = value;
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.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
Simple and short answer: you can't do this.
If you could, what would happen if you start your auto-increment at 1 and insert 1000 rows? The last couldn't be inserted due to "duplicate key"-error.
If you have to have a predefinded entry, with an id that never changes and is easy to remember, why don't you use 0 for that? Or, if you really need to use 1000, what's the problem with letting the other columns start at 1001?
Assuming you have no other row ID with 1000, you can insert the row to the bottom of the table, then you can simply use the update command:
UPDATE table.column SET id = 1000 WHERE id = current_id;
Assuming id is your auto-increment column. And current_id should be replaced with the id that the row is inserted at.
You can use MS Access that link to MySQL as external table, and you can change the auto increment table field value from MS Access via copy paste from Excel (first, you need to arrange the value of auto increment in Excel).
You could use the following statements:
UPDATE tbl SET id=1000 WHERE id=current_id;
ALTER TABLE tbl AUTO_INCREMENT=1001;