Oracle FOREIGN KEY constraints
- A FOREIGN key constraint is defined as having a record of another table to constrain itself. Another table here is the main table.
- When the record of the primary table is deleted, we can delete the record (on delete CASCADE) with the main table, or the corresponding field is set to null (on delete set null), or the deletion is not allowed (default).
A) Note: When the primary table is cascade deleted (Dorp tabel WEN CASCADE CONSTRAINTS), the FOREIGN KEY constraint established from the table associated with the primary table will be deleted and the table data will not change.
-
- It is assumed that the foreign KEY constraint relationship will not be affected if the table is the primary table that corresponds to another table foreign key constraint.
b) Note Two: The Main table field that is pointed from the foreign key constraint must be a unique constraint or a PRIMARY KEY constraint.
-
- Because when a foreign key constraint points to a primary table record with duplicates, deleting one of them is accompanied by an ambiguous operation from the table.
Create a foreign key syntax when creating a table:
Primary[?pra?m?ri] Main, key
FOREIGN[?F?R?N] FOREIGN key
References[?refr?ns]
CONSTRAINT [K?N?STRE?NT]: Constraints, restrictions, coercion
CREATE TABLE WEN
(
MY CHAR (Ten) is not NULL,
Love CHAR (8) is not NULL,
PRIMARY KEY (MY),
CONSTRAINT wen_fk FOREIGN KEY (my,love)
REFERENCES Guoguo (my,love) on delete CASCADE-when the primary table record is deleted, accompany the delete from the table record
)
On DELETE SET NULL;
When the primary table record is deleted, the setting from table data is null, note that the field must be allowed to be null from the fields
Deleting the main table record is not allowed by default when the ON statement is not written.
To append a foreign key when modifying a table:
ALTER TABLE WEN
ADD CONSTRAINT wen_fk FOREIGN KEY (my,love) REFERENCES Guoguo (my,love) on DELETE CASCADE
ALTER TABLE Guoguo ADD CONSTRAINT guoguo_fk FOREIGN KEY (my,love) REFERENCES
WEN (My,love) on DELETE SET NULL
Oracle FOREIGN KEY constraint (Forigen key)