Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:05:39+00:00 2026-05-26T16:05:39+00:00

First, I apologize if this has been asked before – indeed I’m sure it

  • 0

First, I apologize if this has been asked before – indeed I’m sure it has, but I can’t find it/can’t work out what to search for to find it.

I need to generate unique quick reference id’s, based on a company name. So for example:

Company Name                Reference
Smiths Joinery              smit0001
Smith and Jones Consulting  smit0002
Smithsons Carpets           smit0003

These will all be stored in a varchar column in a MySQL table. The data will be collected, escaped and inserted like ‘HTML -> PHP -> MySQL’. The ID’s should be in the format depicted above, four letters, then four numerics (initially at least – when I reach smit9999 it will just spill over into 5 digits).

I can deal with generating the 4 letters from the company name, I will simply step through the name until I have collected 4 alpha characters, and strtolower() it – but then I need to get the next available number.

What is the best/easiest way to do this, so that the possibility of duplicates is eliminated?

At the moment I’m thinking:

$fourLetters = 'smit';
$query = "SELECT `company_ref`
            FROM `companies`
          WHERE
            `company_ref` LIKE '$fourLetters%'
          ORDER BY `company_ref` DESC
          LIMIT 1";
$last = mysqli_fetch_assoc(mysqli_query($link, $query));
$newNum = ((int) ltrim(substr($last['company_ref'],4),'0')) + 1;
$newRef = $fourLetters.str_pad($newNum, 4, '0', STR_PAD_LEFT);

But I can see this causing a problem if two users try to enter company names that would result in the same ID at the same time. I will be using a unique index on the column, so it would not result in duplicates in the database, but it will still cause a problem.

Can anyone think of a way to have MySQL work this out for me when I do the insert, rather than calculating it in PHP beforehand?

Note that actual code will be OO and will handle errors etc – I’m just looking for thoughts on whether there is a better way to do this specific task, it’s more about the SQL than anything else.

EDIT

I think that @EmmanuelN’s suggestion of using a MySQL trigger may be the way to handle this, but:

  • I am not good enough with MySQL, particularly triggers, to get this to work, and would like a step-by-step example of creating, adding and using a trigger.
  • I am still not sure whether this will will eliminate the possibility of two identical ID’s being generated. See what happens if two rows are inserted at the same time that result in the trigger running simultaneously, and produce the same reference? Is there any way to lock the trigger (or a UDF) in such a way that it can only have one concurrent instance?.

Or I would be open to any other suggested approaches to this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:05:39+00:00Added an answer on May 26, 2026 at 4:05 pm

    If you are using MyISAM, then you can create a compound primary key on a text field + auto increment field. MySQL will handle incrementing the number automatically. They are separate fields, but you can get the same effect.

    CREATE TABLE example (
    company_name varchar(100),
    key_prefix char(4) not null,
    key_increment int unsigned auto_increment,
    primary key co_key (key_prefix,key_increment)
    ) ENGINE=MYISAM;
    

    When you do an insert into the table, the key_increment field will increment based on the highest value based on key_prefix. So insert with key_prefix “smit” will start with 1 in key_inrement, key_prefix “jone” will start with 1 in key_inrement, etc.

    Pros:

    • You don’t have to do anything with calculating numbers.

    Cons:

    • You do have a key split across 2 columns.
    • It doesn’t work with InnoDB.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, let me apologize if this has been asked already, but I can’t
I apologize if this has been asked but I can't seem to find it
First off, apologies if this question has been asked before but I couldn't find
If this has been asked before, I apologize but this is kinda of a
First off, I apologize if this question has been asked before. I've done a
I apologize if this has been asked before, but I believe that for me,
I apologize if this has been asked before but I am trying to parse
If this has been asked before I apologize but I wasn't able to get
Apologies if this has been answered before but I can't find a reference. I
Ok, first off, I applogize if this question has been asked before, I've spent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.