2

我有以下声明:

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

Properties.Id 和 Events.PropertyId 之间存在外键约束。并且 URL 是独一无二的。

这有效 - 几乎。如果没有插入记录,但由于重复键(Url)而更新,则插入事件将失败,因为外键根本不存在。像这样:

例如:

exists:    1  |  http://test1.com  |  2  |  2013-03-13
to insert: 2  |  http://test2.com  |  2  |  2013-03-14

当尝试插入时,它会更新,因为唯一的 url。之后尝试插入事件时,属性表中不存在外键 (2)。如何制作 if then 语句来处理这种情况?

就像是 (?):

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;

更新:

触发器可能是解决方案,但我正在努力让它发挥作用。这里有什么问题?

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$$

我收到以下错误:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'SELECT(NOW()), new.Id); 附近使用的正确语法;END$$' 在第 4 行

最好的祝福,

索伦

更新:

这是我的架构:

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;
4

1 回答 1

0

假设以下结构:

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

这是一个工作触发器:

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 ;

在这里查看它的实际应用。注意NOW()+SLEEP(1)hack,只是为了获得显着的结果而延迟执行(SLEEP()如果没有被中断,则返回 0)。

于 2013-03-19T15:38:47.693 回答