Foreign key operations include adding a foreign key and dropping a foreign key.
Add a foreign key constraint
The syntax for adding a foreign key constraint is as follows:
ALTER TABLE table_name ADD CONSTRAINT fk_name FOREIGN KEY(column_name)
REFERENCES table_name(column_name);
The following example shows how to add a foreign key constraint:
obclient> CREATE TABLE t1(c1 INT,c2 VARCHAR(50));
Query OK, 0 rows affected
obclient> CREATE TABLE t2(c1 INT PRIMARY KEY,c2 VARCHAR(50));
Query OK, 0 rows affected
obclient> ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY(c1) REFERENCES t2(c1);
Query OK, 0 rows affected
Drop a foreign key
The syntax for dropping a foreign key constraint is as follows:
ALTER TABLE table_name DROP FOREIGN KEY fk_name;
The following example shows how to drop a foreign key constraint:
obclient> ALTER TABLE t1 DROP FOREIGN KEY fk1;
Query OK, 0 rows affected