Mysql statement insert, if inserted, insert another - mysql

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).

Related

mysql - check before creating

I am creating tables in mysql 5.7 version, such as but would like to check if table exists before creating. or another scenario have some try catch block , where if table exits and i execute create again, it throws an error . i can catch that and log it.
CREATE TABLE `address` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`CreateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UpdateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`UserId` int(11) DEFAULT NULL,
`Address1` varchar(100) CHARACTER SET utf8 NOT NULL,
`Address2` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`City` varchar(50) CHARACTER SET utf8 NOT NULL,
`State` int(11) NOT NULL,
`Zip` varchar(9) NOT NULL,
PRIMARY KEY (`Id`),
KEY `fk_address_user_UserId_idx` (`UserId`),
CONSTRAINT `fk_address_user_UserId` FOREIGN KEY (`UserId`) REFERENCES `user` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
If you can dynamize schema and table names I've written a udf once to be used in a stored procedure like:
IF fn_table_exists('my_schema', 'table_to_be_created') THEN
SELECT 'Table Already exists!';
END IF;
Implementation:
DELIMITER $$
CREATE FUNCTION fn_table_exists(dbName VARCHAR(255), tableName VARCHAR(255)) RETURNS TINYINT(1)
BEGIN
DECLARE totalTablesCount INT DEFAULT (
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA COLLATE utf8_general_ci = dbName COLLATE utf8_general_ci)
AND (TABLE_NAME COLLATE utf8_general_ci = tableName COLLATE utf8_general_ci)
);
RETURN IF(
totalTablesCount > 0,
TRUE,
FALSE
);
END$$
DELIMITER ;

Optimize MySQL trigger performance

I have following table structure. Added two triggers, but as the triggers work with string values and they search for string the database performance might degrade in future when every table will get huge. I have no strong experience with indexes, and don't know which fields to index to make trigger's search operation fast even with millions of rows.
What do you suggest?
CREATE TABLE `ofRoster` (
`rosterID` bigint(20) NOT NULL,
`username` varchar(64) NOT NULL,
`jid` varchar(1024) NOT NULL,
`sub` tinyint(4) NOT NULL,
`ask` tinyint(4) NOT NULL,
`recv` tinyint(4) NOT NULL,
`nick` varchar(255) DEFAULT NULL,
PRIMARY KEY (`rosterID`),
KEY `ofRoster_unameid_idx` (`username`),
KEY `ofRoster_jid_idx` (`jid`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `ofUser` (
`username` varchar(64) NOT NULL,
`plainPassword` varchar(32) DEFAULT NULL,
`encryptedPassword` varchar(255) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`creationDate` char(15) NOT NULL,
`modificationDate` char(15) NOT NULL,
PRIMARY KEY (`username`),
KEY `ofUser_cDate_idx` (`creationDate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
delimiter ;;
CREATE TRIGGER `UpdateNickOnInsert` BEFORE INSERT ON `ofRoster` FOR EACH ROW BEGIN
SET NEW.nick=(SELECT name FROM ofUser where username=NEW.username);
END
;;
delimiter ;
delimiter ;;
CREATE TRIGGER `UpdateRosterNicksOnUpdate` AFTER UPDATE ON `ofUser` FOR EACH ROW BEGIN
IF NEW.name <> OLD.name
THEN
UPDATE ofRoster r SET r.nick=NEW.name WHERE r.username=OLD.username;
END IF;
END
;;
delimiter ;

Unknown column 'dob' in 'field list'

