| p:abstract
| - In the context of relational databases, a foreign key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. The columns in the referencing table must be the primary key or other candidate key in the referenced table. The values in one row of the referencing columns must occur in a single row in the referenced table. Thus, a row in the referencing table cannot contain values that don't exist in the referenced table (except potentially NULL). This way references can be made to link information together and it is an essential part of database normalization. Multiple rows in the referencing table may refer to the same row in the referenced table. Most of the time, it reflects the one (master table, or referenced table) to many (child table, or referencing table) relationship.
The referencing and referenced table may be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known inas self-referencing or recursive foreign key.
A table may have multiple foreign keys, and each foreign key can have a different referenced table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.
Improper foreign key/primary key relationships or not enforcing those relationships are often the source of many database and data modeling problems. (en)
- Dans une base de données relationnelle, une clé étrangère est une contrainte qui garantit l'intégrité référentielle entre deux tables. Une clé étrangère identifie une colonne ou un ensemble de colonnes d'une table comme référençant une colonne ou un ensemble de colonnes d'une autre table (la table référencée). Ces colonnes de la table référencée doivent faire partie d'une contrainte de clé primaire ou d'une contrainte d'unicité. La contrainte de clé étrangère garantit que les valeurs de chaque ligne de la table référençant existent dans la table référencée : ainsi une ligne de la table référençant ne peut pas contenir un ensemble de valeurs qui n'existe pas dans la table référencée.
Une contrainte de clé étrangère permet ainsi d'établir des liens entre plusieurs tables : il s'agit d'un des principes fondamentaux des bases de données relationnelles.
Exemple : Dans un inventaire, on a des objets, relevant chacun de une parmi n catégories (exemple : meuble, véhicule, ordinateur, ...) (gras = clé primaire italique = clé étrangère)
Objets(objet_id, cat_id, description)
Cats(cat_id, cat)
Si je peux supprimer de cats une catégorie utilisée dans un ou plusieurs objets, alors il n'y a plus intégrité référentielle.
Le SGBD ou le programme doivent m'en empêcher.
MS Access le fait, PostgreSQL le fait, Oracle le fait, MySQL le fait ou pas selon le moteur choisi : avec InnoDB oui, avec les autres non.
Catégorie:Base de données (fr)
- 外部キー (がいぶキー、FK; 英: foreign key) は、コンピュータの関係データベースの関係モデルの文脈において、2つの関係変数 (テーブル) の間の参照整合性制約をいう。
外部キーは、参照する側の関係変数で1つの属性 (列、カラム) もしくは2つ以上の属性の集合を同定し、参照する側の関係変数は、参照される側の関係変数の1つの属性もしくは2つ以上の属性の集合を参照する。
参照する側の関係変数の属性の1つの組 (タプル、行) の値は、参照される側の関係変数の組の値として出現しなければならない。
このため、参照する側の関係変数の組には、参照される側の関係変数に現れない値を、含むことはできない。
このような参照関係は、2つの情報の関連づけるために作成され、関係の正規化の本質的な部分をなしている。
参照する側の関係変数の複数の組が、参照される側の関係変数の同一の組を、参照することができる。
参照する側の関係変数と参照される側の関係変数が、同一の関係変数であっても良い。
すなわちこの場合は外部キーは自分自身の関係変数を参照する。
このような外部キーは、データベース言語標準 SQL:2003 では「自己参照」外部キーあるいは「再帰的」外部キーとして規定されている。
一つの関係変数には複数の外部キーを含むことができる。
そしてこのような外部キーのそれぞれが異なる関係変数を参照する。
各々の外部キーは関係データベース管理システム (RDBMS) によりそれぞれ別個に強制適用される。
このため、関係変数間の連鎖した関連を外部キーを使って定義することができる。
不適切な外部キー/主キー関連の存在や、外部キー/主キー関連の不適切な欠如は、関係データベースおよびデータモデリングについての多くの問題の原因となっていることが多い。 (ja)
- En fremmednøkkel er et felt i en tabell i en relasjonsdatabase som peker til et felt i (vanligvis) en annen tabell. Dette siste feltet er ofte tabellens primærnøkkel. På denne måten kan tabeller kobles sammen, og dette er viktig i databasemodellering.
Et eksempel på dette, er om man har en tabell med bøker, som har ISBN-nummeret til boken som primærnøkkel. Da kan man ha en tabell med forfattere, som er relatert til denne tabellen, gjennom å referer til ISBN-nummeret i tabellen med forfattere. (no)
- Вне́шний ключ — понятие теории реляционных баз данных. Внешним ключом называется поле таблицы, предназначенное для хранения значения первичного ключа другой таблицы с целью организации связи между этими таблицами.
Пусть имеется таблицы A и B. Таблица A содержит поля a, b, c, d, из которых поле a — первичный ключ. Таблица B содержит поля x, y, z. В поле y содержится значение поля a одной из записей таблицы A. В таком случае поле y и называется внешним ключом таблицы A в таблице B.
Вот такой SQL-запрос вернёт все связанные пары записей из таблиц A и B:
select * from A, B where A.a = B.y
Внешний ключ в таблице может ссылаться и на саму эту таблицу. В таких случаях говорят о рекурсивном внешнем ключе. Рекурсивные внешние ключи используются для хранения гомогенной древовидной структуры данных в реляционной таблице.
Развитые СУБД поддерживают автоматический контроль ссылочной целостности на внешних ключах.
Категория:Реляционная модель данных (ru)
|