Friday 27 July 2012

Generate Auto Id


Logic to generate unique id -

Create a table in the database named tbl_id and set the fields (the number of unique id’s to be generated. For Example – In Computer rental system, there is a need to generate id for both the customer and the Computer. So, two fields one for the customer and second for the computer must be set / defined. One thing to make sure is that the data type of fields must be Number)

Set the default value to 1000. (Depending on the requirement if the user wants that the id must start from 1 set the default values to 1)

On the form or the web page where unique need to be generated a select query need to be written in which the field value from the table tbl_id need to be selected. For Example – In case of customer id, select the customer_id field value from the table tbl_id by writing a query like this – SELECT customer_id from tbl_id.

Store the result obtained from this query in a variable of type integer. Now if the user wants that the id must be in a suitable format such as CS – 1000, or CUS – 1000 or whatever the format is just concatenating the prefix required with this variable. (make sure that concatenation does not change the value of the variable.

On the click of the save button or key depending on the programming language or the way the action is being performed update the value of the variable by adding 1 to it and update this variable value again in the tbl_id customer_id field.

No comments:

Post a Comment