Referential integrity

From Wikipedia, the free encyclopedia

An example of a database that has not enforced referential integrity. In this example, there is a foreign key (artist_id) value in the album table that references a non-existent artist — in other words there is a foreign key value with no corresponding primary key value in the referenced table. What happened here was that there was an artist called "Aerosmith", with an artist_id of "4", which was deleted from the artist table. However, the album "Eat the Rich" referred to this artist. With referential integrity enforced, this would not have been possible.
An example of a database that has not enforced referential integrity. In this example, there is a foreign key (artist_id) value in the album table that references a non-existent artist — in other words there is a foreign key value with no corresponding primary key value in the referenced table. What happened here was that there was an artist called "Aerosmith", with an artist_id of "4", which was deleted from the artist table. However, the album "Eat the Rich" referred to this artist. With referential integrity enforced, this would not have been possible.

Referential integrity in a relational database is consistency between coupled tables. Referential integrity is usually enforced by the combination of a primary key or candidate key (alternate key) and a foreign key. For referential integrity to hold, any field in a table that is declared a foreign key can contain only values from a parent table's primary key or a candidate key. For instance, deleting a record that contains a value referred to by a foreign key in another table would break referential integrity. The relational database management system (RDBMS) enforces referential integrity, normally either by deleting the foreign key rows as well to maintain integrity, or by returning an error and not performing the delete. Which method is used would be defined by the definition of the referential integrity constraint.

[edit] Example

An employee database stores the department in which each employee works. The field "DepartmentNumber" in the Employee table is declared a foreign key, and it refers to the field "Index" in the Department table which is declared a primary key. Referential integrity would be broken by deleting a department from the Department table if employees listed in the Employee table are listed as working for that department.

[edit] See also


Topics in database management systems (DBMS)view  talk  edit )

Concepts
Database • Database model • Relational database • Relational model • Relational algebra • Primary key, Foreign key, Surrogate key, Superkey, Candidate key • Database normalization • Referential integrity • Relational DBMS • Distributed DBMS • ACID

Objects
Trigger • View • Table • Cursor • Log • Transaction • Index • Stored procedure • Partition

Topics in SQL
Select • Insert • Update • Merge • Delete • Join • Union • Create • Drop

Implementations of database management systems

Types of implementations
Relational • Flat file • Deductive • Dimensional • Hierarchical • Object oriented • Temporal • XML data stores

Components
Query language • Query optimizer • Query plan • ODBC • JDBC

Database products

Apache Derby • Berkeley DB • Caché • DB2 • db4o • DBase • eXtremeDB • Filemaker Pro • Firebird • Greenplum • H2 • Hsqldb • Helix • Informix • Ingres • InterBase • Linter • Microsoft Access • Microsoft SQL Server • Mimer SQL • MonetDB • MySQL • Objectivity/DB • OpenLink Virtuoso • OpenOffice.org Base • Oracle • Oracle Rdb • Paradox • Perst • PostgreSQL • SQLite • Sybase IQ • Sybase • Teradata • UniVerse • Visual FoxPro


Other: Object-oriented (comparison) • relational (comparison)