Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Referential integrity

From Wikipedia, the free encyclopedia
Where all data references are valid
An example of a database that has not enforcedreferential 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 aforeign key value with no correspondingprimary key value in the referenced table. What happened here was that there was an artist called "Aerosmith", with anartist_id of4, 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 is a property of data stating that all its references are valid. In the context ofrelational databases, it requires that if a value of one attribute (column) of arelation (table) references a value of another attribute (either in the same or a different relation), then the referenced value must exist.[1]

For referential integrity to hold in a relational database, any column in a basetable that is declared aforeign key can only contain either null values or values from a parent table'sprimary key or acandidate key.[2] In other words, when a foreign key value is used it must reference a valid, existing primary key in the parent table. For instance, deleting a record that contains a value referred to by a foreign key in another table would break referential integrity. Somerelational database management systems (RDBMS) can enforce 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 may be determined by a referential integrity constraint defined in adata dictionary.

The adjective 'referential' describes the action that aforeign key performs, 'referring' to a linked column in another table. In simple terms, 'referential integrity' guarantees that the target 'referred' to will be found. A lack of referential integrity in a database can lead relational databases to return incomplete data, usually with no indication of an error.

Formalization

[edit]

Aninclusion dependency over two (possibly identical) predicatesR{\displaystyle R} andS{\displaystyle S} from a schema is writtenR[A1,...,An]S[B1,...,Bn]{\displaystyle R[A_{1},...,A_{n}]\subseteq S[B_{1},...,B_{n}]}, where theAi{\displaystyle A_{i}},Bi{\displaystyle B_{i}} are distinct attributes (column names) ofR{\displaystyle R} andS{\displaystyle S}. It implies that the tuples of values appearing in columnsA1,...,An{\displaystyle A_{1},...,A_{n}} for facts ofR{\displaystyle R} must also appear as a tuple of values in columnsB1,...,Bn{\displaystyle B_{1},...,B_{n}} for some fact ofS{\displaystyle S}.

Such constraint is a particular form oftuple-generating dependency (TGD) where in both the sides of the rule there is only one relational atom.[3] Infirst-order logic it is expressible asx,y.(R(x,y)z.S(x,z)){\displaystyle \forall {\vec {x}},{\vec {y}}.(R({\vec {x}},{\vec {y}})\rightarrow \exists {\vec {z}}.S({\vec {x}},{\vec {z}}))}, wherex{\displaystyle {\vec {x}}} is the vector (whose size isn{\displaystyle n}) of variables shared byR{\displaystyle R} andS{\displaystyle S}, and no variable appears multiple times neither in the TGD's body nor in its head.

Logical implication between inclusion dependencies can be axiomatized by inference rules[4]: 193 and can bedecided by aPSPACE algorithm. The problem can be shown to bePSPACE-complete by reduction from the acceptance problem for alinear bounded automaton.[4]: 196  However, logical implication between dependencies that can be inclusion dependencies orfunctional dependencies is undecidable by reduction from theword problem formonoids.[4]: 199 

Declarative referential integrity

[edit]

Declarative referential integrity (DRI) is one of the techniques in theSQL database programming language to ensure data integrity.

Meaning in SQL

[edit]
Main article:Foreign key

A table (called the referencing table) can refer to a column (or a group of columns) in another table (the referenced table) by using aforeign key. The referenced column(s) in the referenced table must be under a unique constraint, such as aprimary key. Also, self-references are possible (not fully implemented in MS SQL Server though[5]). Oninserting a newrow into the referencing table, therelational database management system (RDBMS) checks if the entered key value exists in the referenced table. If not, no insert is possible. It is also possible to specify DRI actions onUPDATE andDELETE, such as CASCADE (forwards a change/delete in the referenced table to the referencing tables), NO ACTION (if the specific row is referenced, changing the key is not allowed) or SET NULL / SET DEFAULT (a changed/deleted key in the referenced table results in setting the referencing values to NULL or to the DEFAULT value if one is specified).[6]

Product-specific meaning

[edit]

InMicrosoft SQL Server the term DRI also applies to the assigning of permissions to users on adatabase object. Giving DRI permission to a database user allows them to add foreign key constraints on a table.[7]

See also

[edit]

References

[edit]
  1. ^Chapple, Mike."Referential Integrity".About.com Tech. About.com. Archived fromthe original on 2004-09-25. Retrieved2011-03-20.Definition: Referential integrity is a database concept that ensures that relationships between tables remain consistent. When one table has a foreign key to another table, the concept of referential integrity states that you may not add a record to the table that contains the foreign key unless there is a corresponding record in the linked table.
  2. ^Coronel et al. (2013). Database Systems 10th ed. Cengage Learning,ISBN 978-1-111-96960-8
  3. ^Kolaitis, Phokion G."A Tutorial on Database Dependencies"(PDF). University of California Santa Cruz & IBM Research - Almaden. Archived fromthe original(PDF) on 2021-12-10. Retrieved2021-12-10.
  4. ^abcAbiteboul, Serge; Hull, Richard B.;Vianu, Victor (1994). "9. Inclusion Dependency".Foundations of Databases. Addison-Wesley. pp. 192–199.
  5. ^Microsoft Support (2007-02-11)."Error message 1785 occurs when you create a FOREIGN KEY constraint that may cause multiple cascade paths". microsoft.com. Retrieved2009-01-24.
  6. ^ANSI/ISO/IEC 9075-1:2003, Information technology—Database languages—SQL
    • Part 1: Framework (SQL/Framework)
    • Part 2: Foundation (SQL/Foundation)
  7. ^Chigrik, Alexander (2003-08-13)."Managing Users Permissions on SQL Server". Database Journal. Retrieved2006-12-17.

External links

[edit]
Types
Concepts
Objects
Components
Functions
Related topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Referential_integrity&oldid=1308130831"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp