1

I'm using Netbeans as my IDE and MySQL as my database, I want to create auto increment id coming up with Strings Like USER101, USER102,... How can I create this auto-increment id? I tried this method do generate auto-increment id in integer my method

FGH
  • 2,900
  • 6
  • 26
  • 59
  • 1
    I'm not sure if you can. As you already know, an Integer is an integer and a String is a string, TEXT is text and VARCHAR is varchar. You can't mix them to make a unique data column that auto-increments. What you can do however is either make the data column TEXT and implement a Unique incremental ID via code OR leave the database column as Integer (or Long...whatever) and create the String in your GUI when displaying the table data in a table (or whatever) component. I would think the Header would be enough information: Header: USER column data: 100, 101, 102, ... You should just use Integer – DevilsHnd - 退職した Mar 29 '20 at 17:41
  • 1
    Maybe [this SO thread](https://stackoverflow.com/questions/14434132/how-to-make-string-auto-increment) will help you out. – DevilsHnd - 退職した Mar 29 '20 at 17:41
  • Thank you, man, a big help to me, Happy Coding – FGH Mar 29 '20 at 18:10

1 Answers1

1

If id is your auto increment column, make a second generated column with

 CONCAT(''USER'',id);

As generating formula.

The next code would change a column to generate automatocally the USER101 depending on the id from the autoincrement.

ALTER TABLE `testdb`.`testtable` 
CHANGE COLUMN `columname`  CHAR(50) NULL GENERATED ALWAYS AS (CONCAT('USER',id)); 

But i don't know how you make this in NEtbeans and google also didn't help.

nbk
  • 45,398
  • 8
  • 30
  • 47
  • 1
    You can't do that in NetBeans GUI, but can execute query like yours in SQL window and achieve whatever you want. – sbrbot Mar 29 '20 at 19:37
  • 1
    that is why i postet the raw query, to use. – nbk Mar 29 '20 at 19:42
  • Don't delete comment that I'm referring to. I'm just confirming your q in comment that one cannot do it in NetBeans GUI. – sbrbot Mar 29 '20 at 19:46