Skip to content

Instantly share code, notes, and snippets.

@iamvery
Created January 5, 2011 16:16
Show Gist options
  • Save iamvery/766524 to your computer and use it in GitHub Desktop.
Save iamvery/766524 to your computer and use it in GitHub Desktop.
Allows foreign key to be deferred during a transaction, but it will be applied immediately if not deferred.
ALTER TABLE "TableName"
ADD CONSTRAINT "ConstraintName" FOREIGN KEY ("columnName")
REFERENCES "AnotherTableName" ("anotherColumnName") MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
-- I generally use the form "TableName_fk_constraintname" for foreign key constraint names (for reference)
-- This particular foreign key will cascade on update and delete, but those particular directives aren't required. (for reference)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment