CREATE TABLE `product` (
`id` VARCHAR(36) NOT NULL,
`productNum` VARCHAR(50) NOT NULL,
`productName` VARCHAR(50) DEFAULT NULL,
`cityName` VARCHAR(50) DEFAULT NULL,
`DepartureTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`productPrice` DOUBLE DEFAULT NULL,
`productDesc` VARCHAR(500) DEFAULT NULL,
`productStatus` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `product` (`id`,`productNum`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
/*删除触发器*/
DROP TRIGGER IF EXISTS `product_before_insert`;
DELIMITER //
CREATE TRIGGER `product_before_insert`
BEFORE INSERT ON `product` FOR EACH ROW
BEGIN
IF new.id IS NULL THEN
SET new.id = UUID();
END IF;
END;
//
INSERT INTO PRODUCT (productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
VALUES ('itcast-002', '北京三日游', '北京', NOW(), 1200, '不错的旅行', 1);
插入的语句中,id值为空,数据库立马会报错,那你会说,那我给id设置一个不就好了,可是!!!使用uuid就是为了让系统生成一个唯一的机器识别码,你自己写一个那我直接用auto_increment来自增不就好了吗。SELECT * FROM product
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` VARCHAR(36) NOT NULL,
`productNum` VARCHAR(50) NOT NULL,
`productName` VARCHAR(50) DEFAULT NULL,
`cityName` VARCHAR(50) DEFAULT NULL,
`DepartureTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`productPrice` DOUBLE DEFAULT NULL,
`productDesc` VARCHAR(500) DEFAULT NULL,
`productStatus` INT(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `product` (`id`,`productNum`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
/*删除触发器*/
DROP TRIGGER IF EXISTS `product_before_insert`;
DELIMITER //
CREATE TRIGGER `product_before_insert`
BEFORE INSERT ON `product` FOR EACH ROW
BEGIN
IF new.productDesc IS NOT NULL THEN
SET new.id = UUID();
END IF;
END;
//
INSERT INTO PRODUCT (productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
VALUES ('itcast-002', '北京三日游', '北京', NOW(), 1200, '不错的旅行', 1);
INSERT INTO PRODUCT (productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
VALUES ('itcast-003', '上海五日游', '上海', NOW(), 1800, '魔都我来了', 0);
INSERT INTO PRODUCT (productnum, productname, cityname, departuretime, productprice, productdesc, productstatus)
VALUES ('itcast-001', '北京三日游', '北京', NOW(), 1200, '不错的旅行', 1);
SELECT * FROM `traveller`;
CREATE TABLE traveller(
id VARCHAR(32) PRIMARY KEY,
NAME VARCHAR(20),
sex VARCHAR(20),
phoneNum VARCHAR(20),
credentialsType INT,
credentialsNum VARCHAR(50),
travellerType INT
)
/*删除触发器*/
DROP TRIGGER IF EXISTS `traveller_before_insert`;
DELIMITER //
CREATE TRIGGER `traveller_before_insert`
BEFORE INSERT ON `traveller` FOR EACH ROW
BEGIN
IF new.credentialsNum IS NOT NULL THEN
SET new.id = UUID();
END IF;
END;
//
INSERT INTO TRAVELLER (NAME, sex, phonenum, credentialstype, credentialsnum, travellertype)
VALUES ('张龙', '男', '13333333333', 0, '123456789009876543', 0);
INSERT INTO TRAVELLER (NAME, sex, phonenum, credentialstype, credentialsnum, travellertype)
VALUES ('张小龙', '男', '15555555555', 0, '987654321123456789', 1);
SELECT * FROM `member`;
CREATE TABLE member(
id VARCHAR(32) PRIMARY KEY,
NAME VARCHAR(20),
nickname VARCHAR(20),
phoneNum VARCHAR(20),
email VARCHAR(20)
)
/*删除触发器*/
DROP TRIGGER IF EXISTS `member_before_insert`;
DELIMITER //
CREATE TRIGGER `member_before_insert`
BEFORE INSERT ON `member` FOR EACH ROW
BEGIN
IF new.phoneNum IS NOT NULL THEN
SET new.id = UUID();
END IF;
END;
//
INSERT INTO MEMBER (NAME, nickname, phonenum, email)
VALUES ('张三', '小三', '18888888888', '[email protected]');
DROP TABLE IF EXISTS `orders`;
SELECT * FROM `orders`;
CREATE TABLE orders(
id VARCHAR(32) PRIMARY KEY,
orderNum VARCHAR(20) NOT NULL UNIQUE,
orderTime TIMESTAMP,
peopleCount INT,
orderDesc VARCHAR(500),
payType INT,
orderStatus INT,
productId VARCHAR(32),
memberId VARCHAR(32),
FOREIGN KEY (productId) REFERENCES product(id),
FOREIGN KEY (memberId) REFERENCES member(id)
)
/*删除触发器*/
DROP TRIGGER IF EXISTS `orders_before_insert`;
DELIMITER //
CREATE TRIGGER `orders_before_insert`
BEFORE INSERT ON `orders` FOR EACH ROW
BEGIN
IF new.orderDesc IS NOT NULL THEN
SET new.id = UUID();
END IF;
END;
//
INSERT INTO ORDERS (ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('12345', NOW(), 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('54321', NOW(), 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('67890', NOW(), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
INSERT INTO ORDERS (ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
VALUES ('98765', NOW(), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
完成。