Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between check and foreign key?

Tags:

sql

oracle

i am quite confused about the difference between a FOREIGN KEY and CHECK constraint - they appear to me to achieve the same result.

I mean I could create a table and enforce a Foreign key on another table, but i could create a CHECK to ensure the value in in another table.

What is the difference and when to use the one or the other?

like image 832
GigaPr Avatar asked Apr 11 '10 17:04

GigaPr


People also ask

What is foreign key check?

A Foreign Key is a database key that is used to link two tables together. The FOREIGN KEY constraint identifies the relationships between the database tables by referencing a column, or set of columns, in the Child table that contains the foreign key, to the PRIMARY KEY column or set of columns, in the Parent table.

What is the difference between foreign key and?

A primary key is used to ensure data in the specific column is unique. A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It uniquely identifies a record in the relational database table.

What is the foreign key used for?

A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables to control the data that can be stored in the foreign key table.

What is an example of a foreign key?

In simpler words, a foreign key is a set of attributes that references a candidate key. For example, a table called TEAM may have an attribute, MEMBER_NAME, which is a foreign key referencing a candidate key, PERSON_NAME, in the PERSON table.


2 Answers

A FOREIGN KEY constrain ensures that the entry DOES EXISTS in

EDIT another table

as per correct comment Exists in another table... or the same table. – Mark Byers

A CHECK constrain ensures that the entry follows some rule.

CHECK Constraints

CHECK constraints enforce domain integrity by limiting the values that are accepted by a column. They are similar to FOREIGN KEY constraints in that they control the values that are put in a column. The difference is in how they determine which values are valid: FOREIGN KEY constraints obtain the list of valid values from another table, and CHECK constraints determine the valid values from a logical expression that is not based on data in another column.

like image 179
Adriaan Stander Avatar answered Oct 06 '22 00:10

Adriaan Stander


A foreign key constraint is more powerful than a CHECK constraint.
A foreign key constraint means that the column (in the current table) can only have values that already exist in the column of the foreign table (which can include the be the same table, often done for hierarchical data). This means that as the list of values changes - gets bigger or smaller - there's no need to update the constraint.

A check constraint can not reference any columns outside of the current table, and can not contain a subquery. Often, the values are hard coded like BETWEEN 100 and 999 or IN (1, 2, 3). This means that as things change, you'll have to update the CHECK constraint every time. Also, a foreign key relationship is visible on an Entity Relationship Diagram (ERD), while a CHECK constraint will never be. The benefit is that someone can read the ERD and construct a query from it without using numerous DESC table commands to know what columns are where and what relates to what to construct proper joins.

Best practice is to use foreign keys (and supporting tables) first. Use CHECK constraints as a backup for situations where you can't use a foreign key, not as the primary solution to validate data.

like image 39
OMG Ponies Avatar answered Oct 05 '22 23:10

OMG Ponies