6

Here I am using MySQL and I want my primary key to start with a letter, like D000. Then everytime I enter a new record the primary key auto increments like so:

D001 D002 D003.

How can I do this?

Bas Pauw
  • 262
  • 1
  • 12
Kogile
  • 528
  • 5
  • 16
  • 2
    Which is the purpose of this letter? If you want an autoincrement, why you need a letter before it? – Narmer Sep 10 '14 at 10:44
  • 1
    why? primary key - pk should not be miced with other requirements – tgkprog Sep 10 '14 at 10:44
  • 2
    What should happen when you'll have to insert 1001th record in that table? – raina77ow Sep 10 '14 at 10:46
  • yes, I need to make autoincrement, D should be the starting letter which is default.then with this the new record will increment as from D001 then, D002 and so on. – Kogile Sep 10 '14 at 10:49
  • I can have D1,D2,D3 and so on, D should be default. – Kogile Sep 10 '14 at 10:51
  • 1
    Maybe you can use a Composite Primary key? http://stackoverflow.com/q/1110349/1689695 – CocoNess Sep 10 '14 at 10:54
  • 1
    @CocoNess in this way the autoincrement is useless – Narmer Sep 10 '14 at 10:54
  • @Narmer why is the autoincrement useless? – CocoNess Sep 10 '14 at 10:58
  • 1
    @CocoNess the autoincrement is used when you want a single PK always different. If you use a composite PK, why would one of the values be an autoincrement? Or you use an autoincrement as PK or you use a composite PK. – Narmer Sep 10 '14 at 10:59
  • 1
    Despite it's name the purpose of AUTO_INCREMENT is simply to guarantee uniqueness. It's irrelevant that the values are incremental (though somewhat relevant that they're sequential). An auto-incremented value should have no significance beyond the scope of the tables that reference it. If it does, do not use auto_increment for this. – Strawberry Sep 10 '14 at 11:51

5 Answers5

1

You can't AUTO_INCREMENT a column whose type is VARCHAR.

What you could do is make it BIGINT and AUTO_INCREMENT, and whenever you need it as String, you can prepend it with your letter 'D' like:

Long dbKey = ...;
String key = "D" + dbKey;

You could create a stored procedure for this to set an "auto-incremented" string as the default value for this column, but it just doesn't worth the hassle. Plus working with numbers is always faster and more efficient than working with strings.

icza
  • 389,944
  • 63
  • 907
  • 827
0

Not sure why you need it but you can add the D AFTER you fetched the data (String id = "D" + autoIncId;).

You can't insert a string or anything in an autoincrement field and I can't see anyway this can be useful (all the recorde will have a D, so no one has).

If you want to declare a row default, you can add a boolean column named DEFAULT.

while(rs.next()){
    String id = rs.getBoolean("DEFAULT")?"D":"ND";
    id+=rs.getLong(1);
}

EDIT

As per your comment I understand that you want to select the max ID and add 1 to it. Then it's ok to use an autoincrement field in your DB and it must be a number type (INTEGER, BIGINT...).

Please FORGET to add the "D" to your primary key, it will simply not going to work as you want. The autoincrement takes the last inserted ID and adds 1 to it. If your last id is "D3" adding 1 has the same meaning as adding 4 to "apple". You are using different types.

There is no way for SQL or any other programming language to understand that if you add 1 to "D3" it should become "D4". What you need to do is get rid of that D (which purpose I still don't understand).

Narmer
  • 1,414
  • 7
  • 16
  • Here i am writing in the query id=D1,D2,D3, select max(id) as id from test, id=id+1,then it should give result as id=D4. D1,D2,D3 are the records.(is it possible D1+1) – Kogile Sep 10 '14 at 11:25
  • You can't add a string to an int. "D1" is a string (VARCHAR in DB), you can't add 1 to it. You should remove te D's from the DB and add them in java. – Narmer Sep 10 '14 at 12:19
0

I'm not sure whether I get your question right, but shouldn't the following work?

CREATE TRIGGER myTrigger
  BEFORE INSERT
  ON myTable
  FOR EACH ROW
BEGIN
  SET NEW.myCustomId = COALESCE('D', RPAD('0',3,NEW.id)); 
END

for this case you NEED a "normal" primary key column..

Edi G.
  • 2,432
  • 7
  • 24
  • 33
0

Two ideas.

  • (Useless IMHO) I think Maria DB has virtual columns, though MySQL I think not. But you have views. So you could make a normal INT, AUTOINCREMENT and in the view have a calculated column concatting your key.

  • One can use different number ranges for different tables.

    ALTER TABLE debtors   AUTO_INCREMENT=10000;
    ALTER TABLE creditors AUTO_INCREMENT=30000;
    ALTER TABLE guests    AUTO_INCREMENT=50000;
    

    This admittedly is a lame solution, but might do. I think such a distinction might be what you are aiming at.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Yo may try to do this aberration at your own risk:

INSERT INTO table (id, a, b, c) 
      VALUES ( fn_get_key( LAST_INSERT_ID("table_name ") +1), "a", "b", "c");

Where fn_get_key is a function that will convert the number into your desired string AND will execute:

ALTER TABLE table_name AUTO_INCREMENT = start_value;

Anyway I do not recommend your approach. Numeric strings are faster and easier to sort. You could always create a view that transforms the ID or use logic o change from the "D001" key to "1". Foreing key and uniqness of ids enforcement will be harder and more expensive

borjab
  • 11,149
  • 6
  • 71
  • 98