Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck SQL reference constraint on INSERT but not on DELETE
    primarykey
    data
    text
    <p>I have a Logs table in my database where I'm storing records of user actions. There's a column for UserID, which is foreign-keyed to the Users table to check that the UserID exists when a log is added. However, attempting to delete a user causes a constraint failure because it leaves a UserID in the Logs table that no longer exists in the Users table. Deleting the offending rows from Logs isn't an option, because the whole point is to have persistent records (so that we can find out who broke what, basically) so deleting all actions by that user rather defeats the purpose.</p> <p>Is there a simple way to have it check the constraint when inserting new logs (to make sure the user exists at the time) but not when deleting a user? I'm accessing the DB via LINQ, so my queries are in C#, not SQL, so anything that requires that is out, but I have access to the database through MS SQL Server Management Studio, so I can do what I like with it.</p> <p>EDIT for slight clarification: the value of knowing who made an action is not just in accountability; it's useful in determining if an issue is user-specific, organisation-specific, or system-wide, which can greatly narrow down the list of possible causes. Once a user is deleted, it's still worth keeping their actions, and it's still useful to know which actions were by that user, for various reasons - for example, when tracking down an issue caused by one user doing a certain combination of things (eg. two actions that are fine separately but cause errors when one immediately follows another).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload