如何在数据库中检查空标题?
我正在做我的第一个数据库项目。
我想知道为什么应该在以下查询中使用NOT NULL
...
TITLE nvarchar(60) NOT NULL
..
Context
CREATE TABLE Questions
(
USER_ID integer FOREIGN KEY
REFERENCES User_info(USER_ID)
PRIMARY KEY
CHECK (USER_ID>0),
QUESTION_ID integer FOREIGN KEY REFERENCES Tags(QUESTION_ID)
NOT NULL
CHECK (USER_ID>0),
QUESTION_BODY nvarchar(4000) NOT NULL,
TITLE nvarchar(60) NOT NULL, /////// HERE
MODERATOR_REMOVAL boolean NOT NULL,
SENT_TIME varchar(15) NOT NULL
)
我观看了 VPuml 的教程。 他们将逻辑图中的所有值都设置为可为空,而其余所有值NOT NULL
。 这表明可为空应该与逻辑图一起使用。
not null在数据库中还有其他用途吗?
我觉得我们可以通过JS检查用户是否给出了值,例如,而不是在数据库级别。
I am doing my first database project.
I would like to know why you should use NOT NULL
in the following query
...
TITLE nvarchar(60) NOT NULL
..
Context
CREATE TABLE Questions
(
USER_ID integer FOREIGN KEY
REFERENCES User_info(USER_ID)
PRIMARY KEY
CHECK (USER_ID>0),
QUESTION_ID integer FOREIGN KEY REFERENCES Tags(QUESTION_ID)
NOT NULL
CHECK (USER_ID>0),
QUESTION_BODY nvarchar(4000) NOT NULL,
TITLE nvarchar(60) NOT NULL, /////// HERE
MODERATOR_REMOVAL boolean NOT NULL,
SENT_TIME varchar(15) NOT NULL
)
I watched VPuml's tutorial. They put all values in Logical diagram nullable, while all the rest NOT NULL
. This suggests me that nullable should be used with logical diagrams.
Is there any other use of not null in databases?
I feel that we can check that the user gives value by JS, for instance, not at a database level.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果不允许使用空值,则应检查用户条目并且数据库中是否有 NOT NULL。 数据库中具有 NOT NULL 可以让您绝对确保不会将错误数据输入到数据库中,无论前端代码是否有错误。
但是,由于数据库错误通常很难向用户显示,因此您应该检查以确保在进行数据库检查之前没有提交空值。
If a null value is not allowed, you should check on user entry and have NOT NULL in the database. Having NOT NULL in the database allows you to make absolutely sure that no bad data is entered into the database, regardless of mistakes in front-end code.
However, since database errors are generally bad to show your users, you should check to make sure that a null value is not being submitted before it gets to the database check.
NOT NULL
通常用于外键(即链接到其他表)。 它确保一行链接到其他表。它几乎总是用在主键上,表示表中的唯一标识符。
这对于必填字段也是一个很大的限制。 这确保了如果用户未输入标题或问题,交易将会失败。 数据库非常擅长快速执行此类约束,因此您会看到许多此类业务逻辑被放入数据库中。 但请注意,您需要检查应用程序端的数据库错误并进行相应的处理。
无论如何,
NOT NULL
确实适用于您总是想要在其中输入值的列。这种情况实际上很多,但希望这能让您了解为什么我们要这样做用它。NOT NULL
is often used for foreign keys (i.e.-links to other tables). It ensures that a row links to some other table.It is almost always used on primary keys, meaning the unique identifier in the table.
It's also a great constraint for required fields. This ensures that the transaction will fail if a user doesn't enter in a title or a question. Databases are great at enforcing constraints like this very quickly, so you see a lot of that type of business logic put into databases. Do note, however, that you'll want to check for a database error on the application side and handle it accordingly.
Anyway,
NOT NULL
is really for a column you always want a value in. The cases for that are actually quite a few, but hopefully this gives you some semblance of why we use it.NOT NULL 表示必须在该列中放置一个值。 当数据没有值而存在毫无意义时,可以使用它。
例如,在您的“SO”数据库中,如果不知道 user_id 列,则存在问题是没有意义的。 问题必须始终由用户创建。
NOT NULL means that a value must be placed in the column. It is used when it makes no sense for the data to exist without a value.
For instance, in your "SO" database, it makes no sense for a question to exist without the user_id column being known. A question must always have been created by a user.
有时,查询取决于某个字段是否有值。 让字段为
NOT NULL
意味着您只需检查单个空值,在本例中为''
(空字符串)。 允许 NULL 值意味着您可能还必须检查该值,这会使查询变得复杂。Occasionally, a query depends on whether a certain field has a value. Having the field be
NOT NULL
means you only have to check against a single empty value, in this case''
(the empty string). AllowingNULL
values means you may have to check for that, too, which can complicate the query.