SQL:在不存在外键参考的外键列上添加默认约束
如果我们尝试在表中不存在该默认值的外键上添加默认值约束,该怎么办?
示例:
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
hasGender
column as foreign keyTable_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将取决于高度在您使用的混凝土数据库系统(RDBMS)上。
对于 SQL Server :
you can 创建两个表,包括
table_person中的默认约束
you can can 中的默认约束明确定义
性别
的值1或2you ,但是不能< /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 forGender
which is 1 or 2You can not however insert rows into
Table_Person
that omit the value forGender
and thus would get the default value of 3 inserted - since that violates the FK constraint