Databases – Primary and Foreign Keys

database tutorial

Sharing is caring!

As an important component of the data model, integrity constraints specifications, such as entity integrity and referential integrity, receive a lot of support, from the relational model. According to, a database must be able to maintain the correct relationship, which exists between tables and data; therefore, entity integrity and referential integrity is very important to help maintain these relationships. Database models contain all types of keys, which are used to make the relations and the types of relationships, which exist between data and tables.

In order to ensure that the data has the correct references, the referential integrity rule is put in effect. This means that foreign keys in one table, must be valid and match the primary keys, of the other table, in order to maintain referential integrity.  The notion that a database should be actively upgraded, in order to ensure its quality, is a fundamental principle of Referential integrity. It is important to maintain the quality of a database, because SQL queries, may fail when there is no match between the relationships of data keys. When bad data is entered into a table, which has keys that provide information to another table, their relationship can be lost. Foreign key values are validated through referential integrity rules, so that a database, for example, that has to store MAC address of a video phone on a table as its primary keys, and connect those MAC address to a customer’s table, which has stored MAC addresses, as foreign keys; therefore, the MAC addresses on the video phone’s table, would have to match the MAC addresses on the customer’s table, in order for SQL queries, to work properly, when searching through the data.

Entity integrity is another rule provided by the relational database model, which states that no primary key shall be null, and shall always be unique. Unique and valid data can only be entered in the column, which is identified as the primary key, or else SQL queries will fail, due to the entity integrity rule, provided by relational database models. The example below is an illustration of a record, which could not be entered into the video phone’s table because it didn’t have a valid MAC address.

The structure and semantics of databases are very well understood, through the keys of entities, which play an important role, in designing the schema of a table. The different keys that a table can have, are the following:

Primary keys: identify each record on a row of a table column as unique, and does not allow the value to be null. When records on a table need to be identified, one by one uniquely, then primary keys are used, which can help database administrators and system analysts, to increase the level of efficacy that exist in the database search results. The search and collection of data are also made efficiently when primary keys are used.

Foreign keys: are used in order to be able to identify a unique record, which exists between two tables, the foreign key is usually the primary key in one of the tables, in order to link two tables together. Therefore, records in one table, are dependent on the records of another table, which are known as the primary key, in order to make to correct match. Foreign keys are one of the most important classes of relationships, which exist between the data of a table.