空外键

扫码查看
嗯。就个人而言,在这个例子中我会说,树的根是 的个人是他们自己的经理。给我一个更好的例子 来说服我。 I''m having an argument with an incumbent self-taught programmer thatit is OK to use null foreign keys in database design.My take is the whole point of a foreign key is that it''s not supposedto be optional, it''s very definition is it''s a necessary link to theparent table and part of the definition. If it''s optional it shouldn''tbe part of the definition of a table and should be in a linking tableinstead.Comments? 解决方案I agree with your programmer. There are those who hold the ideal thatthere should be no nullable columns in database, but if you move alloptional columns into linking tables, you will drown in tables.Say that you in your business process you have an Order. An Order has acustomer - he is probably known from the beginning. There is an employeewho is responsible for assembling the order - but he is not known untilthe order is assigned to someone. The Order is later shipped, and addedto a shipment, and thus there is a Shipment ID. At the same time thereis an invoice ID. Yet, the customer pays, and now you have a payment ID.If you insist on linking tables we are now up to four. Since it wouldbe an error to have a payment ID, if there is not an invoice ID, you needenforce that restriction with a trigger, rather with a CHECK constrainton the table.Of course if we are talking about the OrderDetails table, the OrderIDin this table should be NOT NULL - but that is because OrderID ispart of the primary key of that table.--Erland Sommarskog, SQL Server MVP, so****@algonet.seBooks Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.aspHmmm. Personally in this example I would say that the individuals atthe root of the tree are their own managers. Give me a better exampleto convince me. 这篇关于空外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-16 12:56
查看更多