简体   繁体   English

MySQL中的非空列中的空字符串?

[英]Empty string in not-null column in MySQL?

I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff from PHP. 我曾经使用标准的mysql_connect(),mysql_query()等语句来从PHP做MySQL的东西。 Lately I've been switching over to using the wonderful MDB2 class. 最近我一直在转向使用精彩的MDB2课程。 Along with it, I'm using prepared statements, so I don't have to worry about escaping my input and SQL injection attacks. 除此之外,我正在使用预处理语句,所以我不必担心逃避输入和SQL注入攻击。

However, there's one problem I'm running into. 但是,我遇到了一个问题。 I have a table with a few VARCHAR columns, that are specified as not-null (that is, do not allow NULL values). 我有一个包含几个VARCHAR列的表,指定为非null(即,不允许NULL值)。 Using the old MySQL PHP commands, I could do things like this without any problem: 使用旧的MySQL PHP命令,我可以做这样的事情没有任何问题:

INSERT INTO mytable SET somevarchar = '';

Now, however, if I have a query like: 但是,现在,如果我有一个类似的查询:

INSERT INTO mytable SET somevarchar = ?;

And then in PHP I have: 然后在PHP中我有:

$value = "";
$prepared = $db->prepare($query, array('text'));
$result = $prepared->execute($value);

This will throw the error " null value violates not-null constraint " 这将抛出错误“ null value violates not-null constraint

As a temporary workaround, I check if $value is empty, and change it to " " (a single space), but that's a horrible hack and might cause other issues. 作为临时解决方法,我检查$value是否为空,并将其更改为" " (单个空格),但这是一个可怕的黑客,可能会导致其他问题。

How am I supposed to insert empty strings with prepared statements, without it trying to instead insert a NULL? 我怎么应该用准备好的语句插入空字符串,而不试图插入NULL?

EDIT: It's too big of a project to go through my entire codebase, find everywhere that uses an empty string "" and change it to use NULL instead. 编辑:通过我的整个代码库是一个太大的项目,找到使用空字符串“”的所有地方,并将其更改为使用NULL代替。 What I need to know is why standard MySQL queries treat "" and NULL as two separate things (as I think is correct), but prepared statements converts "" into NULL. 我需要知道的是为什么标准的MySQL查询将“”和NULL视为两个独立的东西(我认为是正确的),但是准备好的语句将“”转换为NULL。

Note that "" and NULL are not the same thing. 请注意,“”和NULL 不是一回事。 For Example, SELECT NULL = ""; 例如, SELECT NULL = ""; returns NULL instead of 1 as you'd expect. 如您所料,返回NULL而不是1

Thanks to some of the answers, I realized that the problem may be in the MDB2 API, and not in the PHP or MYSQL commands themselves. 感谢一些答案,我意识到问题可能出在MDB2 API中,而不是PHP或MYSQL命令本身。 Sure enough, I found this in the MDB2 FAQ: 果然,我在MDB2 FAQ中找到了这个:

  • Why do empty strings end up as NULL in the database? 为什么空字符串在数据库中最终为NULL? Why do I get an NULL not allowed in NOT NULL text fields eventhough the default value is ""? 为什么我的NOT NULL文本字段中不允许使用NULL,而默认值为“”?
    • The problem is that for some RDBMS (most noteably Oracle) an empty string is NULL. 问题是对于某些RDBMS(最值得注意的是Oracle),空字符串为NULL。 Therefore MDB2 provides a portability option to enforce the same behaviour on all RDBMS. 因此,MDB2提供了一个可移植性选项,以在所有RDBMS上强制执行相同的行为。
    • Since all portability options are enabled by default you will have to disable the feature if you dont want to have this behaviour: $mdb2->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL); 由于默认情况下启用了所有可移植性选项,因此如果您不希望出现此行为,则必须禁用该功能:$ mdb2-> setOption('portability',MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);

Thanks to everyone who provided thoughtful answers. 感谢所有提供深思熟虑答案的人。

This sounds like a problem with the MDB2 API fumbling PHP's duck typing semantics. 这听起来像是MDB2 API愚弄PHP的鸭子类型语义的问题。 Because the empty string in PHP is equivalent to NULL, MDB2 is probably mis-treating it as such. 因为PHP中的空字符串等于NULL,所以MDB2可能会错误地处理它。 The ideal solution would be to find a workaround for it within it's API, but I'm not overly familiar with it. 理想的解决方案是在其API中找到解决方法,但我并不过分熟悉它。

