Are foreign keys really necessary in a database design?

Regarding I recognize, foreign keys (FK) are made use of to assist the designer to adjust information in the proper means. Intend a designer is in fact doing this in the appropriate fashion currently, after that do we actually require the principle of foreign keys?

Exist any kind of various other usages for foreign keys? Am I missing out on something below?

130
2022-06-03 19:15:04
Source Share
Answers: 20

They are necessary, due to the fact that your application is not the only means information can be adjusted in the database. Your application might take care of referential honesty as truthfully as it desires, yet all it takes is one jackass with the appropriate advantages ahead along and also release an insert, delete or upgrade command at the database degree, and also all your application referential honesty enforcement is bypassed. Placing FK restraints in at the database degree suggests that, preventing this jackass picking to disable the FK restraint prior to releasing their command, the FK restraint will certainly create a negative insert/update/delete declaration to fall short with a referential honesty offense.

4
2022-06-30 01:59:14
Source

I intend you are speaking about international key restraints applied by the database . You possibly currently are making use of foreign keys, you simply have not informed the database concerning it.

Intend a designer is in fact doing this in the appropriate fashion currently, after that do we actually require the principle of foreign keys?

In theory, no. Nonetheless, there have actually never ever been an item of software program without pests.

Pests in application code are commonly not that unsafe - you recognize the bug and also repair it, and also afterwards the application runs efficiently once more. Yet if a bug permits currupt information to enter the database, after that you are persevered! It is really tough to recoup from corrupt information in the database.

Take into consideration if a refined bug in FogBugz permitted a corrupt international key to be created in the database. It could be very easy to deal with the bug and also promptly push the solution to consumers in a bugfix release. Nonetheless, just how should the corrupt information in loads of data sources be dealt with? Proper code could currently instantly damage due to the fact that the presumptions concerning the honesty of foreign keys do not hold any longer.

In internet applications you commonly just have one program talking to the database, so there is just one area where pests can corrupt the information. In a venture application there could be numerous independent applications talking with the very same database (as well as individuals functioning straight with the database covering). There is no other way to ensure that all applications adhere to the very same presumptions without pests, constantly and also for life.

If restraints are inscribed in the database, after that the most awful that can occur with pests is that the customer is revealed a hideous mistake message concerning some SQL restraint not completely satisfied. This is much prefereable to allow currupt information right into your venture database, where it subsequently will certainly damage all your applications or simply bring about all sort of incorrect or deceptive result.

Oh, and also international key restraints additionally boosts efficiency due to the fact that they are indexed by default. I can not consider any kind of factor not to make use of international key restraints.

8
2022-06-29 03:28:39
Source

Foreign keys permit a person that has actually not seen your database before to establish the partnership in between tables.

Every little thing might be great currently, yet assume what will certainly take place when your designer leaves and also somebody else needs to take control of.

Foreign keys will certainly permit them to recognize the database framework without trawling via hundred of lines of code.

5
2022-06-28 23:19:53
Source

Yes. The ON DELETE [RESTRICT|CASCADE] maintains programmers from stranding information, maintaining the information tidy. I lately signed up with a group of Rails programmers that did not concentrate on database restraints such as foreign keys.

The good news is, I located these: http://www.redhillonrails.org/foreign_key_associations.html - - RedHill on Ruby on Rails connect - ins create foreign keys making use of the convention over configuration design. A movement with product_id will certainly create an international key to the id in the items table.

Look into the various other wonderful plug - ins at RedHill, consisting of movements covered in purchases.

0
2022-06-28 23:18:54
Source

Is there an advantage to not having foreign keys? Unless you are making use of a bad database, FKs aren't that tough to set up. So why would certainly you have a plan of preventing them? It is one point to have a calling convention that claims a column referrals an additional, it is an additional to recognize the database is in fact validating that partnership for you.

8
2022-06-05 01:32:07
Source

