To add a PRIMARY KEY constraint:
ALTER TABLE ga_airline ADD CONSTRAINT pk_airline_id PRIMARY KEY (airline_id);
There are three types of foreign KEY constraints:
1. Normal FOREIGN KEY constraint (cannot delete parent table record if there is a child table reference parent table primary key)
2. Cascading foreign KEY constraint (delete the parent table record that has references, and also delete all referenced child table records)
3. Empty FOREIGN KEY constraint (you can delete the parent table record that has a reference, and the foreign key field that references the parent table's primary key in the child table is automatically set to null, but the field should allow null values)
The syntax for establishing these three foreign key constraints is as follows:
For example, there are two table parent table T_INVOICE
Primary key IDs. Child tableT_INVOICE_DETAIL外键字段INVOICE_ID
1, ordinary foreign KEY constraints:
ALTER
TABLE
T_INVOICE_DETAIL
ADD
CONSTRAINT
FK_INVOICE_ID
FOREIGN
KEY
(INVOICE_ID )
REFERENCES
T_INVOICE(ID);
2. Cascading FOREIGN KEY constraints:
ALTER
TABLE
T_INVOICE_DETAIL
ADD
CONSTRAINT
FK_INVOICE_ID
FOREIGN
KEY
(INVOICE_ID )
REFERENCES
T_INVOICE(ID)
ON
DELETE
CASCADE
;
3, empty foreign KEY constraints:
ALTER
TABLE
T_INVOICE_DETAIL
ADD
CONSTRAINT
FK_INVOICE_ID
FOREIGN
KEY
(INVOICE_ID )
REFERENCES
T_INVOICE(ID)
ON
DELETE
SET
NULL
;
Adding FOREIGN KEY constraints in Oracle tables