现象:
mysql> insert into TB_SEND_FILE (FILE_ID,FILE_NAME,DATA_TYPE,DATA_TYPE_C,FILE_SIZE,SEND_EFFORT,SEND_PROTOCOL,SEND_USER,SENDUSER_ID,SEND_START_TIME,SEND_TIME,SEND_RESULT,SEND_HOST,REGULAR_FILE_NAME,PRODUCT_TIME,ARICHIVE_PATH,PART_DAY,SEND_FROM_HOST,DEST_RELA_PATH,DATA_SOURCE,DATA_SOURCE_NAME) values('d0dd0e88-f7a1-11e8-8da5-6c92bf136552','Z_RADA_C_BABJ_20181204084200_P_DOR_RDCP_CR_ASWC.PNG','J','J.0012.0007.R000','1009962',1,'ftp','华信_DMZ','huaxin_dmz','2018-12-04 08:52:35','2018-12-04 08:52:35',0,'8a94cc9559b465c40159c3d2c0b30001','Z_RADA_C_BABJ_20181204084200_P_DOR_RDCP_CR_ASWC.PNG','2018-12-04 08:42:00',' ',4,'babj-cts01','//RADA/M',' ',NULL);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
一开始以为是TB_SEND_FILE这个表的列和插入的数据不匹配导致的,结果都是21列。
然后又只修改DATA_TYPE_C这一列的数据,将J.0012.0007.R000改为J0012.0007.R000,便可以执行成功。
mysql> insert into TB_SEND_FILE (FILE_ID,FILE_NAME,DATA_TYPE,DATA_TYPE_C,FILE_SIZE,SEND_EFFORT,SEND_PROTOCOL,SEND_USER,SENDUSER_ID,SEND_START_TIME,SEND_TIME,SEND_RESULT,SEND_HOST,REGULAR_FILE_NAME,PRODUCT_TIME,ARICHIVE_PATH,PART_DAY,SEND_FROM_HOST,DEST_RELA_PATH,DATA_SOURCE,DATA_SOURCE_NAME) values('d0dd0e88-f7a1-11e8-8da5-6c92bf136552','Z_RADA_C_BABJ_20181204084200_P_DOR_RDCP_CR_ASWC.PNG','J','J0012.0007.R000','1009962',1,'ftp','华信_DMZ','huaxin_dmz','2018-12-04 08:52:35','2018-12-04 08:52:35',0,'8a94cc9559b465c40159c3d2c0b30001','Z_RADA_C_BABJ_20181204084200_P_DOR_RDCP_CR_ASWC.PNG','2018-12-04 08:42:00',' ',4,'babj-cts01','//RADA/M',' ',NULL);
Query OK, 1 row affected, 0 warnings (0.00 sec)
建表语句如下:
CREATE TABLE `TB_SEND_FILE` (
`FILE_ID` varchar(40) NOT NULL COMMENT '文件ID',
`FILE_NAME` varchar(255) NOT NULL COMMENT '文件名',
`DATA_TYPE` varchar(50) DEFAULT NULL COMMENT '资料类型',
`DATA_TYPE_C` varchar(50) DEFAULT NULL COMMENT '资料子类型',
`FILE_SIZE` int(11) DEFAULT NULL COMMENT '文件大小:实际发送的文件大小信息',
`SEND_PROTOCOL` varchar(30) DEFAULT NULL COMMENT '分发协议:FTP,HTTP,HTTPS,SMTP',
`SEND_EFFORT` int(11) DEFAULT '0' COMMENT '文件发送时效:0不计,1及时,2逾限,3缺发',
`SEND_USER` varchar(20) NOT NULL COMMENT '发送用户',
`SEND_TIME` datetime NOT NULL COMMENT '发送结果时间:YYYY-MM-DD HH:MM:SS',
`SEND_RESULT` int(1) DEFAULT NULL COMMENT '发送结果:0,成功;1,失败',
`SEND_HOST` varchar(40) DEFAULT NULL COMMENT '发送主机',
`REGULAR_FILE_NAME` varchar(255) DEFAULT NULL COMMENT '规整文件名',
`ARICHIVE_PATH` varchar(255) DEFAULT NULL COMMENT '分发存档路径',
`HH` varchar(2) NOT NULL DEFAULT '00',
`DATA_SOURCE` varchar(30) DEFAULT NULL COMMENT '资料来源编码',
`DATA_SOURCE_NAME` varchar(100) DEFAULT NULL COMMENT '资料来源名称',
`PRODUCT_TIME` datetime DEFAULT NULL COMMENT '产品时间:YYYY-MM-DD HH:MM:SS',
`SENDUSER_ID` varchar(40) DEFAULT NULL COMMENT '分发用户ID',
`SEND_START_TIME` datetime DEFAULT NULL COMMENT '发送开始时间',
`PART_DAY` int(11) NOT NULL DEFAULT '0',
`SEND_FROM_HOST` varchar(40) DEFAULT NULL,
`DEST_RELA_PATH` varchar(300) DEFAULT NULL,
PRIMARY KEY (`PART_DAY`,`FILE_ID`,`SEND_USER`),
KEY `FILE_ID` (`FILE_ID`,`SEND_USER`),
KEY `INDEXSENDTIME` (`SEND_TIME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='文件分发信息表'
PARTITION BY RANGE (PART_DAY)
(PARTITION p1 VALUES LESS THAN (2) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (3) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (4) ENGINE = InnoDB,
PARTITION p4 VALUES LESS THAN (5) ENGINE = InnoDB,
PARTITION p5 VALUES LESS THAN (6) ENGINE = InnoDB,
PARTITION p6 VALUES LESS THAN (7) ENGINE = InnoDB,
PARTITION p7 VALUES LESS THAN (8) ENGINE = InnoDB,
PARTITION p8 VALUES LESS THAN (9) ENGINE = InnoDB,
PARTITION p9 VALUES LESS THAN (10) ENGINE = InnoDB,
PARTITION p10 VALUES LESS THAN (11) ENGINE = InnoDB,
PARTITION p11 VALUES LESS THAN (12) ENGINE = InnoDB,
PARTITION p12 VALUES LESS THAN (13) ENGINE = InnoDB,
PARTITION p13 VALUES LESS THAN (14) ENGINE = InnoDB,
PARTITION p14 VALUES LESS THAN (15) ENGINE = InnoDB,
PARTITION p15 VALUES LESS THAN (16) ENGINE = InnoDB,
PARTITION p16 VALUES LESS THAN (17) ENGINE = InnoDB,
PARTITION p17 VALUES LESS THAN (18) ENGINE = InnoDB,
PARTITION p18 VALUES LESS THAN (19) ENGINE = InnoDB,
PARTITION p19 VALUES LESS THAN (20) ENGINE = InnoDB,
PARTITION p20 VALUES LESS THAN (21) ENGINE = InnoDB,
PARTITION p21 VALUES LESS THAN (22) ENGINE = InnoDB,
PARTITION p22 VALUES LESS THAN (23) ENGINE = InnoDB,
PARTITION p23 VALUES LESS THAN (24) ENGINE = InnoDB,
PARTITION p24 VALUES LESS THAN (25) ENGINE = InnoDB,
PARTITION p25 VALUES LESS THAN (26) ENGINE = InnoDB,
PARTITION p26 VALUES LESS THAN (27) ENGINE = InnoDB,
PARTITION p27 VALUES LESS THAN (28) ENGINE = InnoDB,
PARTITION p28 VALUES LESS THAN (29) ENGINE = InnoDB,
PARTITION p29 VALUES LESS THAN (30) ENGINE = InnoDB,
PARTITION p30 VALUES LESS THAN (31) ENGINE = InnoDB,
PARTITION p31 VALUES LESS THAN MAXVALUE ENGINE = InnoDB);
继续排查如下:
mysql> insert into TB_SEND_FILE (DATA_TYPE_C) values('123');
ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '>='
当单独插入一行数据时报collation的问题,然后检查该表的collation和charset。也没有发现问题,排查如下:
mysql> show variables like '%collation%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
mysql> show variables like '%char%';
+--------------------------+-----------------------------+
| Variable_name | Value |
+--------------------------+-----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /home/mysql/share/charsets/ |
+--------------------------+-----------------------------+
8 rows in set (0.00 sec)
mysql> show full fields from TB_SEND_FILE;
+-------------------+--------------+-----------------+------+-----+---------+-------+---------------------------------+----------------------------------------------------------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------------------+--------------+-----------------+------+-----+---------+-------+---------------------------------+----------------------------------------------------------+
| FILE_ID | varchar(40) | utf8_general_ci | NO | PRI | NULL | | select,insert,update,references | 文件ID |
| FILE_NAME | varchar(255) | utf8_general_ci | NO | | NULL | | select,insert,update,references | 文件名 |
| DATA_TYPE | varchar(50) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 资料类型 |
| DATA_TYPE_C | varchar(50) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 资料子类型 |
| FILE_SIZE | int(11) | NULL | YES | | NULL | | select,insert,update,references | 文件大小:实际发送的文件大小信息 |
| SEND_PROTOCOL | varchar(30) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 分发协议:FTP,HTTP,HTTPS,SMTP |
| SEND_EFFORT | int(11) | NULL | YES | | 0 | | select,insert,update,references | 文件发送时效:0不计,1及时,2逾限,3缺发 |
| SEND_USER | varchar(20) | utf8_general_ci | NO | PRI | NULL | | select,insert,update,references | 发送用户 |
| SEND_TIME | datetime | NULL | NO | MUL | NULL | | select,insert,update,references | 发送结果时间:YYYY-MM-DD HH:MM:SS |
| SEND_RESULT | int(1) | NULL | YES | | NULL | | select,insert,update,references | 发送结果:0,成功;1,失败 |
| SEND_HOST | varchar(40) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 发送主机 |
| REGULAR_FILE_NAME | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 规整文件名 |
| ARICHIVE_PATH | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 分发存档路径 |
| HH | varchar(2) | utf8_general_ci | NO | | 00 | | select,insert,update,references | |
| DATA_SOURCE | varchar(30) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 资料来源编码 |
| DATA_SOURCE_NAME | varchar(100) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 资料来源名称 |
| PRODUCT_TIME | datetime | NULL | YES | | NULL | | select,insert,update,references | 产品时间:YYYY-MM-DD HH:MM:SS |
| SENDUSER_ID | varchar(40) | utf8_general_ci | YES | | NULL | | select,insert,update,references | 分发用户ID |
| SEND_START_TIME | datetime | NULL | YES | | NULL | | select,insert,update,references | 发送开始时间 |
| PART_DAY | int(11) | NULL | NO | PRI | 0 | | select,insert,update,references | |
| SEND_FROM_HOST | varchar(40) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| DEST_RELA_PATH | varchar(300) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
+-------------------+--------------+-----------------+------+-----+---------+-------+---------------------------------+----------------------------------------------------------+
继续查找该表的约束,找到一个触发器:
mysql> select TRIGGER_NAME from information_schema.TRIGGERS where EVENT_OBJECT_TABLE='TB_SEND_FILE';
+----------------+
| TRIGGER_NAME |
+----------------+
| SEND_FILE_DI_I |
+----------------+
查看该触发器:
mysql> show create trigger SEND_FILE_DI_I\G
*************************** 1. row ***************************
Trigger: SEND_FILE_DI_I
sql_mode: NO_ENGINE_SUBSTITUTION
SQL Original Statement: CREATE DEFINER=`dbscale`@`%` trigger SEND_FILE_DI_I after insert on TB_SEND_FILE for each row
BEGIN
DECLARE stationType VARCHAR(5);
DECLARE netType VARCHAR(6);
DECLARE date_time DATETIME;
SET date_time=DATE_ADD(DATE(NOW()),INTERVAL -3 DAY);
IF NEW.SEND_RESULT=0 AND SUBSTR(NEW.PRODUCT_TIME,1,10)>=date_time THEN
SELECT C_NETSTATION_TYPE,C_NETTYPE INTO stationType,netType FROM TAB_OMIN_CM_CC_DATUMPRO
WHERE C_DATUM_CODE = SUBSTR(NEW.DATA_TYPE_C,1,11);
IF stationType='1' THEN
INSERT INTO TAB_OMIN_CTS_DI_1(ID,DATA_ID,STATION_TYPE,S_F_NAME,RECORD_DATE,INFO_DETAIL_HOUR,
PROCESS_PERIOD,PROCESS_START_TIME,PROCESS_END_TIME,SEND_USER,PROCESS_STATUS,
DATA_SOURCE,RECORD_TIME,S_S_NAME,DATA_ID2,C_BBB,C_GPSTATUS,PART_DAY)
VALUES (UUID(),NEW.DATA_TYPE_C,'9999',IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,18),NEW.FILE_NAME),
DATE_FORMAT(NEW.PRODUCT_TIME,"%Y-%m-%d"),DATE_FORMAT(NEW.PRODUCT_TIME,"%H:%i"),3,
NEW.SEND_TIME,NEW.SEND_TIME,NEW.SENDUSER_ID,NEW.SEND_EFFORT,IF(netType IS NOT NULL,netType,''),NOW(),'',
IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,1,16),NEW.DATA_TYPE_C),'','0'),
(UUID(),NEW.DATA_TYPE_C,'9999',IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,18),NEW.FILE_NAME),
DATE_FORMAT(NEW.PRODUCT_TIME,"%Y-%m-%d"),DATE_FORMAT(NEW.PRODUCT_TIME,"%H:%i"),1,
(SELECT FILE_ARRIVE_TIME FROM TB_RCV_FILE t1 WHERE t1.FILE_ID=NEW.FILE_ID LIMIT 1),
NEW.SEND_TIME,NEW.SENDUSER_ID,0,IF(netType IS NOT NULL,netType,''),NOW(),'',
IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,1,16),NEW.DATA_TYPE_C),'','0',DAYOFMONTH(NOW()));
END IF;
END IF;
END
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)
删除触发器,执行插入操作,成功:
mysql> drop trigger SEND_FILE_DI_I;
mysql> insert into TB_SEND_FILE (FILE_ID,FILE_NAME,DATA_TYPE,DATA_TYPE_C,FILE_SIZE,SEND_EFFORT,SEND_PROTOCOL,SEND_USER,SENDUSER_ID,SEND_START_TIME,SEND_TIME,SEND_RESULT,SEND_HOST,REGULAR_FILE_NAME,PRODUCT_TIME,ARICHIVE_PATH,PART_DAY,SEND_FROM_HOST,DEST_RELA_PATH,DATA_SOURCE,DATA_SOURCE_NAME) values('d0dd0e88-f7a1-11e8-8da5-6c92bf136552','Z_RADA_C_BABJ_20181204084200_P_DOR_RDCP_CR_ASWC.PNG','J','J.0012.0007.R000','1009962',1,'ftp','华信_DMZ','huaxin_dmz','2018-12-04 08:52:35','2018-12-04 08:52:35',0,'8a94cc9559b465c40159c3d2c0b30001','Z_RADA_C_BABJ_20181204084200_P_DOR_RDCP_CR_ASWC.PNG','2018-12-04
08:42:00',' ',4,'babj-cts01','//RADA/M',' ',NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into TB_SEND_FILE (FILE_ID) values ('123');
Query OK, 1 row affected, 3 warnings (0.00 sec)
问题分析:
将触发器创建语句捞出来单独检查:
CREATE DEFINER=`dbscale`@`%` trigger SEND_FILE_DI_I after insert on TB_SEND_FILE for each row
BEGIN
DECLARE stationType VARCHAR(5);
DECLARE netType VARCHAR(6);
DECLARE date_time DATETIME;
SET date_time=DATE_ADD(DATE(NOW()),INTERVAL -3 DAY);
IF NEW.SEND_RESULT=0 AND SUBSTR(NEW.PRODUCT_TIME,1,10)>=date_time THEN
SELECT C_NETSTATION_TYPE,C_NETTYPE INTO stationType,netType FROM TAB_OMIN_CM_CC_DATUMPRO
WHERE C_DATUM_CODE = SUBSTR(NEW.DATA_TYPE_C,1,11);
IF stationType='1' THEN
INSERT INTO TAB_OMIN_CTS_DI_1(ID,DATA_ID,STATION_TYPE,S_F_NAME,RECORD_DATE,INFO_DETAIL_HOUR,
PROCESS_PERIOD,PROCESS_START_TIME,PROCESS_END_TIME,SEND_USER,PROCESS_STATUS,
DATA_SOURCE,RECORD_TIME,S_S_NAME,DATA_ID2,C_BBB,C_GPSTATUS,PART_DAY)
VALUES (UUID(),NEW.DATA_TYPE_C,'9999',IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,18),NEW.FILE_NAME),
DATE_FORMAT(NEW.PRODUCT_TIME,"%Y-%m-%d"),DATE_FORMAT(NEW.PRODUCT_TIME,"%H:%i"),3,
NEW.SEND_TIME,NEW.SEND_TIME,NEW.SENDUSER_ID,NEW.SEND_EFFORT,IF(netType IS NOT NULL,netType,''),NOW(),'',
IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,1,16),NEW.DATA_TYPE_C),'','0'),
(UUID(),NEW.DATA_TYPE_C,'9999',IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,18),NEW.FILE_NAME),
DATE_FORMAT(NEW.PRODUCT_TIME,"%Y-%m-%d"),DATE_FORMAT(NEW.PRODUCT_TIME,"%H:%i"),1,
(SELECT FILE_ARRIVE_TIME FROM TB_RCV_FILE t1 WHERE t1.FILE_ID=NEW.FILE_ID LIMIT 1),
NEW.SEND_TIME,NEW.SENDUSER_ID,0,IF(netType IS NOT NULL,netType,''),NOW(),'',
IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,1,16),NEW.DATA_TYPE_C),'','0',DAYOFMONTH(NOW()));
END IF;
END IF;
END
插入报错ERROR 1136 (21S01): Column count doesn't match value count at row 1
的原因分析:
该触发器中包含了INSERT INTO TAB_OMIN_CTS_DI_1
语句,该语句共插入了18个列,其中第一行数据为17列,第二行数据为18列。如下:
INSERT INTO
TAB_OMIN_CTS_DI_1(ID,DATA_ID,STATION_TYPE,S_F_NAME,RECORD_DATE,INFO_DETAIL_HOUR,
PROCESS_PERIOD,PROCESS_START_TIME,PROCESS_END_TIME,SEND_USER,PROCESS_STATUS,
DATA_SOURCE,RECORD_TIME,S_S_NAME,DATA_ID2,C_BBB,C_GPSTATUS,PART_DAY)
VALUES
(UUID(),NEW.DATA_TYPE_C,'9999',IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,18),NEW.FILE_NAME),
DATE_FORMAT(NEW.PRODUCT_TIME,"%Y-%m-%d"),DATE_FORMAT(NEW.PRODUCT_TIME,"%H:%i"),3,
NEW.SEND_TIME,NEW.SEND_TIME,NEW.SENDUSER_ID,NEW.SEND_EFFORT,IF(netType IS NOT NULL,netType,''),NOW(),'',
IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,1,16),NEW.DATA_TYPE_C),'','0'),
**//该语句包含17个列**
(UUID(),NEW.DATA_TYPE_C,'9999',IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,18),NEW.FILE_NAME),
DATE_FORMAT(NEW.PRODUCT_TIME,"%Y-%m-%d"),DATE_FORMAT(NEW.PRODUCT_TIME,"%H:%i"),1,
(SELECT FILE_ARRIVE_TIME FROM TB_RCV_FILE t1 WHERE t1.FILE_ID=NEW.FILE_ID LIMIT 1),
NEW.SEND_TIME,NEW.SENDUSER_ID,0,IF(netType IS NOT NULL,netType,''),NOW(),'',
IF(SUBSTR(NEW.FILE_NAME,17,1)='~',SUBSTR(NEW.FILE_NAME,1,16),NEW.DATA_TYPE_C),'','0',DAYOFMONTH(NOW()));
**//该语句包含18个列**
插入数据中将DATA_TYPE_C
列的数据去掉一个点,插入正常原因分析:
SET date_time=DATE_ADD(DATE(NOW()),INTERVAL -3 DAY);
IF NEW.SEND_RESULT=0 AND SUBSTR(NEW.PRODUCT_TIME,1,10)>=date_time THEN
SELECT C_NETSTATION_TYPE,C_NETTYPE INTO stationType,netType FROM TAB_OMIN_CM_CC_DATUMPRO
WHERE C_DATUM_CODE = SUBSTR(NEW.DATA_TYPE_C,1,11);
IF stationType='1' THEN
INSERT INTO TAB_OMIN_CTS_DI_1 ...
从触发器可以看出,DATA_TYPE_C
列为J00时并没有匹配到TAB_OMIN_CM_CC_DATUMPRO
表中的数据,所以不会继续触发INSERT INTO TAB_OMIN_CTS_DI_1
。
插入报错ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '>='
原因分析:
SET date_time=DATE_ADD(DATE(NOW()),INTERVAL -3 DAY);
IF NEW.SEND_RESULT=0 AND SUBSTR(NEW.PRODUCT_TIME,1,10)>=date_time THEN
SELECT C_NETSTATION_TYPE,C_NETTYPE INTO stationType,netType FROM TAB_OMIN_CM_CC_DATUMPRO
WHERE C_DATUM_CODE = SUBSTR(NEW.DATA_TYPE_C,1,11);
IF stationType='1' THEN
INSERT INTO TAB_OMIN_CTS_DI_1 ...
因为触发器中并没有传入NEW.SEND_RESULT、NEW.PRODUCT_TIME
的值,而date_time
定义的是三天前的同一时间,所以进行判断时会跟空值比较,从而报错。