I have a problem related to foreign keys. I am using SQL Server 2008.
There are 2 tables, Customer and Invoice, and they look like this:
Customer table:
CustomerID
Name
Address
Invoice table:
InvoiceID
Date
CustomerID
The CustomerID column in the Customer table is a primary key, and the CustomerID column in the Invoice table has an foreign key.
I want to delete a row in the Customer table, but without deleting the connected row in the Invoice table. Is there a way to do that?
Edit:
I forgot to mention that the deleted customers are being stored in a log table, so the ID would still exist, but in a different table
The whole and entire point of foreign keys is to prevent you from doing this.
The constraint enforces a rule which states that an Invoice must belong to a Customer. So what would it mean in your application if an Invoice didn’t belong to a Customer? To what should it belong instead?
Or to put it in business terms, if an Invoice doesn’t have a Customer who pays it?
Of copurse, my rant assumes that
Invoice.CustomnerIDis a mandatory column. Perhaps your data model allows it to be optional. In which case set teh column to NULL and then you can delete the Customer record. Given the underlying business rule – invoices must be paid (or the company goes bankrupt) I think this would be a flawed data model, but hey!