SQL - NULL外键 [英] SQL - NULL foreign key

查看:159
本文介绍了SQL - NULL外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  create table Person(id int identity,InvoiceID int not null)

请注意下面的数据库设计:
create table发票(id int identity,date datetime)

目前所有的人都有一个 invoiceID InvoiceID not null



我想扩展数据库,以便某人没有发票。原始的开发者恨零,从不使用它们。我想要一致,所以我想知道是否有其他模式,我可以用来扩展数据库,以满足这一要求。如何在不使用空值的情况下实现这个功能?



请注意,以上两个表格仅用于说明目的。它们不是实际的表。

解决方案

NULL在数据库和编程中,一般来说,NULL是非常重要的功能。它与零或任何其他值显着不同。它通常用来表示缺乏价值(虽然它也可能意味着未知的价值,但是这不太被用作解释)。如果有人没有发票,那么你应该真正地允许NULL,因为它符合你想要的Schema


Please have a look at the database design below:

create table Person (id int identity, InvoiceID int not null) 
create table Invoice (id int identity, date datetime)

Currently all persons have an invoiceID i.e. the InvoiceID is not null.

I want to extend the database so that some person does not have an Invoice. The original developer hated nulls and never uses them. I want to be consistent so I am wondering if there are other patterns I can use to extend the database to meet this requirement. How can this be approached without using nulls?

Please note that the two tables above are for illustration purposes. They are not the actual tables.

解决方案

NULL is a very important feature in databases and programming in general. It is significantly different from being zero or any other value. It is most commonly used to signify absence of value (though it also can mean unknown value, but that's less used as the interpretation). If some people do not have an invoice, then you should truly allow NULL, as that matches your desired Schema

这篇关于SQL - NULL外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