skip to Main Content

The similar questions has already been asked, but my question is quite different with some research done over it.
On signing up to a website, I provide
FirstName: Ajay, LastName: Kumar
Now the backend API need to create a unique-Id from these two texts. Why?
Yes, I have used the autoincrement Integer column for storing the user-id, but this is the Unique_id which will be shown in the URL of the user’s profile page, the way most of the websites(fb, twitter, quora) do. I think it helps in SEO for searching the people’s profile.

Approaches in my mind:

1. FirstName + LastName + mysql-auto-generated-id
API cannot know the autogenerated ID before the creation of the user.

2. Concat FirstName & LastName and us it as unique id, if the DB fails
and returns id violation error, then start appending digits starting from 0 until an valid Id is found.

It increases the database roundtrips.

3. FirstName + LastName + [random integer numbers]
It also increse the database roundtrip.

4. Use UUID concatenated to FirstName + LastName.
Since UUID is 128-bit, that’s quite long and I need to substring it. This could again cause duplicate IDs

5. concatenating current timestamp with FirstName + LastName.
But that value is also quite long.

The most efficient way I have is to use UUID and substring 4-5 starting characters from it. If the unique-id is already taken, try with another UUID. I think this is best in case of decreasing the database roundtrips as well.

I am curious to know how websites handle this(other than this database recursive calls until a valid Unique-id is found)? There is proper digit assingnment in unique-id(e.g in Quora). E.g tim-cook-1, time-cook-2.

6

Answers


  1. You can just get the count+1 of the record from data base having
    Firstname as ‘Arjun’ and Lastname as ‘Kumar’ then just add to your user name

    It will create user name in this pattern

    Arjun-Kumar-1

    Arjun-Kumar-2

    Arjun-Kumar-3

    Login or Signup to reply.
  2. UUID is a good tool to use. You can also try the Crypto module while working with node.js

    Login or Signup to reply.
  3. I assume that you’re using MySQL. Here’s a possible solution dealing with concurrent requests:

    1. create a table (first_name, last_name, count)
    2. when you get a new user registration request:

      • begin a new transaction
      • issue a select for update on this table
      • if no row, then count = 1 & insert; else increment counter & update
      • commit
    Login or Signup to reply.
  4. I cannot see the problem here. Save the object, then create the URL from the name and the already existing id and return it to the client.
    You don’t even have to store that unique id separately, because you can build it easily any time from the other three fields.

    Login or Signup to reply.
  5. You can use the following generator and select how long your unique string must be that you append to the firstname-surname-[uniquenumber]

    https://alex7kom.github.io/nano-nanoid-cc/?alphabet=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&size=8&speed=1000&speedUnit=second

    If you make a unique constraint on this firstname-surname-[uniquenumber] field in the database, you can detect a collision and then just regenerate a uniquenumber again.

    Login or Signup to reply.
  6. ----
    CREATE TABLE name_card
    (id INT(10) AUTO_INCREMENT,
    name_card VARCHAR(200),
    value_card VARCHAR(200),
    PRIMARY KEY(id));
    ----
    
    ----
    DELIMITER $$     
    DROP FUNCTION IF EXISTS unicode_function$$   
    CREATE FUNCTION unicode_function (name_ VARCHAR(200)) 
    RETURNS VARCHAR(200)    
    BEGIN 
    DECLARE var INT(10);    
    DECLARE nam VARCHAR(100);    
    SET var=500;    
    IF EXISTS(SELECT name_card FROM name_card WHERE name_card=name_) THEN     
    SELECT value_card INTO nam FROM name_card WHERE name_card=name_;  
    RETURN nam; 
    ELSE 
    INSERT INTO name_card(name_card,value_card)
    SELECT name_,(SELECT IFNULL((MAX(value_card)+1),var) value_card FROM name_card); 
    SELECT value_card INTO nam FROM name_card WHERE name_card=name_; 
    RETURN nam; 
    END IF; 
    END$$ 
    DELIMITER ; 
    ----

    Based on the given name The below Function generates unique id.
    Create a table mentioned, execute Function and try.

    Create table and Function Script

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search