列空值/可选性:NULL vs NOT NULL [英] Column Nullability/Optionality: NULL vs NOT NULL

查看:99
本文介绍了列空值/可选性:NULL vs NOT NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某个mysql表中,是否存在将某些字段设置为 NULL NOT NULL 的原因从主/外键字段?

解决方案

完全取决于您的域名是诚实的。在功能上,它对数据库引擎几乎没有什么区别,但是如果您希望有一个明确定义的域,则通常最好将数据库和应用程序层都映射到用户的要求上。



如果无论用户是否输入了显示名称,都可以将列标记为可空。另一方面,如果要使用显示名称,则应在数据库中将其标记为非空,并在应用程序中强制执行约束。通过加倍约束,您确保您的前端更改,域仍然完全合格。


Is there a reason for or against setting some fields as NULL or NOT NULL in a mysql table, apart from primary/foreign key fields?

解决方案

That completely depends on your domain to be honest. Functionally it makes little difference to the database engine, but if you're looking to have a well defined domain it is often best to have both the database and application layer mirror the requirements you are placing on the user.

If it's moot to you whether or not the user enters their "Display Name", then by all means mark the column as nullable. On the other hand, if you are going to require a "Display Name" you should mark it non null in the database as well as enforcing the constraint in the application. By doubling the constraint, you ensure that should your front-end change, the domain is still fully qualified.

这篇关于列空值/可选性:NULL vs NOT NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