0

I have a SQL table called "user" and a table "login" that has a foreign key constraint to a user. I want to be able to delete a row in the user table, even if there are login rows that reference it. Right now the database stops me from doing this.

Does anyone know how I can alter the table (through SQL or preferably through PHPmyAdmin to allow me to do this?

The tables were created automatically through Django.

Edit: To clarify: I don't want to cascade the delete. That is, I want the rows in the Login table to remain even though the user they reference is gone.

9
  • dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html, see dropping foreign key section
    – koriander
    Feb 27, 2015 at 20:26
  • 2
    You use a foreign key and you ask why does it behaves like a foreign key...Why do you need constraint integrity if you dont care about it?Just drop the foreign key altogether.
    – Mihai
    Feb 27, 2015 at 20:29
  • @Mihai I was under the impression that there were other benefits to foreign keys than just enforcing this constraint. Is that true? Feb 27, 2015 at 20:29
  • 1
    not really,a foreign key creates an index on that column but thats about it.The question on my mind why do you need to keep logins with users which dont exist anymore?If you want a history of users of some sort use a trigger to dump the deleted users in a history table
    – Mihai
    Feb 27, 2015 at 20:31
  • 1
    OK,so how many users do you have?Why do you think you need to delete them?
    – Mihai
    Feb 27, 2015 at 20:38

2 Answers 2

0

If you want this kind of behavior you have to create the foreign key with an ON DELETE CASCADE clause. With an ON DELETE CASCADE foreign key all rows referencing the user will be deleted with the user.

See: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Edit: If you want to keep the user_id in your login tables you just have to drop the foreign key. Anyway, If you are asking this is because you should probably do a logical delete instead of a physical delete: Physical vs. logical / soft delete of database record?

4
  • Please re read the question. I dont want them to be deleted. I want them to just have the value of the FK of the user which no longer exists. Just the user would be deleted, but the rows with the FK would remain. Feb 27, 2015 at 20:23
  • Either you want referential integrity, in which case the referenced records must exist, or you don't care. Pick one. You can't have referential integrity without the database being consistent.
    – tadman
    Feb 27, 2015 at 20:35
  • 1
    Sorry I didn't understand you the first time. I edited the answer giving some additional information. Feb 27, 2015 at 20:37
  • @JordiLlull Thanks! The problem is I have hundreds of querys to the table and I would rather not do WHERE deleted=False in every query. Feb 27, 2015 at 20:42
0

Proper way to do this is to mark offending users as "inactive" so they can't login and you still maintain referential integrity of your database.

Deleting data from master table that has referential integrity links to some data in slave table is bad praxis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.