Sql-server – When Creating a Foreign Key Relationship

sql serversql-server-2008

When creating a primary/foreign key relationship there are the following options:

  1. Check Existing Data On Creation [Yes/No]
  2. Enforce for Replication [Yes/No]
  3. Enforce Foreign Key Constraint [Yes/No]

What are the uses for all these when you select 'Yes' or 'No' ?

Because when deleting the record with the primary key, I have a problem if there are any foreign key relationships, it doesn't let me delete. So how do you delete all related records to primary key at that same time? or how do you ignore other foreign records when primary is deleted and allow the delete?

I searched online but can't find the answer to my questions.

Best Answer

What you need to check are the referential integrity constraints.

If you want to delete the parent key there are around four options on how you want to deal with the children keys:

  1. NO ACTION which raises an error and does not delete the parent key (rolls back).
  2. CASCADE which will carry on and delete all the child keys referred by that parent key, leaving no orphans.
  3. SET NULL which will set all the child keys to null values.
  4. SET DEFAULT sets the child keys to the specified default value.