0

我遇到了一个问题,可以在没有专门设置 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

4

2 回答 2

3

您需要将 MySQL 置于严格模式

在您的配置文件中:

sql-mode="STRICT_ALL_TABLES"

编辑:

您还可以使用以下查询在本地设置(并将其放回):

SET @mode = @@SESSION.sql_mode;
SET sql_mode = "STRICT_ALL_TABLES";

INSERT...;

SET sql_mode = @mode;
于 2009-01-30T11:40:06.223 回答
-1

将值设置为 NOT NULL,如果这不起作用并且您无法在插入行之前通过代码检查值,则应该放置新的触发器,当数据库上有插入或修改调用时查找值。

希望这可以帮助!

于 2009-01-30T11:20:11.503 回答