41.7 Foreign Keys
The InnoDB tables support Foreign Keys.
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. Foreign Keys are used to link information together and it is an essential part of database normalization.
Creating a Foreign Key
To create a Foreign Key:
- open the ''Server Object List'' (chap. 16);
- expand the ''Tables'' node relating the database which contains the table you want to create the key in;
- double click on the table name;
- click on the ''Foreign Keys'' Tab of the ''Table Editor'' (sec. 41.4);
- click the ''Add Foreign Key'' button (or right click and select the ''Add Foreign Key'' item from the popup menu);
- use the ''Foreign Key Editor'' (sec. 41.7.1).
Editing a Foreign Key
To edit a Foreign Key:
- open the ''Server Object List'' (chap. 16);
- expand the ''Tables'' node relating the database which contains the table which contains the key you want to edit;
- double click on the table name;
- click on the ''Foreign Keys'' Tab of the ''Table Editor'' (sec. 41.4);
- in the keys' grid select the row relating the key you want to edit;
- click the ''Edit selected Foreign Key'' button (or double click or right click and select the ''Edit Foreign Key'' item from the popup menu);
- use the ''Foreign Key Editor'' (sec. 41.7.1).
Dropping a Foreign Key
To drop a Foreign Key:
- open the ''Server Object List'' (chap. 16);
- expand the ''Tables'' node relating the database which contains the table you want to drop the key from;
- double click on the table name;
- click on the ''Foreign Keys'' Tab of the ''Table Editor'' (sec. 41.4);
- in the keys' grid select the row relating the key you want to drop;
- click on the ''Drop selected Foreign Key'' button (or right click and select the ''Drop Foreign Key'' item from the popup menu);
- confirm the requested operation.
Multi-Drop of Foreign Keys
To drop a set of Foreign Keys of a Table:
- open the ''Server Object List'' (chap. 16);
- expand the table node relating the database which contains the table you want to drop the keys from;
- right click on the ''Foreign Keys'' node and select the ''Multi-Drop of Foreign Keys'' item from the popup menu;
- use the ''Multi-Drop Wizard'' (chap. 26).
41.7.1 Foreign Keys Editor
The
Foreign Keys Editor allows you to create and edit foreign keys. The caption of the Editor reports if you are adding or editing a key.
The ''Editor'' (Fig. 41.53) can be opened from the Create Table Wizard, from the Table Editor, or using the popup menu of the Server Objects List (see the introduction of the sec. 41.7).
See Also:
Figure 41.53:
The Foreign Keys Editor.
|
|
41.7.1.1 Parent Table
Choose a name for the key (if you leave ''Default name'' MySQL
TM will assign a name for you) and select the fields of the foreign key (Fig.
41.54).
Figure 41.54:
Foreign Keys: parent table fields.
|
|
41.7.1.2 Child Table
Choose a database, a table and the fields of the ''child table'' (Fig.
41.55).
Figure 41.55:
Foreign Keys: child table fields.
|
|
41.7.1.3 Action Rule
Choose the action rule to be used when updating or deleting records (Fig.
41.56).
Figure 41.56:
Foreign Keys: action rule.
|
|
Copyright © 2007 - 2009 HoneySoftware - All Rights Reserved
Submit feedback on this topic.