MYSQL Table creation with default value(expression) to a column

前端 未结 2 1464
忘掉有多难
忘掉有多难 2021-01-03 08:13

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

相关标签:
2条回答
  • 2021-01-03 09:09

    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 讨论(0)
  • 2021-01-03 09:14

    You could try concatenating it in your select statement instead of storing an auto increment column and an id column,

    SELECT CONCAT(id, substring(name,4) FROM tbl_employee
    

    That way you wouldn't need triggers

    0 讨论(0)
提交回复
热议问题