AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL Foreign Key |
|
SQL > Constraint >
Foreign Key
A foreign key is a column (or columns) that references a column (most often the primary key) of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted. A foreign key links a column in one table to a column in another table (usually the primary key), enforcing referential integrity by preventing rows that have no valid parent record. Foreign key columns allow NULL, unlike primary keys.
For example, say we have two tables, a CUSTOMER table that includes all customer data, and an ORDERS table that includes all customer orders. Business logic requires that all orders must be associated with a customer that is already in the CUSTOMER table. To enforce this logic, we place a foreign key on the ORDERS table and have it reference the primary key of the CUSTOMER table. This way, we can ensure that all orders in the ORDERS table are related to a customer in the CUSTOMER table. In other words, the ORDERS table cannot contain information on a customer that is not in the CUSTOMER table. The structure of these two tables will be as follows: Table CUSTOMER
Table ORDERS
In the above example, the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the CUSTOMER table. The following examples are operations that violate the referential integrity of this relationship: When these operations are attempted, the database would return an error stating that referential integrity is violated. It is worth noting that foreign keys allow NULL, while the primary key does not. In addition, a foreign key does not always have to reference a primary key of another table. It can also reference a column that has the UNIQUE constraint. Below we show examples of how to specify the foreign key when creating the ORDERS table: MySQL:
Oracle:
SQL Server:
Below are examples for specifying a foreign key by altering a table. This assumes that the ORDERS table has been created, and the foreign key has not yet been specified: MySQL:
Oracle:
SQL Server:
Composite Foreign KeyA composite foreign key is a foreign key that consists of two or more columns. It is important to note that all the columns in a single foreign key must point to the same table. In other words, it is not possible to have a foreign key that references to a column in Table 1 and a column in Table 2. To illustrate composite foreign key, let's look at the following example: Table INVOICE
Table PAYMENT
Below is CREATE TABLE statement that creates the composite foreign key when creating the PAYMENT table: The above SQL works for MySQL, Oracle, and SQL Server. Specifying the composite key using ALTER TABLE can be done as follows: MySQL:
Oracle:
SQL Server:
Frequently Asked QuestionsWhat is a SQL foreign key?A foreign key is a column (or set of columns) in one table that references a column in another table — typically the primary key — to enforce referential integrity and ensure only valid, existing values are stored. Can a foreign key reference a non-primary key column?Yes. A foreign key can reference any column that has a UNIQUE constraint, not just a primary key column. Can a foreign key column contain NULL values?Yes. Unlike primary keys, foreign key columns allow NULL values. A NULL foreign key simply means the row is not associated with any parent row. What is a composite foreign key?A composite foreign key consists of two or more columns that together reference a composite primary key in another table. All columns in the composite foreign key must point to the same referenced table. |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.