默认约束是实际约束吗?
在SQL中,我看到了几种类型的约束,例如主键。不是零,外键等。但是我现在偶然发现了默认约束,我感到困惑。
它阻止发生什么?任何查询都不会遵守吗?
如果没有查询由于默认约束而失败,那真的是一个约束吗?
谢谢。 一个困惑的开发人员
In SQL I see several types of constraints, like PRIMARY KEY. NOT NULL, FOREIGN KEY, etc. But I stumbled on DEFAULT constraints now and I'm confused.
What does it prevent to happen? Will any query fail to adhere by it?
If no query will ever fail because of a DEFAULT constraint, is it really a constraint?
Thank you.
A confused developer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果列是
而不是null
喜欢
,并且您尝试的
您会遇到错误,因为TE数据库无法用
default
value替换null,因为它不存在。因此,它将
插入到
中。if a column is
Not NULL
Like
and you try
You will get an error, as te database can't replace the NULL with a
DEFAULT
value, because it doesn't exist.so it constraints the
INSERT INTO
检查以下答案是否有助于您了解默认约束。
https://stackoverflow.com/a/1213316/7056491
Check if the following answer helps you understanding about DEFAULT constraint.
https://stackoverflow.com/a/1213316/7056491
如果列有默认约束,并且
insert
或update
语句不提供该列的值,则MySQL将使用默认值中指定的默认值
约束。因此,表:
您可以插入:
年龄将为4岁,性别将是“女性”。
默认约束不限制任何值的添加,也不像其他约束一样强制执行规则,但是它确实提供了适当的预定义值以保持域的完整性完整。
If a column has a DEFAULT constraint and the
INSERT
orUPDATE
statement doesn’t provide the value for that column, MySQL will use the default value specified in theDEFAULT
constraint.So for table:
You can Insert:
Age will be 4, and Gender will be 'Female'.
The DEFAULT constraint does not RESTRICT the addition of any value nor does it enforce a rule like the other constraints, but it does provide proper predefined values to keep domain integrity intact.