Show 13.1.20.5 FOREIGN KEY ConstraintsMySQL supports foreign keys, which permit cross-referencing related data across tables, and foreign key constraints, which help keep the related data consistent. A foreign key relationship involves a parent table that holds the initial column values, and a child table with column values that reference the parent column values. A foreign key constraint is defined on the child table. The essential syntax for a defining a foreign key constraint in a
Foreign key constraint usage is described under the following topics in this section:
IdentifiersForeign key constraint naming is governed by the following rules:
Table and column identifiers in a Conditions and RestrictionsForeign key constraints are subject to the following conditions and restrictions:
For information about how the MySQL implementation of foreign key constraints differs from the SQL standard, see Section 1.7.2.3, “FOREIGN KEY Constraint Differences”. Referential Actions When an
For storage engines that support foreign keys, MySQL rejects any
For an As the
default, an For
As of NDB 8.0.16: For A foreign key constraint on a stored
generated column cannot use A foreign key constraint on the base column of a stored generated column cannot use Foreign Key Constraint Examples This simple example relates
This is a more complex example in which a
Adding Foreign Key Constraints You can add a foreign key constraint to an existing table using the following
The foreign key can be self referential (referring to the same table). When you add a foreign key constraint to a table using Dropping Foreign Key Constraints You can drop a foreign key constraint using the following
If the
Adding and dropping a foreign key in the same Foreign Key Checks In MySQL, InnoDB and NDB tables support checking of foreign key constraints. Foreign key checking is
controlled by the The Disabling foreign key checking is useful when:
When
Disabling
LockingMySQL extends metadata locks, as necessary, to tables that are related by a foreign key constraint. Extending metadata locks prevents conflicting DML and DDL operations from executing concurrently on related tables. This feature also enables updates to foreign key metadata when a parent table is modified. In earlier MySQL releases, foreign key metadata, which is owned by the child table, could not be updated safely. If a table is locked explicitly with
Foreign Key Definitions and Metadata To view a foreign key definition, use
You can obtain information about foreign keys from the
You can obtain
information specific to
Foreign Key Errors In the event of a foreign key error involving
Warning If a user has table-level privileges for all parent
tables, An exception is that, for stored programs defined to execute with Apa itu Foreign Key pada MySQL?Foreign Key digunakan untuk menandai suatu tabel terhubung dengan tabel lain dalam konteks tabel parent dan child. Sebuah tabel dikatakan child bila memiliki suatu field yang merupakan reference terhadap key di tabel parent. Hal ini digunakan untuk menjaga konsistensi dan keterkaitan antar tabel.
Bagaimana cara membuat foreign key?Cara Membuat Foreign Key di PHPMyAdmin. Pertama kamu haru membuka tabel yang ingin kamu tambahkan foreign key tersebut dan buka tab 'Structure'. Kamu akan melihat sub menu 'Relation View'.. Pilih kolom yang menampung kunci asing serta tabel yang akan kamu referensikan dengan foreign key tersebut.. Apa itu foreign key dalam database?Dalam SQL database sistem manajamen, foreign key adalah pengenal unik atau kombinasi pengenal unik yang menghubungkan dua tabel atau lebih dalam suatu database. Foreign key juga sering disebut sebagai kunci asing.
Apa itu primary key pada MySQL?Fungsi Primary Key pada MySQL
Kunci utama memungkinkan kamu untuk mengidentifikasi setiap baris di tabel. Hal ini penting dilakukan karena dapat menautkan tabel ke tabel lain yang berkaitan dengan kunci utama sebagai tautannya.
|