A foreign key specifies that the value of a column (or a group of columns) must conform to the values of some rows in another table. We say this is to maintain the referential integrity of the associated table.
In the graphical interface, the Foreign key tab simply clicks on the Outer key field to edit. Use the Foreign key toolbar to allow you to create new, edit, or Delete selected foreign key fields.
Add foreign Key: Add a foreign key to the table.
Delete foreign key: Deletes the foreign key that has been selected.
Use the name edit box to enter the name of the new key.
Select an external index database, table, and restriction by using the reference mode, reference table, and reference restriction Drop-down list.
To include a field to the key, simply double-click the Field field or click to open the editor for editing.
Deletes the current Drop-down list definition of the type of action taken.
No action: This is the default action. The reference key value will not be updated or deleted.
CASCADE: Deletes any rows that refer to the deleted row or updates the new value of the referenced column, respectively.
Set NULL: Set reference column null.
Enable
You can select whether to enable or disable the foreign key limit by checking or canceling the check box.
Here's a formal look at the method created using SQL statements:
1, the default way to create
when the referenced data is deleted, it cannot be deleted.
CREATE TABLE t_invoice
(ID number () not NULL,
invoice_no VARCHAR2 (=) NOT null,
CONSTRAINT pk_invoice_id PRIMARY KEY (ID));
CREATE TABLE t_invoice_detail
(ID number () not NULL, AMOUNT number (
10,3),
PIECE number (a),
INVOICE _id number (a),
CONSTRAINT pk_detail_id PRIMARY KEY (ID));
ALTER TABLE t_invoice_detail
ADD CONSTRAINT fk_invoice_id
FOREIGN KEY (invoice_id) REFERENCES t_invoice (ID);
2, Cascade delete
The foreign key syntax has an option to specify cascading deletion features. This feature only makes deletion statements for the parent table. With this option, a delete operation of the parent table will automatically delete all related child table records
ALTER TABLE t_invoice_detail
ADD CONSTRAINT fk_invoice_id
FOREIGN KEY (invoice_id) REFERENCES T_invoice (ID) C18/>on DELETE CASCADE;
If you cannot cascade deletes, you can set the foreign key field value of the child table to NULL, using the ON DELETE SET NULL statement (the Foreign key field cannot set the NOT NULL constraint).
ALTER TABLE t_invoice_detail
ADD CONSTRAINT fk_invoice_id
FOREIGN KEY (invoice_id) REFERENCES T_invoice (ID) On
DELETE SET NULL;
III. Reference field syntax structure
Creating a FOREIGN KEY constraint is that the foreign key field references the parent table's primary KEY or UNIQUE constraint field. In this case, you can not specify a foreign key reference field name, as follows:
ALTER TABLE t_invoice_detail
ADD CONSTRAINT fk_invoice_id
FOREIGN KEY (invoice_id) REFERENCES T_invoice;
When no reference field is specified, the default reference field is the primary key of the parent table.
If the foreign key field references a unique rather than primary key field, you must specify the field name in the Add constraint statement.