Automatically add letters before auto-increment fieild

Is it possible to add a letter before auto increment in MySQL. I have several tables in my database, some of which have a unique ID generated by auto-incrementing. I want to be able to distinguish between the auto-generated numbers by the letter in front.

This is not a feature that is absolutely necessary, but it will just make small tasks easier.

+3


source to share


3 answers


Unfortunately, you cannot do this. At least not in sql. The autoincrement field is of integer type, so adding a letter there violates the constraint.

You can take a look at the link below for some sort of solution to this problem.



MySQL auto-add plus alphanumeric expression in one column

I hope this helps you in the right direction.

+3


source


You can create views for tables that must have distinctive letters in front of their identification values, and read tables through the views:

CREATE VIEW VTableA
AS
SELECT
  CONCAT('A', ID) AS ID,
  other columns
FROM TableA

      



The same goes for other tables.

+3


source


The best answer probably depends on what you mean by alphanumeric identifier. Does the alpha part increase in some way, and if so, what are the rules for doing this? If the alpha part is static, then you don't even need it in the DB: just add it to the numeric id when printing it out (perhaps using [s] printf () or similar functions to add zeros to keep it fixed in length?). However, not knowing the full requirement, we all just speculate

0


source







All Articles