Skip to main content

Foreign Key Operations

In seekdb, you can add and drop foreign key constraints.

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);

Here is an example of adding a foreign key constraint:

CREATE TABLE t1(c1 INT,c2 VARCHAR(50));
Query OK, 0 rows affected (0.002 sec)

CREATE TABLE t2(c1 INT PRIMARY KEY,c2 VARCHAR(50));
Query OK, 0 rows affected (0.002 sec)

ALTER TABLE t1 ADD CONSTRAINT fk1 FOREIGN KEY(c1) REFERENCES t2(c1);
Query OK, 0 rows affected (0.007 sec)

Drop a foreign key constraint

The syntax for dropping a foreign key constraint is as follows:

ALTER TABLE table_name DROP FOREIGN KEY fk_name;

Here is an example of dropping a foreign key constraint:

ALTER TABLE t1 DROP FOREIGN KEY fk1;
Query OK, 0 rows affected (0.002 sec)