Changing autoincrement field values as per year in MYSQL - mysql

i have a field id in my table which is autoincrement field. It starts from 1,2,3,........ Now every year April 1st i need to reset this id. What ever record i enter from April 1st should start again from 1. It doesnt need to be updated automatically. I can do it manually every year. What are the options available to do the same? Can anyone please suggest a simple way through which i can achieve the same.
Table tender
id (autoincrement)
tender_id
...
...

Use alter table to modify the auto_increment for table.
alter table t1 auto_increment = 1;

Related

Auto increment in mysql by more than 1

I have an invoicing system that runs off an sql db.
Every time I create a new invoice, the invoice number increases by 1.
So invoice number 5000, the next is 5001, then 5002 and so on.
I want so that the next invoice number increased by a different number, Say 15.
So invoice number 5000, then 5015, then 5030 etc.
Is it possible to change something in phpmyadmin to achieve this.
TIA
Try this, do note that this is global, and not just one table. If you are going to just have this happen on one table, create an stored procedure to set the id instead of auto increment.
SET ##auto_increment_increment=2;
SET ##auto_increment_offset=2;
Documentation:
https://dev.mysql.com/doc/refman/8.0/en/replication-options-source.html#sysvar_auto_increment_increment
You can also just have a subquery decide what the invoice no is supposed to be and not have it autoincremented. I would suspect that this code should be written in the invoicing software itself (Which might not be possible, in your case, at which point my top example is the only way to go) but if you can edit the software, you simply need to select the highest invoice that exists, and then + 2 - then store that in the column you present.
You can do it with two columns. One ID with normal auto increment and one InvoiceNumber with a default value of (ID * 15):
ALTER TABLE `definitions`
ADD COLUMN `InvoiceNumber` INT NOT NULL DEFAULT (ID * 15) AFTER `DisplayOrder`;
I know that you can modify the step in mySQL, but it is a global change which would affect all tables. I suggest that you leave the data as is and use a view to multiply the values by 15.
Obviously you will replace the column description with a number of columns with the real information, date, customer etc. etc.
create table invoice_data(
id int primary key AUTO_INCREMENT,
description varchar(100)
);
create view invoices as
select
15 * id as invoice_number,
description
from invoice_data;
insert into invoice_data (description) values
('invoice 1 information'),('invoice 2 information');
select * from invoices;
invoice_number | description
-------------: | :--------------------
15 | invoice 1 information
30 | invoice 2 information
db<>fiddle here

Update pricing table with new value and field to record date

