2

I'm confused about something, time and again i've seen developers including team leaders tell me that using foreign key constraints on their DB is bad practice, that it adds "logic to the database" but that goes against everything i've ever learned about SQL so far...

I need someone to tell me if i'm wrong for thinking that foreign key constraints are a major benefit or if i'm totally wrong and if so why. It just makes sense to me that in order to ensure data integrity you'd refuse any data that doesn't make sense being there, i'm sorry if this is a duplicate question, but i'm not really finding any definitive answers i'm happy with.

17
  • 1
    I flagged this question, because it will result in opinion based answers. And the usage of foreign keys, is up to one self, whether they are useful or not, is based on situation. To be clear, I don't think it is a bad question at all... just the answers will be bad. Commented Dec 3, 2016 at 18:31
  • 2
    What's wrong with foreign keys? Commented Dec 3, 2016 at 18:32
  • I'm voting to close this question as off-topic because this is a beef. Sure, we share it. Commented Dec 3, 2016 at 18:33
  • @juergend Sometimes they're not really an issue if you have other ways of ensuring data integrity or the consequences of an unlinked record are minimal. Commented Dec 3, 2016 at 18:49
  • 1
    So the answer so far is you can't without FK's. There is no Referential Integrity without FK's. The NDB storage engine handles RI across clusters. Commented Dec 3, 2016 at 19:17

1 Answer 1

0

thinking that foreign key constraints are a major benefit

That statement is bit confusing to me cause whether you will create a FK constraint between tables depends on whether those table(s) contains/maintain any relationship between them or not; like Students has courses (OR) Employees works under department

If those table can exist individually (stand alone) then why would you create a FOREIGN KEY constraint at all.

Per your comment: Then what's the reason for not using foreign keys. Ask your lead to give the explanation behind not using FK's instead just saying they are bad. If your lead says that they don't want to enforce foreign key constraint then suggest them to go for NOSQL database like MongoDB or Cassandra and not use Relational Database

Sign up to request clarification or add additional context in comments.

8 Comments

yes they do, of course, in my particular case it's a Crons platform where i have Campaigns and Crons associated with them. So one Campaign can have multiple Crons etc.. and i used FK constraints to ensure that no invalid ID's can be inserted at any point, and to ensure that no Campaign can be deleted while there's any Cron that references it.
@JonnySerra, see edit in answer if that helps.
@downvoter: care to explain the reason for downvote
You actually did not answer the question, did you?
@juergend, I believe I did.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.