【问题标题】:How to handle blank entries in MySQL如何处理 MySQL 中的空白条目
【发布时间】:2020-12-04 05:52:31
【问题描述】:

我一直在努力在 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');

INSERT INTO `tablename` (column1, column2) VALUES ('value1', NULL);

查询工作得很好。 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

其他 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 'month' at row 1

【问题讨论】:

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

标签: mysql


【解决方案1】:

column2 很可能是数字数据类型,而不是类似字符串的数据类型。空字符串不是有效的数值,因此您会收到警告。

指南:

  • 如果要存储空字符串,请使用字符串数据类型(varcharchar 等)。

  • 不要将数字存储为字符串!

  • 如果要表示没有数据,使用NULL

【讨论】:

    猜你喜欢
    • 2015-10-14
    • 2013-04-25
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多