1

I am having an auto increment in assigning ID. The ID assigned starts from 1 and so on. I want to have an ID that starts with characters, in PHPMYADMIN.

Example for students, ST-0001; for teachers, TE-0001.

Macoi
  • 48
  • 1
  • 5
  • 1
    possible duplicate of [How to make MySQL table primary key auto increment with some prefix](http://stackoverflow.com/questions/17893988/how-to-make-mysql-table-primary-key-auto-increment-with-some-prefix) – Antti29 Dec 11 '14 at 10:03

2 Answers2

2

Try this i hope it will work :

The idea in database design, is to keep each data element separate. And each element has its own datatype, constraints and rules. That ST0001 is not one field, but two. Same with XXnnnn or whatever. It is incorrect , and it will severely limit your ability to use the data, and use database features and facilities.

Break it up into two discrete data items:

column_1  VARCHAR(2)

column_2   INTEGER

Then set AUTOINCREMENT on column_2

And yes, your Primary Key can be (column_1, column_2), so you have not lost whatever meaning ST0001 has for you.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

if you want to get both ST-1 and TE-1, you must use a sequence table for both student and teacher, with only a AUTOINCREMENT on each table.

When you create a student, insert a row in the student_sequence, get the generated value, and then insert datas into the student table, including the Id value. For further security, add a foreign key constraint between the student ID and student_sequence ID. Same for the teacher.

A variant : http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/

David Doumèche
  • 508
  • 3
  • 10