Foreign key referencing of two DB2 tables

DB2DatabaseBig Data Analytics

A foreign key is a column in a table that establishes a referential link with another table. A foreign key can be defined during creation of table (CREATE TABLE command) or it can be defined by modifying the table (ALTER TABLE command). However, before defining any key as foreign key, make sure that an index is built up on that column. We can use the below command to define an existing column CLASS in table TAB1 as a foriegn key which links to table TAB2.

ALTER TABLE TAB1
ADD FOREIGN KEY (CLASS) REFERENCES CLASSDATA (CLASS_ID);

The ALTER TABLE reserved words are followed by the name of the table which has to be modified.

Then we will use the ADD FOREIGN KEY parameter to define the name of the column which has to be defined as foreign key. Which is CLASS in this case.

At last we have to use a REFERENCES reserved word followed by the name of the database and corresponding column name which needs to be linked.

raja
Published on 12-Sep-2020 13:18:09
Advertisements