5

MySQL Current table

CREATE TABLE document_control (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT...

From the above - the id will be created in the following sequence: 1,2,3,4,5,6,7....

Intention

I need the sequence to go like so:

19-001
19-002
19-003

Explanation

19 - Todays year date format (yy)

001 - Increments by +1 when a new value is added.

Next year (2020)..

the sequence needs to be reset back to 001 but the 19 changes to 20 because of the year being 2020 :

20-001
20-002
20-003

Question

How can I create this custom ID column?

2 Answers 2

4

You can use something like the following using a INSERT ... SELECT:

INSERT INTO document_control 
  SELECT CONCAT_WS('-', RIGHT(YEAR(CURRENT_TIMESTAMP), 2), LPAD(COUNT(*) + 1, 3, 0)) 
  FROM document_control
  WHERE LEFT(id_custom, 2) = RIGHT(YEAR(CURRENT_TIMESTAMP), 2)

Note: It can be dangerous to use such a generated custom ID as identifier for specific records since the custom ID can change after changing the data (UPDATE or DELETE) of the table. So I don't recommend to use this custom ID as foreign key on other tables.


A better solution (in my opinion) would be the following:

CREATE TABLE document_control (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  name VARCHAR(10)
);

You are using a table with column id using auto increment (so the database organizes the ID itself) and the created_at column to store the date and time of the creation. You can use CURRENT_TIMESTAMP as default value so you don't have to provide a value on INSERT everytime. With these two columns you can get your custom ID with a simple query:

SELECT *, CONCAT_WS('-', RIGHT(YEAR(created_at), 2), LPAD(id, 3, 0)) AS custom_id 
FROM document_control

You can create a VIEW to generate the custom ID in the background. In this case you don't have to build the custom ID on every SELECT yourself:

-- create the VIEW
CREATE VIEW v_document_control AS
  SELECT *, CONCAT_WS('-', RIGHT(YEAR(created_at), 2), LPAD(id, 3, 0)) AS custom_id 
  FROM document_control

-- use the VIEW
SELECT * FROM v_document_control

In case you need a consecutive number without gaps and starting on "1" every year, you can use the above example (same columns) but with the following SELECT using ROW_NUMBER (since MySQL 8.0):

SELECT *, CONCAT_WS('-', RIGHT(YEAR(created_at), 2), LPAD(ROW_NUMBER() OVER (PARTITION BY YEAR(created_at) ORDER BY id), 3, 0)) AS custom_id 
FROM document_control

demo on dbfiddle.uk

2
  • 1
    Insert select should work, but this creates a problem, having the responsibility to ensure that every time a record is inserted this is called. It looks much more like a trigger's job. Commented Dec 18, 2019 at 13:40
  • I wouldn't use the INSERT ... SELECT. It is a possibility to solve this but there are better ways (as described). Commented Dec 18, 2019 at 13:43
0

You can create a stored function which would run a selection to get the highest ID value, the equivalent of

select max(id) from document_control

The function would check whether the year is the current one and if so, it would use it as the first part and concatenate the incremented second part. If the document represented the last year, then you can use current year and concatenate -001 to it. And then you can call it from a before insert trigger.

However, it is not advisable to work like this, because you violate NF1. It makes much more sense to have a numeric id, a year column, a number column and a stored function which would calculate this concatenation. You can of course index the underlying columns for perfromance's sake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.