How to automatically increase the primary key of a MySQL table?

CREATE TABLE IF NOT EXISTS `mydb`.`Info_instrumental` (
  `id_instrument` INT NOT NULL COMMENT '',
  `instrument` VARCHAR(45) NOT NULL COMMENT '',
  `exp_ins` VARCHAR(45) NULL COMMENT '',
  `User_name_user` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`id_instrument`, `User_name_user` `)  COMMENT '',
  INDEX `fk_Info_instrumental_User1_idx` (`User_name_user` ` ASC)  COMMENT '',
  CONSTRAINT `fk_Info_instrumental_User1`
    FOREIGN KEY (`User_name_user`)
    REFERENCES `mydb`.`Usuario` (`name_user`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

I want to increase my id field, for example, '001', '002', '003' .... How can I do this with a trigger?

I need something like this:

| id_instrument | instrument| exp_ins | User_name_user
------------------------------------------------------
|      001     | guitar    | 2 years |   Marcus
|      002     | piano     | 6 months|   Leo
+4
source share
1 answer

You can use the keyword zerofillin your create table to see the result you are looking for. Here is an example:

mysql> create table info_instrumental (
     >     id_instrument int(3) zerofill not null primary key auto_increment
     > );

mysql> insert into info_instrumental values (1), (2), (3);

mysql> select * from info_instrumental;
+---------------+
| id_instrument |
+---------------+
|           001 |
|           002 |
|           003 |
+---------------+

You do not need a trigger to get this result.

+2
source

Source: https://habr.com/ru/post/1614536/


All Articles