lundi 1 juin 2015

Sqlite: if reference not used then delete, else update

I have a user table with user_id as primary key

user_id | name   | email           | deleted
1       | John   | john@gmail.com  | NULL

Field user_id is used like reference in some other tables.

Now, when I want to delete the user, I would like to delete the row if is not used in some other tables or update (mark users.deleted=1) if used

In this moment I do it from 2 sequences, but I would like to do it from one.

I've try with CASE but no chance.

CASE 
WHEN (SELECT count(resp_user_id) AS counter FROM equip WHERE user_id = 1)  > 0
  THEN (UPDATE users SET deleted = 1 WHERE user_id=1)
  ELSE (DELETE FROM users WHERE user_id=1) 
END

Any suggestions please?

Aucun commentaire:

Enregistrer un commentaire