以下查询让我感到惊讶并没有失败,但插入了一条记录。
INSERT INTO buy_contacts(buys_id,contacts_id,buy_status,sites_id,record_status,date_modified,date_created)
VALUES(863999180,1367026068,"buy",2675631760,"active",NOW(),NOW());
SELECT查询将其显示为已插入。
+-----------+-------------+-------+-----------------+-----------------+---------------+------------+------------+---------------+----------------+
| buys_id | contacts_id | notes | date_modified | date_created | record_status | sites_id | buy_status | created_by_id | modified_by_id |
+-----------+-------------+-------+-----------------+-----------------+---------------+------------+------------+---------------+----------------+
| 863999180 | 1367026068 | NULL | 1/10/2015 10:31 | 1/10/2015 10:31 | active | 2675631760 | buy | 0 | 0 |
+-----------+-------------+-------+-----------------+-----------------+---------------+------------+------------+---------------+----------------+
我的表模式(由MySQL Workbench创建)如下所示。如图所示,created_by_id
和modified_by_id
都是NOT NULL
,没有DEFAULT
值,插入记录的查询不包含这些列。
为什么MySQL没有因为NOT NULL约束而拒绝查询,为什么它为这些列插入零值?
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
// other tables are created...
CREATE TABLE IF NOT EXISTS `buy_contacts` (
`buys_id` INT UNSIGNED NOT NULL,
`contacts_id` INT UNSIGNED NOT NULL,
`notes` TEXT NULL,
`date_modified` DATETIME NOT NULL,
`date_created` DATETIME NOT NULL,
`record_status` VARCHAR(8) NOT NULL,
`sites_id` INT UNSIGNED NOT NULL,
`buy_status` CHAR(3) NOT NULL,
`created_by_id` INT UNSIGNED NOT NULL,
`modified_by_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`buys_id`, `contacts_id`),
INDEX `fk_buys_has_contacts_contacts1_idx` (`contacts_id` ASC),
INDEX `fk_buys_has_contacts_buys1_idx` (`buys_id` ASC),
INDEX `fk_buy_contacts_record_status2_idx` (`record_status` ASC),
INDEX `fk_buy_contacts_sites2_idx` (`sites_id` ASC),
INDEX `fk_buy_contacts_buy_status1_idx` (`buy_status` ASC),
INDEX `fk_buy_contacts_users1_idx` (`created_by_id` ASC),
INDEX `fk_buy_contacts_users2_idx` (`modified_by_id` ASC),
CONSTRAINT `fk_buys_has_contacts_buys1`
FOREIGN KEY (`buys_id`)
REFERENCES `buys` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_buys_has_contacts_contacts1`
FOREIGN KEY (`contacts_id`)
REFERENCES `contacts` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_buy_contacts_record_status2`
FOREIGN KEY (`record_status`)
REFERENCES `record_status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_buy_contacts_sites2`
FOREIGN KEY (`sites_id`)
REFERENCES `sites` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_buy_contacts_buy_status1`
FOREIGN KEY (`buy_status`)
REFERENCES `buy_status` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_buy_contacts_users1`
FOREIGN KEY (`created_by_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_buy_contacts_users2`
FOREIGN KEY (`modified_by_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
答案 0 :(得分:1)
检查是否已启用STRICT_ALL_TABLES模式并安装了5.6版。
默认情况下,MySQL使用过时的"容忍"模式,非事务存储引擎(MyISAM)需要。