MySQL 从 Null 更改为 Non Null
我有一个已设置为允许 Null 的字段,但现在想将其更改为不允许 Null。
当我尝试更改此设置时,我无法更改,因为 PhpMyAdmin 已将所有以前的记录设置为 Null。
我该如何解决这个问题?
是否有一个查询可以删除所有空值并将字段设置为非空?
I have got a field that has been setup to allow for Null, but am now wanting to change that to not allow for Null.
When ever I try to change this I can't because PhpMyAdmin has set all the previous records to Null.
How can I fix this problem?
Is there a query I could put that will remove all the Null values and set the field to Non Null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然。但是“删除所有
NULL
值”是什么意思?这是否意味着删除该字段中具有 NULL 的所有行?如果是这样,您可以这样做:如果您想保留这些行,则必须在该字段中添加一些其他值。有没有一个说得通的?如果是这样,您可以这样做:(
当然,
42
是您想要使用的任何值。)如果您想保留行,并且没有任何其他值,您可以使用,那么现在的
NULL
应该保留,并且可能是时候重新评估为什么要将字段设置为NOT NULL
了。Of course. But what do you you mean by "remove all the
NULL
values?" Does that mean to delete all the rows that have aNULL
in that field? If so, you could do this:If you want to keep those rows, you'll have to put some other value in that field. Is there one that makes sense? If so, you could do this:
(Where, of course,
42
is whatever value you want to use instead.)If you want to keep the rows, and there isn't any other value you could use, then the
NULL
s that are there now should stay, and it might be time to reevaluate why you want to set the field toNOT NULL
.您似乎对 null 的真正含义有点困惑。这是我在其他地方发表的文章,可以安全地在此处重新发布:
Null 表示没有预定义值。
当在数据库的表中创建列时,可以添加一些约束。这些约束之一是该列是否允许空值。
如果行上的字段未显式设置为其他值,则该字段将为空。您可以在创建表时提供默认值,也可以在向表中添加新行时设置一个值,或者稍后更新该行。要添加或更新表中的行,请使用 INSERT 和 UPDATE 命令。
当某个字段为空时,与该字段的所有比较也将为空。这意味着当涉及空值时,所有布尔代数都会出现错误:
* True AND Null 为 Null
* False AND Null 就是 Null
等等……
普通数学也不起作用,因为 5 + Null 等于 Null。
这样做的一个有趣的副作用是,当您比较两个都为空的字段时,结果将不是 true,而是 null。
为了仍然能够使用可以包含空值的字段,有几个选项: T-SQL (Microsoft SQL Server) 和 MySql 都具有 COALESCE() 和 ISNULL() 函数。
ISNULL():如果您要查找的值为空,则返回预定义值。 SELECT ISNULL(null, 2) 将返回 2
COALESCE():返回预定义列表中不为 null 的第一个值: SELECT COALESCE(a, b, c, d ...)
在 MySql 中还有 null 安全比较运算符:a <=> b 如果两个值都为 null,则返回 1 (true);如果其中一个值为 null,则返回 0 (false)。
在计算表中的行数时,您还需要了解 null 的含义。 SELECT COUNT(*) FROM TableName 将返回表中的总行数,但 SELECT COUNT(FieldName) FROM TableName 将仅返回“FieldName”列不为空的行数。
数据库设计者常常或多或少地出于懒惰,为表中的所有列设置默认值或限制所有字段不允许为空。当数据库设计者这样做时,每个使用数据库的人都被迫在没有实际值可用的情况下编写“魔法”值。
假设我们有一个用于存储车辆信息的表,并且在该表中我们有一列用于存储每个存储车辆的燃料类型。自行车应使用什么类型的燃料?如果该列允许为空,那么答案很简单;由于自行车上没有发动机,因此没有可供使用的燃料类型,因此只需将代表自行车的所有行上的字段设置为 null 即可。如果数据库设计者对不允许为 null 的列施加约束,则开发人员必须编写一个值来表示“无燃料类型”。如果该字段是整数,则可以使用零来做到这一点。此外,如果有一个表包含所有可行的燃料类型,车辆表应该引用该表来查找燃料类型的名称和描述,那么开发人员必须在表中放入假燃料类型,以便数据库管理服务器可以强制执行数据数据库的完整性。
Null 是数据库的一个非常好的特性,许多“普通”编程语言都没有,但数据库开发人员和设计人员需要跟踪它的使用位置和方式。
You seem a bit confused as to what a null realy is. Here's an article I've published somewhere else that should be safe to republish here:
Null means there is no predefined value.
When a column is created in a table in a database there are some constraints that can be added. One of those constraints is wether or not the column allows null values.
A field on a row will be null if it is not explicitly set to something else. You can either supply a default value when the table is created, or you can set a value when adding a new row to the table, or update the row later on. To add or update rows in a table the INSERT and UPDATE commands are used.
When a field is null all comparisons to that field will also be null. This means that all boolean algebra misbehaves when a null value is involved:
* True AND Null is Null
* False AND Null is Null
and so on …
Ordinary mathematics doesn't work either since 5 + Null equals Null.
An interesting side effect of this is that when you compare two fields that are both null the result will not be true, it will be null.
To still be able to work with fields that can contain null values there are a couple of options: Both T-SQL (Microsoft SQL Server) and MySql has the functions COALESCE() and ISNULL().
ISNULL(): returns a predefined value if the value you are looking for is null. SELECT ISNULL(null, 2) will return 2
COALESCE(): returns the first value in a predefined list that is not null: SELECT COALESCE(a, b, c, d ...)
In MySql there is also a null safe comparison operator: a <=> b It will return 1 (true) if both values are null and 0 (false) if either value is null.
When it comes to counting rows in a table you also need to be aware of the implications of null. SELECT COUNT(*) FROM TableName will return the total amount of rows in the table, but SELECT COUNT(FieldName) FROM TableName will will only return the amount of rows where the column 'FieldName' is not null.
Quite often the database designer, more or less by lazyness, sets a default value for all columns in a table or constrains all feilds to not allow null. When the database designer does that everyone that uses the database is forced to make up 'magic' values when ever there is no actual value to use.
Suppose we have a table for storing vehicle information and in that table we have a column to store the fuel type of each stored vehicle. What fuel type should be used for a bicycle? If the column allows null the answer is easy; since there is no engine on a bicycle there is no fuel type to use, so just set the field to null on all rows representing bicycles. If the database designer instead had put the contraint on the column that null is not allowed the developer have to make up a value to represent 'no fuel type'. If the field is an integer zero might be used to do that. Furthermore, if there is a table that contains all viable fuel types that the vehicle table is supposed to reference to find names and descriptions for fuel types the developer have to put fake fuel types in the table so that the database management server can enforce the data integrity of the database.
Null is a very nice feature of databases that lots of 'ordinary' programming languages don't have, but a database developer and designer needs to keep track of where and how it is used.