0

I have this script:

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='STRICT_ALL_TABLES';

SET SQL_MODE = `STRICT_ALL_TABLES`;

CREATE TABLE IF NOT EXISTS `w_bank_account` (
  `account_id` INT NOT NULL AUTO_INCREMENT,
  `bank_id` INT(11) NOT NULL,
  `account_number` VARCHAR(20) NOT NULL,
  `account_type` ENUM('1','2') NOT NULL,
  `balance` DECIMAL(19,4) NOT NULL DEFAULT 0,
  `created` TIMESTAMP NOT NULL,
  `modified` TIMESTAMP NULL,
  PRIMARY KEY (`account_id`),
  CONSTRAINT `fk_w_bank_account_n_bank1`
    FOREIGN KEY (`bank_id`)
    REFERENCES `n_bank` (`id`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE INDEX `fk_w_bank_account_n_bank1_idx` ON `w_bank_account` (`bank_id` ASC);

I try this two queries:

INSERT INTO `w_bank_account` (`bank_id`, `account_number`, `account_type`, `balance`) VALUES 
(1, '01234567890123456789', '1', 0.0000), 
(1, '01234567890123456789', '6', 0.0000);

And it works but in the second insert it leaves account_type empty (I think this goes to NULL) :-O why if I'm setting SQL_MODE as STRICT_ALL_TABLES? Should not the ENUM field gets the first value instead of leave field NULL? I'm using MySQL 5.5.31 on Debian

4

1 回答 1

1

http://dev.mysql.com/doc/refman/5.0/en/enum.html

如果将无效值插入 ENUM(即,允许值列表中不存在的字符串),则会将空字符串作为特殊错误值插入。这个字符串可以通过这个字符串的数值 0 与“正常”的空字符串区分开来。稍后会详细介绍。

您可以使用此触发功能

     delimiter //
     CREATE TRIGGER upd_check BEFORE UPDATE ON `w_bank_account`
     FOR EACH ROW
     BEGIN
     IF (NEW.`account_type` != '2' AND NEW.`account_type` != '1')
     SET NEW.`account_type` = '1';
     END IF;
     END;//
     delimiter ;
于 2013-08-06T16:21:05.833 回答