Foreign keys had actually never ever been specific (FOREIGN KEY REFERENCES table (column)) proclaimed in tasks (organization applications and also social networking internet sites) which I worked with.

Yet there constantly was a sort of convention of calling columns which were foreign keys.

It resembles with database normalization - - you need to recognize what are you doing and also what are effect of that (mostly performance).

I recognize benefits of foreign keys (information honesty, index for international key column, devices knowledgeable about database schema), yet additionally I hesitate of making use of foreign keys as basic regulation.

Additionally numerous database engines can offer foreign type in a various means, which can bring about refined pests throughout movement.

Getting rid of all orders and also billings of removed customer with ON DELETE CASCADE is the excellent instance of wonderful looking, yet incorrect made, database schema.

3
2022-06-04 02:06:32
Source

I think of it in regards to cost/benefit ... In MySQL, including a restraint is a solitary added line of DDL. It is simply a handful of keywords and also a number of secs of idea. That is the only "cost" in my point of view ...

Tools enjoy foreign keys. Foreign keys protect against negative information (that is, orphaned rows) that might not influence organization reasoning or capability and also consequently go undetected, and also accumulate. It additionally protects against programmers that are not familiar with the schema from applying whole portions of job without understanding they are missing out on a partnership. Probably every little thing is wonderful within the extent of your existing application, yet if you missed out on something and also sooner or later something unanticipated is included (assume expensive coverage), you could be in a place where you need to by hand clean up negative information that is been gathering given that the beginning of the schema without a database applied check.

The little time it requires to order what is currently in your head when you are placing points with each other can conserve you or somebody else a number of pain months or years later on.

The inquiry :

Are there any kind of various other usages for foreign keys? Am I missing out on something below?

It is a little bit crammed. Insert remarks, impression or variable identifying instead of "foreign keys" ... If you currently recognize things concerned flawlessly, it is "no use" to you.

3
2022-06-04 01:52:04
Source

A database schema without FK restraints resembles driving without a safety belt.

Eventually, you'll regret it. Not investing that little added time on the layout principles and also information honesty is a certain fire means of ensuring frustrations later on.

Would certainly you approve code in your application that was that careless? That straight accessed the participant things and also changed the information frameworks straight.

Why do you assume this has been made tough and also also undesirable within modern-day languages?

45
2022-06-04 01:23:56
Source

The ideal feature of international key restraints (and also restraints as a whole, actually) are that you can rely upon them when creating your questions. A great deal of questions can come to be a whole lot extra difficult if you can not rely upon the information version holding "true".

In code, we'll usually simply get an exemption tossed someplace - yet in SQL, we'll usually simply get the "wrong" solutions.

Theoretically, SQL Server can make use of restraints as component of a question strategy - yet with the exception of check restraints for dividing, I can not claim that I've ever before in fact observed that.

2
2022-06-03 20:33:16
Source

Foreign keys aid apply referential honesty at the information degree. They additionally boost efficiency due to the fact that they are generally indexed by default.

116
2022-06-03 19:46:13
Source

Without an international key just how do you inform that 2 documents in various tables are connected?

I assume what you are describing is referential honesty, where the youngster document is not permitted to be developed without an existing moms and dad document and so on These are usually called international key restraints - yet are not to be perplexed with the presence of foreign type in the starting point.

10
2022-06-03 19:45:06
Source

Suppose a designer is in fact doing this in the appropriate fashion currently

Making such a supposition appears to me to be an exceptionally negative suggestion ; as a whole software program is extremely buggy.

Which is the factor, actually. Programmers can not get points right, so making certain the database can not be loaded with negative information is a Good Thing.

Although in an excellent globe, all-natural signs up with would certainly make use of partnerships (i.e. FK restraints) as opposed to matching column names. This would certainly make FKs a lot more valuable.

16
2022-06-03 19:43:36
Source

Personally, I favor foreign keys due to the fact that it defines the partnership in between the tables. I understand that your inquiry infers that the designer is not presenting information that would certainly go against referential honesty, yet I have actually seen means way too many circumstances where information referential honesty is gone against, regardless of ideal purposes!

