Are foreign keys really necessary in a database design?

前端 未结 24 2189
执笔经年
执笔经年 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:27

    You can view foreign keys as a constraint that,

    • Help maintain data integrity
    • Show how data is related to each other (which can help in enforcing business logic and rules)
    • If used correctly, can help increase the efficiency with which the data is fetched from the tables.
    0 讨论(0)
  • 2020-11-28 18:28

    Foreign keys can also help the programmer write less code using things like ON DELETE CASCADE. This means that if you have one table containing users and another containing orders or something, then deleting a user could automatically delete all orders that point to that user.

    0 讨论(0)
  • 2020-11-28 18:28

    A database schema without FK constraints is like driving without a seat belt.

    One day, you'll regret it. Not spending that little extra time on the design fundamentals and data integrity is a sure fire way of assuring headaches later.

    Would you accept code in your application that was that sloppy? That directly accessed the member objects and modified the data structures directly.

    Why do you think this has been made hard and even unacceptable within modern languages?

    0 讨论(0)
  • 2020-11-28 18:28

    Yes.

    1. They keep you honest
    2. They keep new developers honest
    3. You can do ON DELETE CASCADE
    4. They help you to generate nice diagrams that self explain the links between tables
    0 讨论(0)
  • 2020-11-28 18:30

    Is there a benefit to not having foreign keys? Unless you are using a crappy database, FKs aren't that hard to set up. So why would you have a policy of avoiding them? It's one thing to have a naming convention that says a column references another, it's another to know the database is actually verifying that relationship for you.

    0 讨论(0)
  • 2020-11-28 18:31

    Without a foreign key how do you tell that two records in different tables are related?

    I think what you are referring to is referential integrity, where the child record is not allowed to be created without an existing parent record etc. These are often known as foreign key constraints - but are not to be confused with the existence of foreign keys in the first place.

    0 讨论(0)
提交回复
热议问题