I am managing an eCommerce database that has Amazon sales data and am still learning MySQL. I am going to create a new pricing table, but am not 100% I am doing this the best way and want advice.
The table data I get from Amazon includes ASIN (Amazon's SKU that is unique to each product) and the price. There are some other pieces of data, but those are the most important. I want to add two pieces of info that I think make this table easier to maintain. One is a created date field that is when I add a new ASIN that is not already there. The second is an update date field showing the date when an ASIN price is updated to a newer price. By doing this I do not have historical pricing detail, but I don't think I would need it.
Question #1: I think I want this line in the create statement for create date field:
create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Correct?
Question #2: How would I update this table so it does not add a new record for an existing ASIN, but instead updates to the new price and changes the update_date value to the timestamp when that update was made?
You can define the update_date field as
update_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
and update the record simply as
UPDATE sales_table SET price = 123 WHERE id = 4
and the update_date column will change to reflect the new update timestamp.

How to delete the gaps & reset the auto_increment value in mysql

http://tinypic.com/view.php?pic=2hd2ush&s=8#.U7vlf_mSzNg
I created the table which have auto_increment in id field, when i delete some row & insert another row it having a gaps between them. For example id is 1,2,3,4,5. Deleted 3,5 and insert another row, the id will be 1,2,4,6, it should be 1,2,3,4,5. Please help.
That behaviour is normal and desired. You don't want to "reset" auto_increment counter, its job is not to provide you with nice, sequential numbers.
Thanks for the clarify N.B

How to Permanently Delete Table Data

I have table that have 4 fields.
i.e.
1. id : Primary key, Auto Increment ,
2. name,
3. salary,
4. designation.
when I add data id increments automatically.
Suppose when I remove or delete row that have id = 15 i.e. last record and after that if I add new record then id starts from 16.
So that is my problem, when I remove record It should be permanently removed.
and id starts from 15 again.
Any suggestion for it ?
The purpose of the Primary Key on a table on RDBMS is to uniquely identify a record FOREVER. Even if you delete this record, its id "remains there" forever. The world needs to know that the record with id e.g. 15, existed once, but does not exist anymore. You should not be reusing primary keys that have been deleted. It is a wrong business and technical approach. You will get into much trouble.
If you have used sequence for auto increment for the 'id' and you delete a record with id = 15 then the sequence will get the next value i.e 16 as it has already picked the value 15 as it is independent of whether the id exists in the table or not. Hope it helps you.

How to handle fragmentation of auto_increment ID column in MySQL

I have a table with an auto_increment field and sometimes rows get deleted so auto_increment leaves gaps. Is there any way to avoid this or if not, at the very least, how to write an SQL query that:
Alters the auto_increment value to be the max(current value) + 1
Return the new auto_increment value?
I know how to write part 1 and 2 but can I put them in the same query?
If that is not possible:
How do I "select" (return) the auto_increment value or auto_increment value + 1?
Renumbering will cause confusion. Existing reports will refer to record 99, and yet if the system renumbers it may renumber that record to 98, now all reports (and populated UIs) are wrong. Once you allocate a unique ID it's got to stay fixed.
Using ID fields for anything other than simple unique numbering is going to be problematic. Having a requirement for "no gaps" is simply inconsistent with the requirement to be able to delete. Perhaps you could mark records as deleted rather than delete them. Then there are truly no gaps. Say you are producing numbered invoices: you would have a zero value cancelled invoice with that number rather than delete it.
There is a way to manually insert the id even in an autoinc table. All you would have to do is identify the missing id.
However, don't do this. It can be very dangerous if your database is relational. It is possible that the deleted id was used elsewhere. When removed, it would not present much of an issue, perhaps it would orphan a record. If replaced, it would present a huge issue because the wrong relation would be present.
Consider that I have a table of cars and a table of people
car
carid
ownerid
name
person
personid
name
And that there is some simple data
car
1 1 Van
2 1 Truck
3 2 Car
4 3 Ferrari
5 4 Pinto
person
1 Mike
2 Joe
3 John
4 Steve
and now I delete person John.
person
1 Mike
2 Joe
4 Steve
If I added a new person, Jim, into the table, and he got an id which filled the gap, then he would end up getting id 3
1 Mike
2 Joe
3 Jim
4 Steve
and by relation, would be the owner of the Ferrari.
I generally agree with the wise people on this page (and duplicate questions) advising against reusing auto-incremented id's. It is good advice, but I don't think it's up to us to decide the rights or wrongs of asking the question, let's assume the developer knows what they want to do and why.
The answer is, as mentioned by Travis J, you can reuse an auto-increment id by including the id column in an insert statement and assigning the specific value you want.
Here is a point to put a spanner in the works: MySQL itself (at least 5.6 InnoDB) will reuse an auto-increment ID in the following circumstance:
delete any number rows with the highest auto-increment id
Stop and start MySQL
insert a new row
The inserted row will have an id calculated as max(id)+1, it does not continue from the id that was deleted.
As djna said in her/his answer, it's not a good practice to alter database tables in such a way, also there is no need to that if you have been choosing the right scheme and data types. By the way according to part od your question:
I have a table with an auto_increment field and sometimes rows get deleted so auto_increment leaves gaps. Is there any way to avoid this?
If your table has too many gaps in its auto-increment column, probably as a result of so many test INSERT queries
And if you want to prevent overwhelming id values by removing the gaps
And also if the id column is just a counter and has no relation to any other column in your database
, this may be the thing you ( or any other person looking for such a thing ) are looking for:
SOLUTION
remove the original id column
add it again using auto_increment on
But if you just want to reset the auto_increment to the first available value:
ALTER TABLE `table_name` AUTO_INCREMENT=1
not sure if this will help, but in sql server you can reseed the identity fields. It seems there's an ALTER TABLE statement in mySql to acheive this. Eg to set the id to continue at 59446.
ALTER TABLE table_name AUTO_INCREMENT = 59446;
I'm thinking you should be able to combine a query to get the largest value of auto_increment field, and then use the alter table to update as needed.