Autoincrement with prefix and update after deletion

Hi, I have a table with some fields and 2 of them are:
ID int(10) PK AI
RefNum varchar(50)
I want the reference number to start with the prefix RE-001 and I want everytime i insert a new record to increment it accordingly
ID          RefNum
1           RE-001
2           RE-002
3           RE-003
How can this be done, i am aware that auto increment can be done on INT but maybe there is another way
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could do it with a insert-trigger that relies on the autoincrement of the id-column. The trigger should build the string: "RE-" and concat to the id the should be formatted with 3 digits.
Here's the docu to trigger:

Please have a look at the example below.

Hope this helps,

$insquery = mysql_query("INSERT INTO tbltest (ID) VALUES ('')") or die(mysql_error());
$lastinsert = mysql_insert_id();
$refNum = 'RE-'.str_pad($lastinsert, '3', '0', STR_PAD_LEFT);
$updquery = mysql_query("UPDATE tbltest SET refNum = '$refNum' WHERE ID = '$lastinsert'") or die(mysql_error());

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Also, for reference, here is the MySQL table.
CREATE TABLE `tbltest` (
  `ID` int(10) NOT NULL auto_increment,
  `refNum` varchar(50) NOT NULL,

-- Dumping data for table `tbltest`

INSERT INTO `tbltest` (`ID`, `refNum`) VALUES
(1, 'RE-001'),
(2, 'RE-002');

Open in new window

dino_angelidesAuthor Commented:
well, let me try this one later :)
dino_angelidesAuthor Commented:
I did not get a chance to try that yet.....
my insert code, which derived from dreamweaver insert behavior is below in the snippet....where should I add the code you provided?
also there is a textbox of the Reference Number which is readonly
 <input name="refNumber" type="text" class="style811" id="refNumber" size="50" maxlength="50" readonly="readonly" />
and I want to display the next available reference number in there so that the user cannot change it!!
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO listings (id, datePosted, featured, listingType, refNum, contactName, contactPhone, contactMobile, contactEmail, region, area, address, price, priceRequested, beds, baths, parking, propType, coveredArea, pool, furnished, deeds, postedBy, deliveryDate, mapLocation, briefComments, comments, image1, image2, image3, image4, image5, image6, image7, image8) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['datePosted'], "text"),
                       GetSQLValueString(isset($_POST['featured']) ? "true" : "", "defined","'Yes'","'No'"),
                       GetSQLValueString($_POST['listingType'], "text"),
                       GetSQLValueString($_POST['refNumber'], "text"),
                       GetSQLValueString($_POST['contactName'], "text"),
                       GetSQLValueString($_POST['contactPhone'], "text"),
                       GetSQLValueString($_POST['contactMobile'], "text"),
                       GetSQLValueString($_POST['contactEmail'], "text"),
                       GetSQLValueString($_POST['region'], "text"),
                       GetSQLValueString($_POST['area'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['price'], "text"),
                       GetSQLValueString($_POST['sellerPrice'], "text"),
                       GetSQLValueString($_POST['bedrooms'], "text"),
                       GetSQLValueString($_POST['bathrooms'], "text"),
                       GetSQLValueString($_POST['parking'], "text"),
                       GetSQLValueString($_POST['propertyType'], "text"),
                       GetSQLValueString($_POST['coveredArea'], "text"),
                       GetSQLValueString($_POST['pool'], "text"),
                       GetSQLValueString($_POST['furnished'], "text"),
                       GetSQLValueString($_POST['deeds'], "text"),
                       GetSQLValueString($_POST['postedBy'], "text"),
                       GetSQLValueString($_POST['deliveryDate'], "text"),
                       GetSQLValueString($_POST['maplocation'], "text"),
                       GetSQLValueString($_POST['commentsBrief'], "text"),
                       GetSQLValueString($_POST['comments'], "text"),
                       GetSQLValueString($_POST['image1'], "text"),
                       GetSQLValueString($_POST['image2'], "text"),
                       GetSQLValueString($_POST['image6'], "text"),
                       GetSQLValueString($_POST['image6'], "text"),
                       GetSQLValueString($_POST['image5'], "text"),
                       GetSQLValueString($_POST['image6'], "text"),
                       GetSQLValueString($_POST['image7'], "text"),
                       GetSQLValueString($_POST['image8'], "text"));

  mysql_select_db($database_RealEstateOnlineCy, $RealEstateOnlineCy);
  $Result1 = mysql_query($insertSQL, $RealEstateOnlineCy) or die(mysql_error());

  $insertGoTo = "/RealEstateOnline/admin/addListingSuccess.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  header(sprintf("Location: %s", $insertGoTo));

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.