What is difference between foreign key and reference key?

SqlDatabaseForeign Keys

Sql Problem Overview


I am very confused about those two terms. Are they the same or different?

Some books and people say they are the same and others say they are different.

I tried but couldn't find a conclusive answer.

Sql Solutions


Solution 1 - Sql

I am supposing that you are talking about using the REFERENCES where the FOREIGN KEY keyword is not used when constraining a column inline, which is called a column-level foreign key constraint, eg.

author_id INTEGER REFERENCES author(id)

... instead of the table-level foreign key constraint, which is placed after the column declarations ...

author_id INTEGER,
FOREIGN KEY(author_id) REFERENCES author(id)

The answer is, that it is simply shorthand syntax for the same thing. The main concern when altering between the two should be readability.

For more advanced use, it might be relevant that only table-level foreign key constraints can describe constraints on multiple keys at once, where all must be present in the referenced table.


Do note that MySQL 'parses but ignores “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification', meaning that only the table-level foreign key constraint will work.

Both Postgres and Microsoft's SQL Server respect both column- and table-level foreign key constraints.

Solution 2 - Sql

A foreign key must refer to a primary key. When using REFERENCES constraint simply, then it isn't necessary that the referenced key be a primary key.

Solution 3 - Sql

"Reference key" isn't a normal technical term in relational modeling or in SQL implementation in US English.

A foreign key "references" a key in some other table; could that be where the confusion comes from?

Solution 4 - Sql

You don't really call something a reference key... They are the same thing... you might see the word references used for example in sqlite: you might use syntax like this to start a db of authors and books. This lets you show that one author can have many books. This tells the db that the books.author_id (defined a couple of lines up) references author.id

CREATE TABLE 'author' (
	id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
	firstname varchar(255)
    lastname varchar(255)
);

CREATE TABLE 'books' (
	id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
	author_id INTEGER,
	title varchar(255),
	published date,
	FOREIGN KEY(author_id) REFERENCES author(id)
);

Solution 5 - Sql

In terms of standard SQL, both result in a foreign key constraint.

One form is a table constraint, meaning it can apply to one or more columns. You would need this to reference a table that has a multi-column primary key:

CREATE TABLE child (
    id int PRIMARY KEY,
    parent_id int,
    date date,
    FOREIGN KEY (parent_id, date) REFERENCES parent(id, date)
);

The other form is a column constraint, meaning it can only apply to the single column it is defined with. It cannot be used to reference a table with a multi-column primary key.

CREATE TABLE child (
    id int PRIMARY KEY,
    parent_id int REFERENCES parent(id)
);

The above syntax works exactly the same as if you declared a table constraint for a single column (supposing the RDBMS supports this type of column constraint), as follows:

CREATE TABLE child (
    id int PRIMARY KEY,
    parent_id int,
    FOREIGN KEY (parent_id) REFERENCES parent(id)
);

It frequently causes confusion for users of MySQL and its InnoDB storage engine, that the latter column-constraint style is not supported. You must define a table-level constraint for a foreign key, even if it is a single-column constraint. This has been a strange behavior of MySQL since its earliest days, that some constraint syntax is valid, but results in no constraint. See discussion here: https://bugs.mysql.com/bug.php?id=17943

Solution 6 - Sql

The only and most important difference between the two keywords 'FOREIGN KEY" and "REFERENCES" keywords is though both of them make the data to be child data of the parent table, the "FOREIGN KEY" is used to create a table level constraint whereas REFERENCES keyword can be used to create column level constraint only. Column level constraints can be created only while creating the table only. But table level constraints can be added using ALTER TABLE command.

Solution 7 - Sql

Perhaps you are using the term "reference key" somewhat loosely?

A foreign key value in one row is said to "reference" the row that contains the corresponding key value. Note the word "reference" in the prior sentence is a verb, so we may say we have a referencing foreign key value and a referenced key value.

Although it is the key values, rather than the table key constraint, that is being referenced, I suppose loosely speaking we could say "referenced key" to mean the rows that comprise the values that may potentially be referenced. I then see how "referenced key" could become "referenced key" but not belie its origin.

Solution 8 - Sql

There are 2 ways to declare a foreign key(s):

  1. if the foreign key is a SINGLE attribute:
    REFERENCES

    ()

  2. if foreign keys are a LIST of attributes

  3. FOREIGN KEY () REFERENCES

Solution 9 - Sql

A foreign key "references" a key in some other table. That key in some other table is called Referenced key. You'll probably hear a lot about this if you're using Graphic feature on phpmyadmin.

Solution 10 - Sql

The Reference Key is the primary key that is referenced in the other table. On the other hand, Foreign Key is how you link the second table to the primary tables Primary Key (or Reference Key).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndersonView Question on Stackoverflow
Solution 1 - SqlNiels AbildgaardView Answer on Stackoverflow
Solution 2 - SqlalphaView Answer on Stackoverflow
Solution 3 - SqlMike Sherrill 'Cat Recall'View Answer on Stackoverflow
Solution 4 - SqlmaneeshaView Answer on Stackoverflow
Solution 5 - SqlBill KarwinView Answer on Stackoverflow
Solution 6 - Sqluser7901336View Answer on Stackoverflow
Solution 7 - SqlonedaywhenView Answer on Stackoverflow
Solution 8 - SqlSebastian NedumalaView Answer on Stackoverflow
Solution 9 - SqlMatt JuniorView Answer on Stackoverflow
Solution 10 - SqlNazmus ShakibView Answer on Stackoverflow