简体   繁体   English

如何在带有LOAD DATA的mysql十进制字段上使用null作为默认值?

[英]How to use null as default value on a mysql decimal field with LOAD DATA?

I load a CSV file using LOAD DATA LOCAL INFILE . 我使用LOAD DATA LOCAL INFILE加载CSV文件。 Some values eg for decimals are optional and thus not set in csv. 一些值(例如, decimals是可选的,因此未在csv中设置。

I tried both inserting a blank ,, in csv, and a ,null, . 我都尝试插入一个空白,,以CSV,和,null, For both of them the mysql table shows a 0 as decimal value. 对于它们两者, mysql表均显示0作为十进制值。

CREATE TABLE `my_table` (
     ...
    `price` decimal(10,2) DEFAULT NULL,
)

But why isn't the default value used if I leave the field blank during load? 但是,如果在加载期间将该字段保留为空白,为什么不使用默认值呢?

Sidenote: the linked question is not an answer, as the comment accepted has nothing to do with the special null handling in mysql! 旁注:链接的问题不是答案,因为接受的注释与mysql中的特殊null处理无关! Moreover, an acceptable answer to that question should state the solution as a oneliner, so everyone finds a quick solution. 此外,对于该问题的可接受答案应将解决方案描述为一个整体,因此每个人都可以找到快速解决方案。 Which is also not the case in the linked "duplciate" question. 链接的“重复”问题中也不是这种情况。

Found it: mysql is case sensitive here and expects a uppercase ,NULL, . 发现它:mysql在这里区分大小写,并且期望大写,NULL, Or ,\\N, ,\\N,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM