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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:06:38+00:00 2026-05-20T13:06:38+00:00

I have a Table Employee(id,name,dept_name) .I want the id will alphanumeric [ dddddaaaaa ]

  • 0

I have a Table Employee(id,name,dept_name).I want the id will alphanumeric [dddddaaaaa] with first 5 digit will be auto increment id and rest 4 char will be the first 4 char of employee name.

For example , for the first employee name=John Todd ,the auto incremented part of the Id will be 00001. And so the Id will be 00001JOHN.

Is it possible to set a default expression to the column Id=(concat(autoincrement,substring(name,4)).

I was also thinking if I Can create a trigger on after insert Employee and the trigger will update the Employee.Id. But MySql does not allow to update the same table from trigger for which trigger got fired.

Please Help.

  • 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-20T13:06:38+00:00Added an answer on May 20, 2026 at 1:06 pm

    What about a schema like

    CREATE TABLE employee
    (
    employeeid INT PRIMARY KEY AUTO_INCREMENT,
    firstname varchar(255)
    );
    
    CREATE INDEX part_of_firstname ON employee (firstname(4));
    

    That’ll let you perform lookups fairly quickly using your natural primary key, while giving you an artificial primary key and not forcing to denormalize.

    EXPLAIN SELECT * FROM EMPLOYEE WHERE EMPLOYEEID = 1 AND FIRSTNAME LIKE 'john%';
    
    +----+-------------+----------+-------+---------------------------+---------+---------+-------+------+-------+
    | id | select_type | table    | type  | possible_keys             | key     | key_len | ref   | rows | Extra |
    +----+-------------+----------+-------+---------------------------+---------+---------+-------+------+-------+
    |  1 | SIMPLE      | employee | const | PRIMARY,part_of_firstname | PRIMARY | 4       | const |    1 |       |
    +----+-------------+----------+-------+---------------------------+---------+---------+-------+------+-------+
    

    Of course since the 0001 part of the primary key is unique enough to identify the user you need not query the name at all.

    If you insist on precalculating this should work

    CREATE TABLE employee
    (
    employeeid INT PRIMARY KEY AUTO_INCREMENT,
    specialid VARCHAR(255),
    firstname VARCHAR(255)
    );
    
    CREATE INDEX employee_specialid ON employee (firstname(4));
    
    DELIMITER ;;
    CREATE TRIGGER employeeid_trigger BEFORE insert ON employee
    FOR EACH ROW
    BEGIN
    SET new.specialid = CONCAT(LPAD((SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'employee'), 4, '0'), SUBSTRING(new.firstname, 1, 4));
    END
    ;;
    DELIMITER ;
    

    Testing it:

    mysql> insert into employee (firstname) values ('johnathan');
    Query OK, 1 row affected (0.04 sec)
    
    mysql> insert into employee (firstname) values ('johnathan');
    Query OK, 1 row affected (0.02 sec)
    
    mysql> insert into employee (firstname) values ('johnathan');
    Query OK, 1 row affected (0.02 sec)
    
    mysql> select * from employee;
    +------------+-----------+-----------+
    | employeeid | specialid | firstname |
    +------------+-----------+-----------+
    |          1 | 0001john  | johnathan |
    |          2 | 0002john  | johnathan |
    |          3 | 0003john  | johnathan |
    +------------+-----------+-----------+
    3 rows in set (0.00 sec)
    

    This is kind of a hack, and information_schema won’t be available on some DBs where permissions aren’t under your control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Employee with the following fields: EmpID Salary Name I
I have a table which has employee relationship defined within itself. i.e. EmpID Name
Suppose I have table Person table Employee, which inherits Person. I want to get
I have a table containing attributes Id, Emp_name, dept_name, salary . Now i want
Well I have this - Table DimDate- Date Table Employee- Id,Name,Points,Date Now the Employee
I have a table like Employee ================== name salary ================== a 10000 b 20000
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint
Let's say I have the following table: Employee name start_date end_date John 2009-10-10 2009-12-31
i have an employee table with fields like id, name, age and salary. Iam

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.