主键 UUID 触发器
Primary key UUID Trigger
我正在尝试创建一个 table,其中主键是每次出现 INSERT 事件时自动生成的 UUID。但问题是当我尝试执行 INSERT 查询时跳过主键列,如下所示:
INSERT INTO `tablename` (`reference`) VALUES ('hey');
没有插入行,也没有错误。只是“插入 0 行...”
这是我的 table 和触发器。谢谢。
CREATE TABLE `tablename` (
`uuid` char(36) NULL,
`reference` varchar(100) NOT NULL,
PRIMARY KEY (uuid)
);
DELIMITER ;;
CREATE TRIGGER before_insert_tablename
BEFORE INSERT ON tablename
FOR EACH ROW
BEGIN
IF new.uuid IS NULL THEN
SET new.uuid = uuid();
END IF;
END
;;
DELIMITER ;
你不能将Primary key
设为NULL
改成NOT NULL
就可以了
`uuid` char(36) NOT NULL,
The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values.
我正在尝试创建一个 table,其中主键是每次出现 INSERT 事件时自动生成的 UUID。但问题是当我尝试执行 INSERT 查询时跳过主键列,如下所示:
INSERT INTO `tablename` (`reference`) VALUES ('hey');
没有插入行,也没有错误。只是“插入 0 行...” 这是我的 table 和触发器。谢谢。
CREATE TABLE `tablename` (
`uuid` char(36) NULL,
`reference` varchar(100) NOT NULL,
PRIMARY KEY (uuid)
);
DELIMITER ;;
CREATE TRIGGER before_insert_tablename
BEFORE INSERT ON tablename
FOR EACH ROW
BEGIN
IF new.uuid IS NULL THEN
SET new.uuid = uuid();
END IF;
END
;;
DELIMITER ;
你不能将Primary key
设为NULL
改成NOT NULL
`uuid` char(36) NOT NULL,
The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values.