table1
bookid bookname author
0 No book no author
1 Engg Maths Greiwal
2 Java Basics James Gosling
3 Mahabharata Ved Vyasa
4 Ramayana Valmiki
5 Harry Potter JK Rowling
table2
userid name bookid
1 Arjun 2
2 Charles 2
3 Babbage 3
我有 table1 和 table2。 table1 中的 bookid 是主键,table2 中的 bookid 是外键。我想将 table2 bookid 默认值设置为 0。
有没有可能? 我们尝试使用默认值为零。
它抛出异常“无法添加或更新子行:外键约束失败”
最佳答案
为什么有一排叫“没书”? table 上只需要书,而不是“没有书” 首先从表 1 中删除无用的行,然后在表 2 中: 允许外键为空,如果外键为空则表示“没有书” 基本上,现在“null”是默认值,null 表示“没有书” 这就是你想要的
关于mysql - 如何在mysql中为外键列设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797417/