简体   繁体   English

在数字字段中插入空值而不是NULL

[英]insert empty values instead of NULL in numeric fields

I need your help please. 我需要你的帮助。

We have a PHP application running on MySQL, and we need to use PostgreSQL for a new customer. 我们有一个在MySQL上运行的PHP应用程序,我们需要将PostgreSQL用于新客户。

the problem is that when we insert empty strings in a field of type numeric, we get an error. 问题是,当我们在数字类型的字段中插入空字符串时,我们得到一个错误。

I think we should actually use NULL instead of empty string. 我认为我们应该使用NULL而不是空字符串。

However we would like to avoid having to recode all the SQL code of our application, my question is whether it is possible in the structure of a table (or elsewhere) to ensure that our numeric column accepts empty values? 但是我们希望避免重新编写应用程序的所有SQL代码,我的问题是,是否可以在表(或其他地方)的结构中确保我们的数值列接受空值? or convert them to NULL? 或将它们转换为NULL?

In the documentation I have seen that it is possible to define your own types, it is possible for example to create a type based on numeric and ensure that it accepts empty values? 在文档中我看到可以定义自己的类型,例如可以创建一个基于数字的类型并确保它接受空值? If yes how? 如果有,怎么样? I found no solution 我找不到解决办法

Thank you 谢谢

the problem is that when we insert empty strings in a field of type numeric, we get an error. 问题是,当我们在数字类型的字段中插入空字符串时,我们得到一个错误。

That's correct, a string isn't a number. 这是正确的,字符串不是数字。 Not even an empty string. 甚至不是空字符串。

I think we should actually use NULL instead of empty string. 我认为我们应该使用NULL而不是空字符串。

That's correct, your current SQL is wrong. 这是正确的,你当前的SQL是错误的。 If your MySQL-configuration would use a better SQL_MODE, your queries will also fail on MySQL. 如果您的MySQL配置将使用更好的SQL_MODE,那么您的查询也将在MySQL上失败。 Fix your code, it's by far the best option you have. 修复你的代码,它是迄今为止最好的选择。

Hacking around by creating dummy datatypes is just a hack, it's not a solution for bad SQL. 通过创建虚拟数据类型进行黑客攻击只是一个黑客,它不是一个坏SQL的解决方案。 The problem is your SQL, not your database. 问题是你的SQL,而不是你的数据库。

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

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