Pre - international key restraints (also known as declarative referential honesty or DRI) great deals of time was invested applying these partnerships making use of triggers. The reality that we can define the partnership by a declarative restraint is really effective.

@John - Other data sources might instantly create indexes for foreign keys, yet SQL Server does not. In SQL Server, international key partnerships are just restraints. You have to specified your index on foreign keys independently (which can be of advantage.)

Modify : I would certainly such as to add that, IMO, making use of foreign type in assistance of ON DELETE or ON UPDATE CASCADE is not always a good idea. In technique, I have actually located that waterfall on delete need to be meticulously taken into consideration based upon the partnership of the information - - as an example do you have an all-natural moms and dad - youngster where this might be alright or is the relevant table a set of lookup values. Making use of cascaded updates indicates you are permitting the main key of one table to be changed. Because instance, I have a basic thoughtful argument because the main key of a table need to not transform. Keys needs to be naturally constant.

14
2022-06-03 19:42:03
Source

You can watch foreign keys as a restraint that,

  • Help keep information honesty
  • Show just how information is connected to each various other (which can aid in applying organization reasoning and also regulations)
  • If made use of appropriately, can aid increase the performance with which the information is brought from the tables.
1
2022-06-03 19:41:59
Source

I assume some solitary point at some time have to be in charge of making certain legitimate partnerships.

As an example, Ruby on Rails does not make use of foreign keys, yet it confirms all the partnerships itself. If you just ever before accessibility your database from that Ruby on Rails application, this is great.

Nonetheless, if you have various other customers which are contacting the database, after that without foreign keys they require to implement their very own recognition. You after that have 2 duplicates of the recognition code which are more than likely various, which any kind of designer needs to have the ability to inform is a mortal sin.

Then, foreign keys actually are neccessary, as they permit you to relocate the duty to a solitary factor once more.

6
2022-06-03 19:41:57
Source

Yes.

  1. They maintain you straightforward
  2. They maintain new programmers straightforward
  3. You can do ON DELETE CASCADE
  4. They aid you to create wonderful layouts that self clarify the web links in between tables
22
2022-06-03 19:40:34
Source

Foreign keys can additionally aid the designer write much less code making use of points like ON DELETE CASCADE. This suggests that if you have one table having users and also an additional having orders or something, after that removing a customer can instantly delete all orders that indicate that customer.

64
2022-06-03 19:38:57
Source

I can not visualize making a database without foreign keys. Without them, at some point you are bound to slip up and also corrupt the honesty of your information.

They are not called for , purely talking, yet the advantages are massive.

I'm rather particular that FogBugz does not have international key restraints in the database. I would certainly be interested to listen to just how the Fog Creek Software group frameworks their code to assure that they will certainly never ever present an incongruity.

48
2022-06-03 19:38:53
Source

They are not purely essential, in the manner in which seat belts are not purely essential. Yet they can actually conserve you from doing something foolish that screws up your database.

It is a lot better to debug a FK restraint mistake than need to rebuild a delete that damaged your application.

4
2022-06-03 19:38:53
Source

We do not presently make use of foreign keys. And also essentially we do not regret it.

That claimed - we are most likely to start utilizing them a whole lot extra in the future for numerous factors, both of them for comparable factors :

  1. Diagramming. It is a lot less complicated to generate a diagram of a database if there are international key partnerships appropriately made use of.

  2. Device assistance. It is a whole lot less complicated to construct information versions making use of Visual Studio 2008 that can be made use of for LINQ to SQL if there appertain international key partnerships.

So I presume my factor is that we've located that if we are doing a great deal of hand-operated SQL job (construct question, run question, blahblahblah) foreign keys aren't always crucial. As soon as you start getting involved in making use of devices, however, they come to be a whole lot better.

2
2022-06-03 19:35:48
Source