知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
DROP TABLE IF EXISTS `uuid_test`; CREATE TABLE IF NOT EXISTS `uuid_test` ( `uuid` varchar(36) NOT NULL, `name` varchar(20) NOT NULL, PRIMARY KEY (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='uuid 測試';
插入觸發器
DROP TRIGGER IF EXISTS `uuid_test_insert`; SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE=''; DELIMITER // CREATE TRIGGER `uuid_test_insert` BEFORE INSERT ON `uuid_test` FOR EACH ROW BEGIN IF new.uuid is null or new.uuid = '' or length(new.uuid) != 36 THEN set new.uuid=uuid(); END IF; END// DELIMITER ; SET SQL_MODE=@OLDTMP_SQL_MODE;
ID放撰改觸發器
DROP TRIGGER IF EXISTS `uuid_test_update`; SET @OLDTMP_SQL_MODE=@@SQL_MODE, SQL_MODE=''; DELIMITER // CREATE TRIGGER `uuid_test_update` BEFORE UPDATE ON `uuid_test` FOR EACH ROW BEGIN set new.uuid = old.uuid; END// DELIMITER ; SET SQL_MODE=@OLDTMP_SQL_MODE;