One thing that you should consider, though, is that an empty string in SQL is not a NULL value. 但是,您应该考虑的一件事是SQL中的空字符串不是NULL值。 You can insert them into rows declared 'NOT NULL' just fine: 你可以将它们插入声明为'NOT NULL'的行中就好了:

mysql> CREATE TABLE tbl( row CHAR(128) NOT NULL );
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO tbl VALUES( 'not empty' ), ( '' );
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT row, row IS NULL FROM tbl;
+-----------+-------------+
| row       | row IS NULL |
+-----------+-------------+
| not empty |           0 | 
|           |           0 | 
+-----------+-------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO tbl VALUES( NULL );
ERROR 1048 (23000): Column 'row' cannot be null

If you're unable to find (or implement) a workaround in the MDB2 API, one hackish solution (though slightly better than the one you're currently using) might be to define a user variable for the empty string -- 如果您无法在MDB2 API中找到(或实现)变通方法,那么一个hackish解决方案(虽然比您当前使用的解决方案略好)可能是为空字符串定义用户变量 -

SET @EMPTY_STRING = "";
UPDATE tbl SET row=@EMPTY_STRING;

Finally, if you need to use the empty string in an INSERT statement but find yourself unable to, the default value for string types in MySQL is an empty string. 最后,如果您需要在INSERT语句中使用空字符串但发现自己无法使用,则MySQL中字符串类型的默认值为空字符串。 So you could simply omit the column from INSERT statement and it would automatically get set to the empty string (provided the column has a NOT NULL constraint). 因此,您可以简单地省略INSERT语句中的列,它将自动设置为空字符串(前提是该列具有NOT NULL约束)。

I realize this question is pretty much answered and retired, but I found it while looking for answers to a similar situation and I can't resist throwing my hat in the ring. 我意识到这个问题已得到很好的回答和退休,但我在寻找类似情况的答案时找到了它,我无法抗拒将我的帽子扔进戒指。

