2011-12-09 30 views
1

有谁知道是否有可能在MySQL中实现行约束?如何实现mysql行约束

可以说我有一个非常简单的表保持促销代码的网上商店:

CREATE TABLE `promotioncodes` (
    `promotionid` int(11) NOT NULL AUTO_INCREMENT, 
    `promotioncode` varchar(30) DEFAULT NULL, 
    `partnerid` int(11) DEFAULT NULL, 
    `value` double DEFAULT NULL, 
    `nr_of_usage` int(11) NOT NULL DEFAULT '0', 
    `valid_from` datetime DEFAULT '0000-00-00 00:00:00', 
    `valid_through` datetime DEFAULT '0000-00-00 00:00:00', 
    PRIMARY KEY (`promotionid`), 
    UNIQUE KEY `promotioncode_unique` (`promotioncode`) 
) 

有没有什么办法确保,当行被插入或更新:

1 )'valid_from'日期总是'较小'(以日期表示),然后是'valid_through'。 2)如果'valid_from'发生空白/ null'valid_through'也必须为空/空。 3)如果'valid_through'恰好留空/ null'valid_from'也必须为空/空。

我收集了一些关于触发器和存储过程的东西,但我没有这些解决方案的感觉:/?如果他们是解决方案,那么请给我一个具体的例子来说明如何实现这个

+0

存储过程可能是可能的,但我无法承诺。对于其他更强大的数据库,MySQL并不是那么热门。 – GordonM

回答

1

你应该创建存储过程来插入数据。在这个存储过程中,你可以实现你需要的任何规则。

这里是例子。

create procedure promotioncodes_insert(
    IN... list of params 
    OUT error_code 
) 
exec:begin 
    set error_code = 0; -- everything is ok 

    if valid_from > valid_thru then 
    set error_code = -1; -- error 
    leave exec; 
    end if; 

    if (valid_from is null) and (valid_thru is not null) then 
    set error_code = -2; -- another kind of error 
    leave exec; 
    end if; 

    -- and so on 

    -- doing insert 

end; 

请注意,如果您将直接插入,如insert into promocodes() values()这些约束将不起作用。

+0

好的谢谢你的回复。但是,如果我理解正确,这是唯一可能通过存储过程? – Marc

+0

这是正确的做法。此外,您可以将虚假列添加到您的表(fake_status tinyint(1)不为空)。用您的限制实现插入/更新触发器。如果数据没有通过你的限制,你将设置'fake_status = NULL',这将导致数据根本不会插入,除了你应该在应用程序内处理。但它是完全邪恶的。 – ravnur