question

arshadmagdum avatar image
arshadmagdum asked

Delete error

i have two tables tblCourse,tblCourseEnquiry tblCourse have column name PK_CourseId and tblCourseEnquiry have FK_CourseId. i have established relationshib between them (PK,FK)so when i try to delete any tblCourse record dey give some error like below The DELETE statement conflicted with the REFERENCE constraint "FK_tblCourseEnquiry_tblCourse". The conflict occurred in database "VInfotech", table "dbo.tblCourseEnquiry", column 'FK_CourseId'. The statement has been terminated.
primary-keyforeign-keyconstraint
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image
Fatherjack answered
This is because deleting the record in tblCourse would leave a record in tblCourseEnquiry 'orphaned', it would have no PK to link to. You will have to delete the tblCourseEnquiry records that have the FK value of the record you want to delete in tblCourse and THEN delete the record from tblCourse
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Håkan Winther avatar image
Håkan Winther answered
Fatherjack is right, but you have a second option. You can alter your foreign key to use cascade delete. That is, you can delete records from the primary table and all corresponding records from the related table will be deleted. It will make it easier to delete records from a table with foreign keys, but it is usually not recommended for a couple of reasons: - You have no control over the delete process. Deleting one record from primary table can cause a delete of millions of records from related table. - If you accidentially delete a record, you will not get an error and all corresponding records will also be deleted. If I spend a little more time, I can probably remember some additional reasons to avoid cascading delets, but time is a bottleneck... :)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.