How to generate/autoincrement guid on insert without triggers and manual inserts in mysql?

前端 未结 2 1330
花落未央
花落未央 2021-01-15 00:59

Im revisiting my database and noticed I had some primary keys that were of type INT.

This wasn\'t unique enough so I thought I would have a guid. I come from a mic

2条回答
  •  不要未来只要你来
    2021-01-15 01:48

    As far as stated in the documentation, you can use uid() as a column default starting version 8.0.13, so something like this should work:

    create table tbl_test (
        guid binary(16) default (uuid_to_bin(uuid())) not null primary key,
        name varchar(50) not null
    );
    

    This is pretty much copied from the documentation. I don't have a recent enough version of MySQL at hand to test this.

提交回复
热议问题