Without knowing what the NULL/"" column relates to, I can't know how the true significance of an empty string. 在不知道NULL /“”列与什么相关的情况下,我无法知道空字符串的真正意义。 Does empty string mean something unto itself (like, if I convinced a judge to let me change my name to simply nothing, I would be really irritated if my name showed up on my Driver's License as NULL. My name would be ! 空字符串本身就意味着什么(就像,如果我说服法官让我改变我的名字就不算什么,如果我的名字出现在我的驾驶执照上,我会非常恼火。我的名字就是!

However, the empty string (or blank, or the nothingness that is SOMETHING, not simply the lack of anything (like NULL)) could also simply just mean "NOT NULL" or "Nothing, but still not Null". 但是,空字符串(或空白,或者是SOMETHING的虚无,而不仅仅是缺少任何东西(如NULL))也可能只是意味着“NOT NULL”或“Nothing,但仍然不是Null”。 You could even go the other direction and suggest that the absence of the value NULL makes it even LESS something than Null, cuz at least Null has a name you can say aloud! 你甚至可以去另一个方向,并建议缺少值NULL使它甚至比Null更少,因为至少Null有一个你可以大声说出的名字!

My point is, that if the empty string is a direct representation of some data (like a name, or what I prefer be inserted between the numbers in my phone number, etc), then your options are either to argue until you're sore for the legitimate use of empty string or to use something that represents an empty string that isn't NULL (Like an ASCII control character or some unicode equivalent, a regex value of some kind, or, even worse, an arbitrary yet totally unused token, like: ◘ 我的观点是,如果空字符串是某些数据的直接表示(如姓名,或者我喜欢插入电话号码中的数字之间等),那么你的选择要么是争论直到你感到疼痛合法使用空字符串或使用表示非空字符串的东西(如ASCII控制字符或某些等效的unicode,某种类型的正则表达式值,或者更糟糕的是,任意但完全未使用的令牌,像:◘

If the empty cell really just means NOT NULL, then you could think of some other way of expressing it. 如果空单元格实际上只是意味着NOT NULL,那么你可以想出一些表达它的方法。 One silly and obvious way is the phrase "Not NULL". 一个愚蠢而明显的方式是短语“Not NULL”。 But I have a hunch that NULL means something like "Not part of this group at all" while the empty string means something like "this guy is cool, he just hasn't gotten his gang tattoos yet". 但我有一种预感,即NULL意味着“根本不属于这个群体”,而空字符串意味着“这个家伙很酷,他还没有得到他的帮派纹身”。 In which case I would come up with a term/name/idea for this situation, like "default" or "rookie" or "Pending". 在这种情况下,我会针对这种情况提出一个术语/名称/想法,例如“默认”或“新手”或“待定”。

Now, if by some crazy chance you actually want empty string to represent that which is not even worthy of NULL, again, come up with a more significant symbol for that, such as "-1" or "SUPERNULL" or "UGLIES". 现在,如果有一些疯狂的机会,你实际上想要空字符串来代表那些甚至不值得为NULL的字符串,那么再次提出一个更重要的符号,例如“-1”或“SUPERNULL”或“UGLIES”。

In the Indian Caste System, the lowest Caste are Shudra: Farmers and Laborers. 在印度种姓制度中,最低的种姓是Shudra:农民和劳动者。 Beneath this caste are the Dalit: "The Untouchables". 在这个种姓之下是达利特:“不可触犯”。 They are not considered a lower caste, because setting them as the lowest caste would be considered a contamination of the entire system. 它们不被认为是较低的种姓,因为将它们设置为最低种姓将被视为整个系统的污染。

So don't call me crazy for thinking empty strings may be WORSE than NULL! 所以不要叫我疯狂,因为空字符串可能比NULL更糟糕!

'Til next time. '下一次。

I found the solution! 我找到了解决方案!

MDB2 converts empty strings to NULL because portability option MDB2_PORTABILITY_EMPTY_TO_NULL is on by default (thanks to Oracle which considers empty strings to be null). MDB2将空字符串转换为NULL,因为默认情况下可移植性选项MDB2_PORTABILITY_EMPTY_TO_NULL处于打开状态(感谢Oracle认为空字符串为空)。

Switch this options off when you connect to the database: 连接到数据库时,请关闭此选项:

$options = array(
    'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL
);
$res= & MDB2::connect("mysql://user:password@server/dbase", $options);

While 0 and empty strings are variables NULL is the absence of data. 0和空字符串是变量NULL是缺少数据。 And trust me, it's a lot easier to write a query to 相信我,编写查询要容易得多

SELECT * from table where mything IS NULL than to try to query for empty strings :/ SELECT * from table where mything IS NULL不是尝试查询空字符串:/

不是一组空的引号, ""那样做?

I'm confused. 我糊涂了。 It looks like you're using mysqli OO (from the tags and style), but the syntax is different than the manual on php.net, which says to do this instead: 看起来你正在使用mysqli OO(来自标签和样式),但语法与php.net上的手册不同,后者说这样做:

$query = "INSERT INTO mytable SET somevarchar = ?";
$value = "";
$prepared = $db->prepare($query);
$prepared->bind_param("s", $value);
$result = $prepared->execute();

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

相关问题 尝试将空字符串存储为非空列的值时,flavorishlib引发异常 - flourishlib throws exception when trying to store an empty string as a value of a not-null column mysql date列的PHP空字符串错误,值为空 - PHP empty string error for mysql date column with null value 在“IN CLAUSE”mysql php中返回null或空字符串 - returning null or empty string in “IN CLAUSE” mysql php 空的Mysql字段(NULL)在Java中最终是字符串“ null” - Empty Mysql field (NULL) ends up being a string “null” in Java 防止MySQL用空字符串更新列? - Prevent MySQL from updating column with empty string? PHP MYSQL->带有变量的UPDATE列(如果该变量不为null或为空) - PHP MYSQL -> UPDATE column with variable if that variable isn't null or empty Laravel:即使确实提供了密码,“密码”列中的空值也违反了非空约束” - Laravel: 'null value in column “password” violates not-null constraint' even though password is definitely provided MySQL中是否有一种方法可以将空字符串自动转换为NULL值? - Is there a way in MySQL to automatically convert empty string to NULL value? 在PHP中为MySQL插入使空字符串为NULL的函数 - Function to Make Empty String NULL for MySQL Insertion in PHP MySQL和PHP-插入NULL而不是空字符串 - MySQL and PHP - insert NULL rather than empty string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM