Update inventory after transaction - mysql

CREATE TABLE IF NOT EXISTS `inventory` (
`id_product` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT,
`model` varchar(45) NOT NULL,
`price_new` FLOAT DEFAULT '0',
sell_price float,
`condition1` varchar(45) NOT NULL,
`launch_date` DATE NOT NULL,
`stock` INT DEFAULT '0',
PRIMARY KEY (`id_product`));
CREATE TABLE IF NOT EXISTS `order` (
`id_order` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT,
`id_customer` INT(6) ZEROFILL NOT NULL,
`subtotal` FLOAT DEFAULT '0',
`discount` float DEFAULT '0',
`tax_rate` float DEFAULT '0.23',
`total` float DEFAULT '0',
`date` DATETIME DEFAULT NOW(),
PRIMARY KEY (`id_order`),
CONSTRAINT `fk_order_1`
FOREIGN KEY (`id_customer`)
REFERENCES `iSAVE`.`customer` (`id_customer`)
ON DELETE RESTRICT
ON UPDATE CASCADE);
CREATE TABLE IF NOT EXISTS `transaction` (
`id_transaction` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_order`INT(4) ZEROFILL NOT NULL,
`status` char(20) default 'Not completed',
FOREIGN KEY (`id_order`) REFERENCES `iSAVE`.`order` (`id_order`)
);
CREATE TABLE IF NOT EXISTS `order_item` (
`id_order_item` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_product` INT(4) ZEROFILL NOT NULL,
`id_order` INT(4) ZEROFILL NOT NULL,
`quantity` INT DEFAULT NULL,
FOREIGN KEY (`id_product`) REFERENCES `iSAVE`.`inventory` (`id_product`),
FOREIGN KEY (`id_order`) REFERENCES `iSAVE`.`order` (`id_order`)
);
I have those tables and my objective is after i update transactions to completed it goes to my inventory table and update the stock. The items that it should update are the items that i choose in order items.
For now i have this peace of code but its not working ...
delimiter $
create trigger update_stock
after update on `transaction`
for each row
begin
if new.`status` = 'Completed' THEN
update inventory
set inventory.stock = inventory.stock - new.quantity
where inventory.id_product = new.id_product;
end if;
End $$
delimiter ;
Edited trigger:
delimiter $$
CREATE TRIGGER trigg1 after update on `transaction` for each row
BEGIN
UPDATE `inventory`
SET stock = (SELECT stock - new.quantity FROM order_item WHERE order_item.id_product = inventory.id_product limit 1);
end $$
delimiter ;

You seem to need an extra join to get the quantity from order_item:
update inventory i join
order_item oi
using (id_product)
set i.stock = i.stock - oi.quantity
where oi.id_order = new.id_order;

Related

Transaction within a stored procedure always failing

