How does MYSQL set the UUID of a column?

How does MYSQL set the UUID of a column?

1. What is UUID?
UUID is the abbreviation of Universally Unique Identifier. It is a standard for software construction and a part of the Open Software Foundation in the field of distributed computing environments. The purpose is to allow all elements in the distributed system to have unique identification information, without the need for the central control terminal to specify identification information.

2. How to set UUID for mysql database table?
First, create a table


CREATE TABLE `product` (
  `id` varchar(32) DEFAULT NULL COMMENT '主键uuid',
  `productNum` varchar(50) NOT NULL COMMENT '产品编号',
  `productName` varchar(50) DEFAULT NULL COMMENT '产品名称(路线名称)',
  `cityName` varchar(50) DEFAULT NULL COMMENT '出发城市',
  `DepartureTime` timestamp NULL DEFAULT NULL COMMENT '出发时间',
  `productPrice` double DEFAULT NULL COMMENT '产品价格',
  `productDesc` varchar(500) DEFAULT NULL COMMENT '产品描述',
  `productStatus` int(11) DEFAULT NULL COMMENT '状态(0关闭1开启)',
  PRIMARY KEY (`productNum`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Then set the id column to UUID

DELIMITER $$
CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    TRIGGER `ssm`.`product_trigger` -- 触发器名称
    BEFORE INSERT             -- 触发器被触发的时机
    ON `ssm`.`product`       -- 触发器所作用的表名称
    FOR EACH ROW BEGIN
		SET new.id=REPLACE(UUID(),'-',''); -- 触发器执行的逻辑
    END$$

DELIMITER ;

After setting the trigger, insert the statement

INSERT INTO 
product(productNum,productName,cityName,DepartureTime,prodtcuPrice,productDesc,productStatus) 
VALUES('001','钦州三日游','钦州',2020-11-16 22:39:54,1000,'非常不错的旅行',1);

Then you will be automatically assigned a UUID
Insert picture description here

In this way, the UUID is set!

Guess you like

Origin blog.csdn.net/weixin_49092628/article/details/109732092