In OceanBase Database, you can add and drop foreign keys.
Add a foreign key
The syntax for adding a foreign key is as follows:
ALTER TABLE table_name ADD CONSTRAINT constraint_name
FOREIGN KEY(foreign_columns) REFERENCES reference_table(column_name);
Here is an example:
obclient> CREATE TABLE primary_table (id NUMBER PRIMARY KEY, names VARCHAR(100) NOT NULL, foreign_col NUMBER);
Query OK, 0 rows affected
obclient> CREATE TABLE reference_table (id NUMBER PRIMARY key, comments VARCHAR2(100) NOT NULL);
Query OK, 0 rows affected
obclient> ALTER TABLE primary_table ADD CONSTRAINT cons_fk1 FOREIGN KEY(foreign_col) REFERENCES reference_table(id);
Query OK, 0 rows affected
Drop a foreign key
The syntax for dropping a foreign key is as follows:
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
Here is an example:
obclient> ALTER TABLE primary_table DROP CONSTRAINT cons_fk1;
Query OK, 0 rows affected