There are two tables [UserData
] and [HotelData
] I\'ve linked them with a foreign key. which is \"Username
\" and I want to delete which
There is no need to write any code to delete data on the child table when you delete a record from the UserData
table. Just use Cascade
delete behavior. It means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted.
Do the following steps:
Like the following image:
The DELETE statement conflicted with the REFERENCE constraint "FKHotelData"
You have a constraint and this is helping with performance, do yourself a favor and add a column IsDeleted with a default value of 0 or to allow null's so it doesn't affect anything.
Then change your SELECT queries to have a WHERE clause that includes the condition
WHERE IsDeleted = 0
The point here is that its a Hotel business and they will want metrics/reporting on bookings. For medical and confidentiality purposes I'd lean toward the cascading delete answers. For this scenario it would be better to logically delete the data instead of physically delete it.
Well. To expand on the other option which isn't exactly ideal, but should be available as well. CASCADE is easier to work with though.
Without that, your best bet is
DELETE FROM HotelData WHERE Username = @Username
Followed by
DELETE FROM UserData WHERE Username = @Username