0

I have a table 'user' with 3 items.

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`customernumber` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`type` enum('customer','admin') COLLATE utf8_unicode_ci NOT NULL,

How can I increment 'customernumber' with the first two letters of the 'type'?

For example, user with ID=10 and type='admin' will have a customernumber 'AD000010'. And user with ID = 12 and type = 'customer' will have a customernumber 'CU000012'?

Is this possible to do this in MySQL without using a trigger? If not, how can I do this with a trigger?

2
  • No, you need a trigger for that. Commented Nov 30, 2013 at 13:27
  • Why do you need to store this in a single column? They're two discrete pieces of data. Store them in separate columns and concatenate them when you extract them from the database, if you require that. Commented Nov 30, 2013 at 13:29

1 Answer 1

4

What you could do is store the key as two columns. A char prefix and an auto-incrementing int, both of which are grouped for the primary key.

CREATE TABLE myItems (
    id INT NOT NULL AUTO_INCREMENT,
    prefix CHAR(30) NOT NULL,
    PRIMARY KEY (id, prefix),
    ...

Please refer to this link How to make MySQL table primary key auto increment with some prefix hope this help you .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.