There are some script to verify the limit about one sequence.
for example
one table, with huge information, with INT ID ( auto increment )
how I verify If my sequence are so close from the limit of this column ?
Related
I have table of Users in MariaDB and I need to generate serviceNumber for each newly created user.
Example of this code: 865165
Only two requirements are:
Unique in User table
Unpredictable when creating user (Not based on AutoIncrement maybe?)
Is this possible with just database? Or I need to implement it in backend when creating user.
The only (theoretically) collision free solution would be to generate the serviceNumber with UUID() function (or maybe UUID_SHORT()).
The disadvantage of this solution is, that it can't be used in statement based replication, in this case you should create the value in your backend.
Steps 1-3 are setup steps:
Decide on how big you want the numbers to be. 6-digit numbers would let you have a million numbers.
Build a temp table t with all 6-digit numbers (t_num).
Build and populate a permanent table:
CREATE TABLE `u` (
id INT AUTO_INCREMENT NOT NULL,
t_num INT NOT NULL,
PRIMARY KEY(id)
);
INSERT INTO u (t_num)
SELECT t_num FROM t ORDER BY RAND();
4--Plan A You have an auto_inc id in the real table; simply join to u to get the random 6-digit number.
4--Plan B
BEGIN;
SELECT t_num FROM u ORDER BY id LIMIT 1 FOR UPDATE; -- to get the random id
DELETE FROM u ORDER BY id LIMIT 1; -- Remove it from the list of available ids
From MySQL reference manual:
To obtain a random integer R in the range i <= R < j, use the
expression FLOOR(i + RAND() * (j − i)). For example, to obtain a
random integer in the range the range 7 <= R < 12, use the following
statement: SELECT FLOOR(7 + (RAND() * 5));
Make the field holding serviceNumber uniqe and use the above math expression to generate serviceNumbers.
If we delete row from MySQL table, and if there is some auto incremental column exists, after insertion it will be resumed incrementing by last value. That is if the last value was 1000, after deletion, incremented value will be 1001.
But what if I want it to increment that column (e.g. id) according max id each time before insertion. I know that it could be achieved by setting:
ALTER TABLE `tableName` AUTO_INCREMENT = MAX(`ID`) + 1
manually, but this sql should be called every time after the deletion or before insertion.
In MySQL table I am just inserting elements and fetching them to the java.util.List, and I only need to get data according some interval.
Getting according id is much faster than by limit.
e.g.
SELECT * FROM `TABLE_NAME` WHERE ID >=START AND ID <=END
is much faster than
SELECT * FROM `TABLE_NAME` LIMIT START, (END-START)
And getting size of the table is faster by MAX(ID) than COUNT(*)
Edit:
Regarding deletions, I only delete row from the end of the table or the rest from the id (including) that should be deleted, so that after deletion MAX(ID) and COUNT(*) will be equal.
The question is: Is there any way of setting "automated" sequential AUTO_INCREMENT in MySQL?
Can someone tell me a good method for automatically placing a unique random number in a mysql database table when a new record is created.
I would create a table with a pool of numbers:
Create Table pool (number int PRIMARY KEY AUTO_INCREMENT);
Insert Into pool (),(),(),(),(),(),(),(),…;
And then define a trigger which picks one random number from that pool:
CREATE TRIGGER pickrand BEFORE INSERT ON mytable
FOR EACH ROW BEGIN
DECLARE nr int;
SET nr = (SELECT number FROM pool order by rand() limit 1);
DELETE FROM pool WHERE number = nr;
SET NEW.nr = nr;
END
In order to avoid concurrency issues you have to run queries in transactions. If performance becomes an issue (because of the slow order by rand()) you can change the way to select a random record.
Your criteria of unique and random are generally conflicting. You can easily accomplish one or the other, but both is difficult, and would require evaluating every row when testing a new potential number to insert.
The best method that meets your criteria is to generate a UUID with the UUID function.
The better choice would be to re-evaluate your design and remove one of the (unique, random) criteria.
Your best option is Autoincrement Column
see here for syntax
perhaps for Random number
try this!
select FLOOR(RAND() * 401) + 100
edit
SELECT FLOOR(RAND() * 99999) AS sup_rand
FROM table
WHERE "sup_rand" NOT IN (SELECT sup_rand FROM table)
LIMIT 1
Steps:
1.Generate a random number
2.Check if its already present
3.if not continue use the number
4.Repeat
In our application, when a user creates an order we get the next order # as follows:
SELECT MAX(CAST(REPLACE(orderNum, 'SO', '') AS SIGNED)) + 1 FROM orders
The problem is that because the customer is getting busier, we are starting to see orders that are created at exactly the same time which results in duplicate order #'s.
What is the best way to handle this? Should we lock the whole orders table or just the row? Or should we be doing transactions?
You could create an extra table with just one field which has the auto_increment attribute.
Now every time you need a new order number you call a function that will create a field in this table and return the result of last_insert_id(), which you then can use as an order number (just make sure to set the auto_increment counter of the table higher than your greatest order number).
is there an SQL command to delete the first X lines of a database table?
I have a database table containing some information but no id or auto-incrementing value and a program that processes the first X lines of this table.
Afterwards these X lines need to be deleted. So the standard query is:
DELETE FROM table WHERE something = value;
So, is there a way to build a query like:
DELETE FROM table WHERE rownumber <= X;
I have tried this command, but nothing happens to the database..
Do you have any clue?
Use LIMIT on your delete:
DELETE FROM table WHERE condition LIMIT 10
Or, if you don't want the condition
DELETE FROM table LIMIT 10
Remember that the order in which rows will be deleted is undefined - it depends on your DBMS configuration and table indices. You should include an ORDER BY so that the deletion is done in a defined order, e.g. ORDER BY id ASC to delete the lowest IDs first.
See the MySQL documentation for DELETE for more details.