Are foreign keys really necessary in a database design?

前端 未结 24 2378
执笔经年
执笔经年 2020-11-28 18:08

As far as I know, foreign keys (FK) are used to aid the programmer to manipulate data in the correct way. Suppose a programmer is actually doing this in the right manner alr

24条回答
  •  庸人自扰
    2020-11-28 18:31

    The best thing about foreign key constraints (and constraints in general, really) are that you can rely on them when writing your queries. A lot of queries can become a lot more complicated if you can't rely on the data model holding "true".

    In code, we'll generally just get an exception thrown somewhere - but in SQL, we'll generally just get the "wrong" answers.

    In theory, SQL Server could use constraints as part of a query plan - but except for check constraints for partitioning, I can't say that I've ever actually witnessed that.

提交回复
热议问题