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 7594483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:25:39+00:00 2026-05-30T21:25:39+00:00

I have a table member . I want to make an user id using

  • 0

I have a table member. I want to make an user id using auto increment.
I have tried using the following:

SET new.user_id = CONCAT(new.name, LPAD((SELECT AUTO_INCREMENT FROM 
information_schema.TABLES WHERE
TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'member'),3,'0'));

But when I inserted a new member, i’ve got an error message user_id cannot be null and the data wasn’t added. I want the user id will shown like jos001 as an primary key.

Please help me.. how to make it happen?

  • 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-30T21:25:40+00:00Added an answer on May 30, 2026 at 9:25 pm

    Try this BEFORE INSERT trigger –

    Full script:

    CREATE TABLE member(
      user_id VARCHAR(23) NOT NULL,
      name VARCHAR(20) DEFAULT NULL
    );
    
    DELIMITER $$
    
    CREATE TRIGGER trigger1
      BEFORE INSERT
      ON member
      FOR EACH ROW
    BEGIN
    
      -- Find next id for new user - MAX+1
      SELECT MAX(CAST(SUBSTRING(user_id, LENGTH(user_id) - 2, 3) AS SIGNED)) + 1 INTO @next_id
        FROM member WHERE SUBSTRING(user_id, 1, LENGTH(user_id) - 3) = NEW.name;
    
      SET NEW.user_id = CONCAT(NEW.name, LPAD(IFNULL(@next_id, 1), 3,'0'));
    END$$
    
    DELIMITER ;
    

    Insert some records:

    INSERT INTO member VALUES('', 'jos');
    INSERT INTO member VALUES('', 'jos');
    INSERT INTO member VALUES('', 'jos');
    INSERT INTO member VALUES('', 'alex');
    INSERT INTO member VALUES('', 'alex');
    INSERT INTO member VALUES('', 'jos');
    

    Check result:

    SELECT * FROM member;
    
    +---------+------+
    | user_id | name |
    +---------+------+
    | jos001  | jos  |
    | jos002  | jos  |
    | jos003  | jos  |
    | alex001 | alex |
    | alex002 | alex |
    | jos004  | jos  |
    +---------+------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table with an auto-increment field and I am trying to
I have a custom DataContext with a Table member... Entry is defined as... [Table(Name=Entry)]
I have a number of database tables below. I want to display each user
I have a Linq object, and I want to make changes to it and
I have a TABLE A( Type INT, Value INT ) Now I want to
I have a members table in MySQL CREATE TABLE `members` ( `id` int(10) unsigned
We have two tables Family and Member, the relation between these two is Family
I have a table that contains members names and a field with multiple ID
We have a partitioned view with 12 member tables. The partitioning column is a
I have table inside a div tab. The table has 40 rows in it

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.