我遇到了一个问题,可以在没有专门设置 INT NOT NULL 列的情况下将行插入表中(默认为 0,这对我的目的无效)
我以这种方式创建表:
CREATE TABLE inventorycontrol (
ID INT NOT NULL UNIQUE AUTO_INCREMENT
,nodeID INT NOT NULL CHECK (nodeID > 0)
,custom1 VARCHAR(128) NOT NULL DEFAULT ''
,custom2 VARCHAR(128) NOT NULL DEFAULT ''
,custom3 VARCHAR(128) NOT NULL DEFAULT ''
,custom4 VARCHAR(128) NOT NULL DEFAULT ''
,articlekey VARCHAR(32) NOT NULL
,amount INT NOT NULL
,orderID INT NULL
,supplierID INT NULL
,customerID INT NULL
,datecreated DATETIME NOT NULL
,expectedcompletion DATETIME NOT NULL
,datecompleted DATETIME NULL
,PRIMARY KEY (articlekey, datecreated)
)
GO
CREATE INDEX ix_InventoryArticle ON inventorycontrol ( articlekey )
GO
CREATE INDEX ix_InventoryArticle_Custom ON inventorycontrol ( nodeID, custom1, custom2, custom3, custom4 )
GO
CREATE INDEX ix_InventoryAmount ON inventorycontrol ( amount )
GO
CREATE INDEX ix_InventoryCreated ON inventorycontrol ( datecreated )
GO
CREATE INDEX ix_InventoryCompleted ON inventorycontrol ( datecompleted )
GO
ALTER TABLE inventorycontrol
ADD FOREIGN KEY fk_nodeID (nodeID) REFERENCES node(ID)
GO
我希望这个插入失败:
INSERT INTO inventorycontrol ( articlekey, amount, datecreated, expectedcompletion, datecompleted )
VALUES
( 'abc', -10, '2009-01-27', '2009-01-27', NULL )
GO
不幸的是,这行得通。(没有节点 WHERE ID=0)
有什么办法可以强制这个插入失败,而不是为插入创建触发器?
提前致谢,
克里斯
PS我正在使用mysql Ver 14.12 Distrib 5.0.45,用于redhat-linux-gnu(x86_64),使用readline 5.0