I've created a stored procedure via phpmyadmin which carries out a transaction as follows:
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT -1;
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) VALUES (name, gender, house_id, date_from,date_to, clean_rating,comments);
UPDATE rental SET reviewed = 1 WHERE renter_id = renter_id AND house_id = house_id AND date_from = dateFrom AND date_to = dateTo ;
COMMIT;
SELECT 1;
END
The parameters were set like this:
parameters to the stored procedure
Each time I execute the stored procedure with valid values via phpmyadmin, I get -1 returned and no tables are affected. Not quite sure what's wrong here.
Edit: Added some info
SHOW CREATE PROCEDURE sp_save_review:
| sp_save_review | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_save_review`(IN `name` VARCHAR(200), IN `gender` VARCHAR(50), IN `house_id` INT(11), IN `date_from` DATE, IN `date_to` DATE, IN `clean_rating` FLOAT, IN `comments` VARCHAR(1000), IN `renter_id` INT(11))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT -1;
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) VALUES (name, gender, house_id, date_from,date_to, clean_rating,comments);
UPDATE rental SET reviewed = 1 WHERE renter_id = renter_id AND house_id = house_id AND date_from = dateFrom AND date_to = dateTo ;
COMMIT;
SELECT 1;
SHOW CREATE TABLE review:
| review | CREATE TABLE `review` (
`review_id` int(11) NOT NULL AUTO_INCREMENT,
`reviewer_name` varchar(200) CHARACTER SET utf32 NOT NULL,
`reviewer_gender` varchar(50) CHARACTER SET utf32 NOT NULL,
`house_id` int(11) NOT NULL,
`rental_date_from` date NOT NULL,
`rental_date_to` date NOT NULL,
`house_rating` float NOT NULL,
`house_comment` varchar(1000) CHARACTER SET utf32 NOT NULL,
`flagged` tinyint(1) NOT NULL DEFAULT 0,
`banned` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`review_id`),
KEY `house_id` (`house_id`),
CONSTRAINT `review_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `rental` (`house_id`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=latin1 COLLATE=latin1_bin |
SHOW CREATE TABLE rental:
| rental | CREATE TABLE `rental` (
`renter_id` int(11) NOT NULL,
`house_id` int(11) NOT NULL,
`date_from` date NOT NULL,
`date_to` date NOT NULL,
`price` double NOT NULL,
`reviewed` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`renter_id`,`house_id`,`date_from`),
KEY `house_id` (`house_id`),
CONSTRAINT `rental_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `house` (`house_id`),
CONSTRAINT `rental_ibfk_2` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin |
You seem to have got into a fankle with your naming try this
DROP TABLE IF EXISTS REVIEW;
DROP TABLE IF EXISTS RENTAL;
CREATE TABLE `rental` (
`renter_id` int(11) NOT NULL,
`house_id` int(11) NOT NULL,
`date_from` date NOT NULL,
`date_to` date NOT NULL,
`price` double NOT NULL,
`reviewed` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`renter_id`,`house_id`,`date_from`),
KEY `house_id` (`house_id`) #,
#CONSTRAINT `rental_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `house` (`house_id`),
#CONSTRAINT `rental_ibfk_2` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
) ;
CREATE TABLE `review` (
`review_id` int(11) NOT NULL AUTO_INCREMENT,
`reviewer_name` varchar(200) CHARACTER SET utf32 NOT NULL,
`reviewer_gender` varchar(50) CHARACTER SET utf32 NOT NULL,
`house_id` int(11) NOT NULL,
`rental_date_from` date NOT NULL,
`rental_date_to` date NOT NULL,
`house_rating` float NOT NULL,
`house_comment` varchar(1000) CHARACTER SET utf32 NOT NULL,
`flagged` tinyint(1) NOT NULL DEFAULT 0,
`banned` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`review_id`),
KEY `house_id` (`house_id`),
CONSTRAINT `review_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `rental` (`house_id`)
) ;
DROP PROCEDURE IF EXISTS P;
DELIMITER $$
CREATE PROCEDURE P(IN `Pname` VARCHAR(200), IN `Pgender` VARCHAR(50), IN `Phouse_id` INT(11),
IN `Pdate_from` DATE, IN `Pdate_to` DATE, IN `Pclean_rating` FLOAT,
IN `Pcomments` VARCHAR(1000), IN `Prenter_id` INT(11))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT -1;
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment)
VALUES (Pname, Pgender, Phouse_id, Pdate_from,Pdate_to, Pclean_rating,Pcomments);
UPDATE rental
SET reviewed = 1
WHERE renter_id = Prenter_id AND house_id = Phouse_id AND date_from = Pdate_From AND date_to = Pdate_To ;
COMMIT;
SELECT 1;
END $$
DELIMITER ;
INSERT INTO RENTAL( `renter_id`, `house_id` , `date_from` , `date_to` , `price`, `reviewed` )
VALUES (1,1,'2022-01-01','2022-01-01',10,0);
CALL P('AAA','F',1,'2022-01-01','2022-01-01',1,'BBB',1);

Create a table in a trigger and compare it

i have 4 tables and i want to create a trigger that it declares a table and store it all in that table so it can be compared latter.
CREATE TABLE IF NOT EXISTS `inventory` (
`id_product` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT,
`model` varchar(45) NOT NULL,
`price_new` FLOAT DEFAULT '0',
`launch_date` DATE NOT NULL,
`stock` INT DEFAULT '0',
PRIMARY KEY (`id_product`));
CREATE TABLE IF NOT EXISTS `order` (
`id_order` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT,
`id_customer` INT(6) ZEROFILL NOT NULL,
`subtotal` FLOAT DEFAULT '0',
`discount` float DEFAULT '0',
`tax_rate` float DEFAULT '0.23',
`total` float DEFAULT '0',
`date` DATETIME DEFAULT NOW(),
PRIMARY KEY (`id_order`),
CONSTRAINT `fk_order_1`
FOREIGN KEY (`id_customer`)
REFERENCES `iSAVE`.`customer` (`id_customer`)
ON DELETE RESTRICT
ON UPDATE CASCADE);
CREATE TABLE IF NOT EXISTS `order_item` (
`id_order_item` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_product` INT(4) ZEROFILL NOT NULL,
`id_order` INT(4) ZEROFILL NOT NULL,
`quantity` INT DEFAULT NULL,
FOREIGN KEY (`id_product`) REFERENCES `iSAVE`.`inventory` (`id_product`),
FOREIGN KEY (`id_order`) REFERENCES `iSAVE`.`order` (`id_order`)
);
CREATE TABLE IF NOT EXISTS `transaction` (
`id_transaction` INT(4) ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_order` INT(4) ZEROFILL NOT NULL,
`status` char(20) default 'Not completed',
FOREIGN KEY (`id_order`) REFERENCES `iSAVE`.`order` (`id_order`)
);
In here are the tables that im using. But now i have a trigger that after insert on orders it inserts on transactions. My objective now its to create a trigger that generates a table that takes order and order item and store id_product id_order and quantity so it can be used to after update on transaction the stock is changed on the respective items on tabel inventory.
This is the trigger that i have so far but i cant use it because i dont rly know how to declare a table in mysql:
delimiter $$
CREATE TRIGGER trigg1 after update on `transaction` for each row
BEGIN
declare #item TABLE(
id_order int,
id_product int,
quantity int);
update inventory i join #item it using (id_product)
set i.stock = i.stock - it.quantity
where it.id_order = new.id_order;
end $$
delimiter ;
You can't you have to use a temporaRY TABLE
delimiter $$
CREATE TRIGGER trigg1 after update on `transaction` for each row
BEGIN
CREATE TEMPORARY TABLE tbl1(
id_order int,
id_product int,
quantity int);
update inventory i join tbl1 it using (id_product)
set i.stock = i.stock - it.quantity
where it.id_order = new.id_order;
DROP TEMPORARY TABLE tbl1;
end $$
delimiter ;
But usually you make a temporaray atble with a select
CREATE TEMPORARY TABLE tbl1
SELECT id_order ,
id_product ,
quantity
FROM ORDERS

Error Code: 1109. Unknown table 'evrz.account' in field list

I am trying to create a trigger, but I get this error:
Error Code: 1109. Unknown table 'evrz.account' in field list
I tried to execute this:
INSERT INTO `record` (`record`.account_id) VALUES (289688082)
This is my trigger:
CREATE DEFINER=`root`#`localhost` TRIGGER `evrz`.`record_BEFORE_INSERT` BEFORE INSERT ON `record` FOR EACH ROW
BEGIN
IF `evrz`.`account`.`status` ='OUT' in (
SELECT `evrz`.`account`.`status`
FROM `evrz`.`account`
WHERE (account_id = NEW.account_id)
)THEN
UPDATE `evrz`.`account` SET `evrz`.`account`.`status` = 'IN' WHERE (`evrz`.`account`.`account_id` = NEW.account_id);
END IF;
IF `evrz`.`account`.`status` ='IN' in (
SELECT `evrz`.`account`.`status`
FROM `evrz`.`account`
WHERE (account_id = NEW.account_id)
)THEN
UPDATE `evrz`.`account` SET `status` = 'OUT' WHERE (`evrz`.`account`.`account_id` = NEW.account_id);
END IF;
END
These are my tables:
CREATE TABLE `employee` (
`employee_id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`lastname` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone` varchar(45) NOT NULL,
`mail` varchar(45) NOT NULL,
`created_at` TIMESTAMP(0) NOT NULL DEFAULT 'CURRENT_TIMESTAMP()',
PRIMARY KEY (`employee_id`)
);
CREATE TABLE `account` (
`account_id` int(10) NOT NULL AUTO_INCREMENT,
`status` enum('IN','OUT') NOT NULL DEFAULT ''OUT'',
`employee_id` int(10) NOT NULL UNIQUE,
PRIMARY KEY (`account_id`)
);
CREATE TABLE `record` (
`record_id` int(10) NOT NULL AUTO_INCREMENT,
`account_id` int(10) NOT NULL,
`creted_at` TIMESTAMP(0) NOT NULL DEFAULT 'CURRENT_TIMESTAMP()',
PRIMARY KEY (`record_id`)
);
ALTER TABLE `account` ADD CONSTRAINT `account_fk0` FOREIGN KEY (`employee_id`) REFERENCES `employee`(`employee_id`);
ALTER TABLE `record` ADD CONSTRAINT `record_fk0` FOREIGN KEY (`account_id`) REFERENCES `account`(`account_id`);
I am trying to make that when an employee logs in to a database, his stsus will be changed.
The IF statement can only refer to the row that's being inserted, using NEW.columnName to refer to a column in the new row.
When you're updating another table, use the IF function (or a CASE expression) in the value, and it can refer to the existing value in the row being updated.
CREATE DEFINER=`root`#`localhost` TRIGGER `evrz`.`record_BEFORE_INSERT` BEFORE INSERT ON `record` FOR EACH ROW
BEGIN
UPDATE evrz.account
SET status = IF(status = 'OUT', 'IN', 'OUT')
WHERE account_id = NEW.account_id;
END

how to create a trigger when table transaction data is inserted that will reduce column quantity of table items

I am new to triggers but want to know how I can use one in my database. I have created a table for a transactions, and items. When transaction is carried out the items table should be affected like this,
if transaction_type "outgoing" is selected then trigger should
reduce the value of column 'quantity' in table items by the value
in field quantity from table transaction.
if transaction_type "incoming" is selected then trigger should increase the value of column 'quantity' in table items by the
value in field quantity from table transaction.
here are the table
1. table items
`CREATE TABLE `items` (
`item_id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`purchase_date` varchar(100) NOT NULL,
`category_id` int(10) NOT NULL,
`store_id` int(10) NOT NULL,
`supplier_id` int(10) NOT NULL,
`batch_id` int(10) DEFAULT NULL,
`quantity` bigint(11) NOT NULL,
PRIMARY KEY (`item_id`),
KEY `category` (`category_id`),
KEY `store` (`store_id`),
KEY `supplier` (`supplier_id`),
KEY `batch` (`batch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8`
2. table transactions
CREATE TABLE `transactions` (
`transaction_id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`transaction_date` varchar(100) NOT NULL,
`item_id` int(10) NOT NULL,
`batch_id` int(10) NOT NULL,
`store_id` int(10) NOT NULL,
`transaction_type` varchar(40) NOT NULL,
`remarks` longtext NOT NULL,
`quantity` bigint(11) NOT NULL,
PRIMARY KEY (`transaction_id`),
KEY `item` (`item_id`),
KEY `batch` (`batch_id`),
KEY `store` (`store_id`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8
This is what I have tried but when I run a transaction instead of reducing or adding the column quantity in table items it returns 0:
CREATE TRIGGER subtract_quantity AFTER INSERT ON transactions
FOR EACH ROW UPDATE items
SET items.quantity = items.quantity - transactions.quantity=transactions.quantity
WHERE items.item_id = NEW.item_id
this is what worked for me
CREATE TRIGGER `subtract_quantity` AFTER INSERT ON `transactions`
FOR EACH ROW BEGIN
UPDATE items
SET items.quantity = GREATEST(0, items.quantity - NEW.quantity)
WHERE items.item_id = NEW.item_id
and items.quantity > 0;
UPDATE batches
SET batches.balance = GREATEST(0, batches.balance - NEW.quantity)
WHERE batches.batch_id = NEW.batch_id
and batches.balance > 0;
END

Mysql statement insert, if inserted, insert another

I have the following statement:
INSERT INTO `Properties`(`Id`, `Url`, `BrokerId`, `LastFound`) VALUES
(#Id,#Url,#BrokerId,#LastFound)
ON DUPLICATE KEY UPDATE LastFoundOn = #LastFoundOn;
INSERT INTO `Events`(`Id`, `Type`, `DateTime`, `PropertyId`) VALUES
(#EventId,#EventType,#Now,#Id);
There is a foreign key constraint between Properties.Id and Events.PropertyId. And the Url is unique.
This works - almost. When a recod is not inserted, but updated because of duplicate key (Url), then the insert into event will fail, because the foreign key simply doesn't exist. Like this:
Eg:
exists: 1 | http://test1.com | 2 | 2013-03-13
to insert: 2 | http://test2.com | 2 | 2013-03-14
When trying to insert, it updates instead, because of the unique url. When afterwards trying to insert the event, a foreign key (2) doesn't exist in the Properties table. How can I make an if then statement to handle this scenario?
Something like (?):
INSERT INTO `Properties`(`Id`, `Url`, `BrokerId`, `LastFound`) VALUES
(#Id,#Url,#BrokerId,#LastFound)
ON DUPLICATE KEY UPDATE LastFoundOn = #LastFoundOn;
IF LastInserted = #Id THEN
INSERT INTO `Events`(`Id`, `Type`, `DateTime`, `PropertyId`) VALUES
(#EventId,#EventType,#Now,#Id);
END IF;
UPDATE:
A trigger might be the solution, but I'm struggeling making it work. What's wrong here?
DELIMITER $$
CREATE TRIGGER Event_Submitted_Trigger AFTER INSERT ON Properties
FOR EACH ROW
BEGIN
INSERT INTO Events VALUES(SELECT(UUID()), 'PropertySubmitted', SELECT(NOW()), new.Id);
END$$
I get the following error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT(NOW()), new.Id); END$$' at line 4
Best regards,
Søren
UPDATE:
Here is my schema:
CREATE TABLE IF NOT EXISTS `Events` (
`Id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Type` enum('PropertySubmitted','PropertyChanged','PropertyRemoved') NOT NULL,
`DateTime` datetime NOT NULL,
`Attribute` varchar(128) NOT NULL,
`From` varchar(512) NOT NULL,
`To` varchar(512) NOT NULL,
`PropertyId` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`Id`),
KEY `IX_FK_PropertyEvent` (`PropertyId`),
KEY `DateTimeIndex` (`DateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `Properties`
--
CREATE TABLE IF NOT EXISTS `Properties` (
`Id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Url` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Type` varchar(64) NOT NULL,
`ExtractedAddress` varchar(192) NOT NULL,
`ExtractedPostcode` varchar(8) NOT NULL,
`ExtractedCity` varchar(64) NOT NULL,
`StreetName` varchar(128) DEFAULT NULL,
`StreetNumber` varchar(8) DEFAULT NULL,
`Floor` varchar(8) DEFAULT NULL,
`Side` varchar(8) DEFAULT NULL,
`DoorNo` varchar(8) DEFAULT NULL,
`Postcode` int(4) DEFAULT NULL,
`City` varchar(64) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`ImageUrl` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`Price` int(8) NOT NULL,
`Payout` int(8) NOT NULL,
`GrossPrice` int(6) NOT NULL,
`NetPrice` int(6) NOT NULL,
`Area` int(5) NOT NULL,
`GroundArea` int(5) NOT NULL,
`Rooms` int(2) NOT NULL,
`Year` int(4) NOT NULL,
`PriceChange` int(11) NOT NULL,
`FirstFoundOn` datetime NOT NULL,
`SubmittedOn` datetime NOT NULL,
`LastFoundOn` datetime NOT NULL,
`FoundAt` varchar(256) DEFAULT NULL,
`Validated` tinyint(1) NOT NULL,
`BrokerId` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Archived` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
UNIQUE KEY `Url` (`Url`),
KEY `IX_FK_PropertyBroker` (`BrokerId`),
KEY `UrlIndex` (`Url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Triggers `Properties`
--
DROP TRIGGER IF EXISTS `Event_Submitted_Trigger`;
DELIMITER //
CREATE TRIGGER `Event_Submitted_Trigger` AFTER INSERT ON `Properties`
FOR EACH ROW BEGIN
INSERT INTO `Events` VALUES(UUID(), 'PropertySubmitted', NOW(), NEW.Id);
END
//
DELIMITER ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `Events`
--
ALTER TABLE `Events`
ADD CONSTRAINT `Events_ibfk_1` FOREIGN KEY (`PropertyId`) REFERENCES `Properties` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `Properties`
--
ALTER TABLE `Properties`
ADD CONSTRAINT `Properties_ibfk_2` FOREIGN KEY (`BrokerId`) REFERENCES `Brokers` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
Assuming the following structure:
CREATE TABLE Properties (
id INT,
url VARCHAR(100),
lastFound DATETIME,
UNIQUE (url)
) ;
CREATE TABLE Events (
id VARCHAR(36),
type VARCHAR(20),
t DATETIME,
propertyId INT
) ;
Here is a working trigger:
DELIMITER $$
CREATE TRIGGER Event_Submitted_Trigger AFTER INSERT ON Properties
FOR EACH ROW BEGIN
INSERT INTO Events VALUES( UUID(), 'PropertySubmitted', NOW(), new.Id);
END $$
DELIMITER ;
See it in action here. Notice the NOW()+SLEEP(1) hack, only meant to delay execution in order to get a significant result (SLEEP() returns 0 if not interrupted).