Do you absolutely need foreign keys in a database?

后端 未结 11 1585
孤城傲影
孤城傲影 2021-02-12 20:48

I was wondering how useful foreign keys really are in a database. Essentially, if the developers know what keys the different tables depend on, they can write the queries just

11条回答
  •  遇见更好的自我
    2021-02-12 21:21

    You don't have to use them but why wouldn't you?

    They are there to help. From making life easier with cascade updates and cascade deletes, to guaranteeing that constraints aren't violated.

    Maybe the application honors the constraints, but isn't it useful to have them clearly specified? You could document them, or you could put them in the database where most programmers expect to find constraints they are supposed to conform to (a better idea I think!).

    Finally, if you ever need to import data into this database which doesn't go via the front-end, you may accidently import data which violates the constraints and breaks the application.

    I'd definetly not recommend skipping the relationships in a database

提交回复
热议问题