I am getting error Unknown column 'dob' in 'field list'
I know that dob is in my table and have called it many times and in my code the insert is ordered correctly. I am just learning MySQL so I'm completely lost
insert into users (userid, firstname, username,dob)
values ('gg', 'greg', 'greg2', '1980-01-01');
here is the table I am trying to insert it into.
CREATE TABLE `users` (
`userid` varchar(50) NOT NULL DEFAULT '',
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`middleName` varchar(100) DEFAULT NULL,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`occupationId` int(11) DEFAULT NULL,
`userStatusId` int(11) DEFAULT NULL,
`userTypeId` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `username` (`username`),
KEY `userStatusId` (`userStatusId`),
KEY `userTypeId` (`userTypeId`),
KEY `occupationId` (`occupationId`),
KEY `lastname` (`lastname`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`userStatusId`) REFERENCES `userStatus` (`userStatusId`),
CONSTRAINT `users_ibfk_2` FOREIGN KEY (`userTypeId`) REFERENCES `userType` (`userTypeId`),
CONSTRAINT `users_ibfk_3` FOREIGN KEY (`occupationId`) REFERENCES `occupation` (`occupationId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I am doing all my work in Sequel Pro
insert into users (userid, firstname, username, dob)
values ('gg', 'greg', 'greg2', '1980-01-01');
i tried it this was as well and get the same error, then i tried it like this
insert into users (userid,firstname,username,dob)
values ('gg','greg','greg2','1980-01-01');
here is the code I ran before my insert
delimiter $$
create trigger usersInsert
before insert on users
for each row begin
set new.created = now();
set new.age = floor(datediff(now(),dob)/365);
end $$
delimiter ;
I was trying to see if my trigger was working so I inserted a new user into the user table to make sure it was working properly
i found where my error was. it was in my trigger
delimiter $$
create trigger usersInsert
before insert on users
for each row begin
set new.created = now();
set new.age = floor(datediff(now(),new.dob)/365);
end $$
delimiter ;
I wasn't using 'new.dob' when setting a value to dob
Thank You for all of your help!

Create trigger on insert

I have the following table:
CREATE TABLE `loteria_loterias` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fecha_ini` datetime NOT NULL,
`fecha_fin` datetime DEFAULT NULL,
`ganador` varchar(60) CHARACTER SET utf8 DEFAULT NULL,
`coste` int(11) NOT NULL,
`premium` int(11) NOT NULL,
`duracion` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `ganador` (`ganador`),
CONSTRAINT `loteria_loterias_ibfk_1` FOREIGN KEY (`ganador`) REFERENCES `tegm_users` (`user_login`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1
Is it posible to create a trigger that fill in 'fecha_fin' with 'fecha_ini + duracion' (duracion is in hours) in every insert?
delimiter |
CREATE TRIGGER `some_name` BEFORE INSERT ON loteria_loterias
FOR EACH ROW BEGIN
SET NEW.fecha_fin = NEW.fecha_ini + interval NEW.duracion hour;
END;
|
delimiter ;

MySql - can BEFORE INSERT TRIGGER insert into 2 columns?

Can this trigger be changed so that the sortorder table gets 2 column values (sortOrderId, sortOrder) inserted?
How is the value of sortOrder found?
If it is known and can be inserted into image table then can it also be inserted into the sortorder table?
-- Trigger DDL Statements
DELIMITER $$
USE `nextcart`$$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `nextcart`.`insert_sortorderid`
BEFORE INSERT ON `nextcart`.`image`
FOR EACH ROW
BEGIN
INSERT INTO sortorder SET sortOrderId = NULL, sortOrder = NEW.sortOrder;
SET NEW.sortOrderId = (SELECT LAST_INSERT_ID());
END;
$$
CREATE TABLE sortorder:
delimiter $$
CREATE TABLE `sortorder` (
`sortOrderId` int(11) NOT NULL AUTO_INCREMENT,
`sortOrder` tinyint(4) NOT NULL,
PRIMARY KEY (`sortOrderId`),
KEY `sort_order` (`sortOrderId`,`sortOrder`),
CONSTRAINT `fk_sortOrderId` FOREIGN KEY (`sortOrderId`) REFERENCES `image` (`imageId`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8$$
CREATE TABLE image:
delimiter $$
CREATE TABLE `image` (
`imageId` int(11) NOT NULL AUTO_INCREMENT,
`imageFileName` varchar(45) DEFAULT NULL,
`imagePath` varchar(255) DEFAULT NULL,
`imageTitle` varchar(100) DEFAULT NULL,
`imageAlt` varchar(100) DEFAULT NULL,
`imageWidth` int(11) DEFAULT NULL,
`imageHeight` int(11) DEFAULT NULL,
`classId` int(11) DEFAULT NULL,
`imageSizeId` tinyint(4) NOT NULL,
`isImageEnabled` bit(1) DEFAULT b'0',
`sortOrderId` int(11) DEFAULT NULL,
PRIMARY KEY (`imageId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8$$
ERROR MESSAGE:
Error 1054: Unknown column 'sortOrder' in 'NEW' SQL Statement:
CREATE TRIGGER insert_sortorderid BEFORE INSERT ON image FOR EACH
ROW BEGIN INSERT INTO nextcart.sortorder SET sortOrderId = NULL,
sortOrder = NEW.sortOrder; SET NEW.sortOrderId = ( SELECT
LAST_INSERT_ID()); END; Error when running failback script. Details
follow. Error 1050: Table 'image' already exists SQL Statement: CREATE
TABLE image ( imageId int(11) NOT NULL AUTO_INCREMENT,
imageFileName varchar(45) DEFAULT NULL, imagePath varchar(255)
DEFAULT NULL, imageTitle varchar(100) DEFAULT NULL, imageAlt
varchar(100) DEFAULT NULL, imageWidth int(11) DEFAULT NULL,
imageHeight int(11) DEFAULT NULL, classId int(11) DEFAULT NULL,
imageSizeId tinyint(4) NOT NULL, isImageEnabled bit(1) DEFAULT
b'0', sortOrderId int(11) DEFAULT NULL, PRIMARY KEY (imageId)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
There is no column named sortOrder in the image table.
So, the reference to NEW.sortOrder (on the insert statement in the trigger) is invalid.
To answer your first question: No. Since there is no value supplied for that in the INSERT statement (which fires the BEFORE INSERT TRIGGER), you don't really have a source for that value.
The easy option is to provide a default value for it.
If you want to supply a value for the sortOrder column, then one option is to add a sortOrder column to the image table, and then the value can be supplied in the INSERT INTO image statement. Then it would available in the trigger.
(The purpose of the sortorder table is not at all clear.)