如何处理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

我一直在努力在MySQL中输入空白值。例如,代码:

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


模式是什么<代码>SHOW CREATE TABLE将有助于解释。我不是SQL方面的专家,但我认为添加表架构将真正帮助任何可能知道答案的人。我面临第8-15列中的错误。空字符串无法正确转换为数字数据类型-这是错误原因。设置零而不是空字符串,并且永远不要将字符串分配给数字列。在表的完整创建表和完整有问题的查询文本发布之前,我不会再给您任何建议。我的水晶球坏了。我看到了桌子的结构。现在发布查询代码和执行过程中产生的错误消息。