如何处理MySQL中的空白条目
我一直在努力在MySQL中输入空白值。例如,代码:如何处理MySQL中的空白条目,mysql,Mysql,我一直在努力在MySQL中输入空白值。例如,代码: INSERT INTO `tablename` (column1, column2) VALUES ('value1', ''); 这是行不通的。我总是遇到以下错误: #1265 - Data truncated for column 'column2' at row 1 但是,当我运行这两条语句中的任何一条时: INSERT INTO `tablename` (column1, column2) VALUES ('value1', 'va
INSERT INTO `tablename` (column1, column2) VALUES ('value1', '');
这是行不通的。我总是遇到以下错误:
#1265 - Data truncated for column 'column2' at row 1
但是,当我运行这两条语句中的任何一条时:
INSERT INTO `tablename` (column1, column2) VALUES ('value1', 'value2');
或
查询工作正常。column2的默认值已设置为“0”,但仍然没有成功
我面临着所有表中所有列的问题。我的应用程序将出现空白条目(无法更改)。这个特性在早些时候运行良好,当我将应用程序代码转移到另一台服务器时,我开始出现这个错误
MySQL服务器版本:8.0.22-MySQL社区服务器-GPL
表结构如下所示:
我面临着所有列中的错误。任何帮助都将不胜感激
表架构:
CREATE TABLE `recordnew` (
`id` int NOT NULL AUTO_INCREMENT,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`course_id` int DEFAULT NULL,
`subject_id` int DEFAULT NULL,
`teacher_id` int DEFAULT NULL,
`student_id` int DEFAULT NULL,
`month` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`attendance` int DEFAULT NULL,
`totalclasses` int DEFAULT NULL,
`tutorialattendance` int DEFAULT NULL,
`tutorialclassestotal` int DEFAULT NULL,
`practicalattendance` int DEFAULT NULL,
`practicalclassestotal` int DEFAULT NULL,
`testmarks` double(11,2) DEFAULT '0.00',
`assignmentmarks` double(11,2) DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=68612 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Attendance & Internal Assessment Record'
我的实际查询:
INSERT INTO `recordnew`
(`course_id`,
`subject_id`,
`teacher_id`,
`student_id`,
`month`,
`attendance`,
`totalclasses`,
`testmarks`,
`assignmentmarks`,
`tutorialattendance`,
`tutorialclassestotal`,
`practicalattendance`,
`practicalclassestotal`)
VALUES ('13',
'17',
'10',
'1704',
'Marks',
'0',
'0',
'15',
'',
'0',
'0',
'0',
'0')
上述查询的错误:
#1265 - Data truncated for column 'assignmentmarks' at row 1
#1265 - Data truncated for column 'month' at row 1
其他SQL查询:
INSERT INTO `recordnew`
(`course_id`,
`subject_id`,
`teacher_id`,
`student_id`,
`month`,
`attendance`,
`totalclasses`,
`testmarks`,
`assignmentmarks`,
`tutorialattendance`,
`tutorialclassestotal`,
`practicalattendance`,
`practicalclassestotal`)
VALUES ('13',
'17',
'10',
'1704',
'',
'0',
'0',
'15',
'0',
'0',
'0',
'0',
'0')
上述查询出错:
#1265 - Data truncated for column 'assignmentmarks' at row 1
#1265 - Data truncated for column 'month' at row 1
最有可能的是,
column2
是数字数据类型,而不是类似字符串的数据类型。空字符串不是有效的数值,因此会收到警告
准则:
- 如果要存储空字符串,请使用字符串数据类型(
,varchar
等)char
- 不要将数字存储为字符串
- 如果要表示缺少数据,请使用
NULL