SQL:在不存在外键参考的外键列上添加默认约束

发布于 2025-02-07 09:37:40 字数 283 浏览 1 评论 0原文

如果我们尝试在表中不存在该默认值的外键上添加默认值约束,该怎么办?

示例:

  • table_person具有性别列作为外键
  • table_gender具有'1'和'2'的行,

如果我做默认约束,会发生什么在table_person for 性别 column 带有默认值='3'的列?

会创建或丢失错误吗?和原因?

What will happen if we try to add a default value constraint on a foreign key where that default value does not exist in the table ?

Example:

  • Table_Person has Gender column as foreign key
  • Table_Gender has rows with values '1' and '2'

What will happen if I make default constraint in Table_Person for Gender column with default value = '3' ?

Will it be created or throw an error? And reason for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2025-02-14 09:37:40

这将取决于高度在您使用的混凝土数据库系统(RDBMS)上。

对于 SQL Server

  • you can 创建两个表,包括table_person中的默认约束

  • you can can 中的默认约束明确定义性别的值1或2

  • you ,但是不能< /strong>将行插入table_person省略性别的值,从而将默认值插入3的值 - 因为这违反了FK约束


It will depend highly on what concrete database system (RDBMS) you're using.

For SQL Server:

  • You can create both tables, including the default constraint in Table_Person

  • You can insert rows into Table_Person that explicitly define a value for Gender which is 1 or 2

  • You can not however insert rows into Table_Person that omit the value for Gender and thus would get the default value of 3 inserted - since that violates the FK constraint

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文