Foreign Key Clause
- Last Updated: May 15, 2020
- 1 minute read
- DataDirect Connectors
- JDBC
- IBM Db2 5.1
- MySQL 5.1
- Progress OpenEdge 5.1
- SAP Sybase 5.1
- Documentation
Purpose
Specifies a foreign key for a constraint.
Syntax
FOREIGN KEY (fcolumn_name)
REFERENCES ref_table (pcolumn_name)
where:
- fcolumn_name
- specifies the foreign key column to which the constraint is applied. The data type of this column must be the same as the data type of the column it references.
- ref_table
- specifies the table to which the foreign key refers.
- pcolumn_name
- specifies the primary key column in the referenced table. For Salesforce, the primary key column is always the rowId column.
Example
Assuming the current schema is SFORCE, the remote table emp is created
with the name, empId, and deptId columns.
The table contains a foreign key constraint on the deptId column,
referencing the rowId in the dept table created in Example C. For the operation to succeed, the data type of the
deptId column must be the same as that of the rowId
column.
CREATE TABLE emp (name TEXT(30), empId NUMBER(9, 0) EXT_ID, deptId
TEXT(18), FOREIGN KEY(deptId) REFERENCES dept(rowId))