Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are foreign keys more used in theory than in practice?

When you study relational theory foreign keys are, of course, mandatory. But in practice, in every place I worked, table products and joins are always done by specifying the keys explicitly in the query, instead of relying on foreign keys in the DBMS.

This way, you could of course join two tables by fields that are not meant to be foreign keys, having unexpected results.

Why do you think that is? Shouldn't DBMSs enforce that Joins and Products be made only by foreign keys?

EDIT: Thanks for all the answers. It's clear to me now that the main reason for FKs is reference integrity. But if you design a DB, all relationships in the model (I.E. arrows in the ERD) become Foreign keys, at least in theory, whether or not you define them as such in your DBMS, they're semantically FKs. I can't imagine the need to join tables by fields that aren't FKs. Can someone give an example that makes sense?

PS: I'm aware about the fact that N:M relationships become separate tables and not foreign keys, just omitted it for simplicity's sake.

like image 445
Petruza Avatar asked Dec 09 '09 18:12

Petruza


People also ask

What is the advantage of using foreign key?

The FOREIGN KEY constraint is crucial to relational database design. It lets us link the data according to our needs. As it creates some dependencies between the columns of primary and foreign tables, it also lets us decide what to do ON UPDATE and ON DELETE actions performed on the rows of the primary table.

Why you shouldn't use foreign keys?

Having active foreign keys on tables improves data quality but hurts performance of insert, update and delete operations. Before those tasks database needs to check if it doesn't violate data integrity. This is a reason why some architects and DBAs give up on foreign keys at all.

Is foreign key good practice?

Ideally Entities should not have foreign keys and should be considered masters. So player , dungeon and instance are masters having informations/attributes/fields related to their own features.

When would a foreign key be necessary?

Since the purpose of the foreign key is to identify a particular row of referenced table, it is generally required that the foreign key is equal to the candidate key in some row of the primary table, or else have no value (the NULL value.). This rule is called a referential integrity constraint between the two tables.


2 Answers

The reason foreign key constraints exist is to guarantee that the referenced rows exist.

"The foreign key identifies a column or a set of columns in one table that refers to a column or set of columns in another table. The values in one row of the referencing columns must occur in a single row in the referenced table.

Thus, a row in the referencing table cannot contain values that don't exist in the referenced table (except potentially NULL). This way references can be made to link information together and it is an essential part of database normalization." (Wikipedia)


RE: Your question: "I can't imagine the need to join tables by fields that aren't FKs":

When defining a Foreign Key constraint, the column(s) in the referencing table must be the primary key of the referenced table, or at least a candidate key.

When doing joins, there is no need to join with primary keys or candidate keys.

The following is an example that could make sense:

CREATE TABLE clients (     client_id       uniqueidentifier  NOT NULL,     client_name     nvarchar(250)     NOT NULL,     client_country  char(2)           NOT NULL );  CREATE TABLE suppliers (     supplier_id       uniqueidentifier  NOT NULL,     supplier_name     nvarchar(250)     NOT NULL,     supplier_country  char(2)           NOT NULL ); 

And then query as follows:

SELECT      client_name, supplier_name, client_country  FROM      clients  INNER JOIN     suppliers ON (clients.client_country = suppliers.supplier_country) ORDER BY     client_country; 

Another case where these joins make sense is in databases that offer geospatial features, like SQL Server 2008 or Postgres with PostGIS. You will be able to do queries like these:

SELECT     state, electorate  FROM      electorates  INNER JOIN      postcodes on (postcodes.Location.STIntersects(electorates.Location) = 1); 

Source: ConceptDev - SQL Server 2008 Geography: STIntersects, STArea

You can see another similar geospatial example in the accepted answer to the post "Sql 2008 query problem - which LatLong’s exists in a geography polygon?":

SELECT      G.Name, COUNT(CL.Id) FROM     GeoShapes G INNER JOIN      CrimeLocations CL ON G.ShapeFile.STIntersects(CL.LatLong) = 1 GROUP BY      G.Name; 

These are all valid SQL joins that have nothing to do with foreign keys and candidate keys, and can still be useful in practice.

like image 170
12 revs Avatar answered Oct 13 '22 21:10

12 revs


Foreign keys have less to do with joins than with keeping database integrity. Proof of that is that you can join tables in any way you want, even in ways that don't necessarily make sense.

like image 32
Otávio Décio Avatar answered Oct 13 '22 23:10

Otávio Décio