What is a foreign key?
The
database consists of tables where data or information is stored. A foreign key is a mechanism in a relational database that allows the association of these tables or for tables to have a relationship. There are unique features that must have a foreign key. It must be a column or group of columns in a table whose values create a relationship with values in another table in the same database.
Foreign key ensures that rows in one table correspond to rows in another table, establishing relations with the entire database or links. A table containing a foreign key is a "child" and the second table is "parent". It is possible that the value of a foreign key is zero or empty and to actually refer to or relate to what is known as the primary key of the table in which it is found. This is known as self -reference.
You must understand what the primary key is before you try to fully understand the concept of foreign keys. For example, if they are in the database by storing information aboutPublished books for the web that sells books can be one table that could be a table of publishers and a second table of books. The table of publishers could consist of two columns, the primary key, which is always unique for each record and column of the publisher name. At least three columns would form a table of books to hold the primary key or a unique identifier for each book, book titles, and a column to establish a relationship with the publisher table. This column would be a foreign key.
The primary key for the publisher table could be something like "PUB_ID" with values: P01, P02, P03, etc. The relationship would be created with a book table if the PUB_ID column with the same values. This would be a foreign key to this child table back with the parent - the publisher table.
However, the foreign key may have a column name other than the superior key to which it refers. Sound understanding of the functioning of primary and abroad is necessary to maintain reference integrityripping keys. Very large databases, which are composed of many tables or which include a connection table, represent more than one type of relationship, which can complicate the task of working with foreign keys.