Ken Falk - Software Developer

Login
  My Approach     Services     Contact     Experience     Resume     Blog    
 

Add a Foreign Key Constraint to Existing Table 5/8/2010

Here is the syntax for adding a foreign key constraint (a relationship) on an existing column.

ALTER TABLE TableName ADD CONSTRAINT ConstraintName FOREIGN KEY (ForeignKeyField) REFERENCES ForeignKeyTable (ForeignKeyTableField)

If you want to specify that deleting the parent will cascade down to the child table, add at the very end:

On Delete Cascade

If you want to specify that deleting the parent will set the child table field to null, change the above line to:

On Delete Set NULL