Delete trigger on Many-to-Many Table - sql-server

Hello i have to create a trigger that will help me delete a User.
Example: DELETE FROM Users WHERE Users.Id = 1
I have tables that reference to the Users table, and when i try to delete from the other tables before i delete from the Users table i get this:
The DELETE statement conflicted with the REFERENCE constraint "FK_UsersChats_Users". The conflict occurred in database "cd8eb179-8ec2-41ae-aa28-46e1571ca2bf", table "dbo.UsersChats", column 'UserId'.
My Db diagram
My code so far: http://pastebin.com/45H1WGSr

You can either create a stored procedure or series of queries that deletes from the parent table, then deletes from each child table, or you can set CASCADE DELETE on the relationships so they do it automatically.

Related

T-SQL The conflict occurred in database when delete a record from a parent table

I have a trigger which deletes Attendants when an Employee is deleted. However I get this error:
The DELETE statement conflicted with the REFERENCE constraint "FK_Attendants_Employee". The conflict occurred in database "MYTESTWORKS", table "dbo.Attendants", column 'EmployeeId'.
I cannot define ON DELETE CASCADE between Attendants and Employee because it causes relation cycle error. That's because I defined a trigger to do the job but it seems not to work properly.
I need to catch the delete process before it is executed, so I can delete Attendants.
Employee table has another relationship with another table defined with ON DELETE CASCADE so I cannot implement an INSTEAD OF DELETE TRIGGER.
Any suggestions?

Table is mutating. Trigger may not see it (Even on different tables)

I have two tables: Group and Schedule.
The Group table consists of columns: Group_Name, No_of_member.
The Schedule table consists of column: Group_Name, Schedule_Date.
A group can have many schedules.
I'm trying to create a trigger which deletes all corresponding records from the Schedule table when a particular record is deleted from the Group table. I have written a trigger as:
CREATE OR REPLACE TRIGGER group_test
AFTER DELETE ON Group
FOR EACH ROW
BEGIN
DELETE FROM Schedule WHERE Schedule.group_name = :old.group_name;
END;
But when I try to delete a record from the group table with command: DELETE FROM Group WHERE group_name = 'AwesomeGroup'; , Oracle shows:
Table is mutating, trigger/function may not see it.
at line 2, error during execution of trigger 'group_test'
This would make sense if I was trying to delete a record from the same table in which I've created the trigger (Group in this case), but I have the trigger on Group table and I'm trying to delete the records on Schedule table. So, why does oracle keep giving me this error?
You are getting this error because you have a ON DELETE CASCADE clause on table Schedule for the foreign key on column group_name.
So naturally, when you do the DELETE on table Group, Oracle automatically follows the foreign key and go delete the records in Schedule, which makes the table change.
This is what causes your trigger group_test to dysfunction. In fact, this trigger is redundant with the ON DELETE CASCADE clause. I agree with #WernfriedDomscheit, you must choose between one of the two solutions.

Remove content from SQL Server tables

I have several tables with content. Is there a way how I can remove all the content without removing the keys (primary key, foreign key etc.) ?
When I say truncate or delete it obviously returns an error.
Cannot truncate table 'login' because it is being referenced by a FOREIGN KEY constraint.
You're right, you cannot run truncate table on an table which uses an foreign key. But you can run a normal delete on the table itself.
The only thing you should take care of is the order.
For example:
You have a table users and a table users_log where you store all users logins.
You cannot run the delete on the users table if still rows from users_log referencing them.
If you delete all rows from users_log and afterwards deleting the rows from users everything should be fine.
In this particular example, this code will work:
DELETE FROM users_log
DELETE FROM users
While this won work:
DELETE FROM users
DELETE FROM users_log -- which has a foreign key constraint on users

foreign key data deletion should display error for primary key

i have two tables Table1 and Table2. Where table1 column primary key is referred to table2 column as foreign key.
Now i have to display a error message of constraint violation when ever i delete records from table2 which is having foreign key column of table1.
If I get it right your column A (say) in table 1 references column B (say) in table 2.
What you can do is set the ON DELETE to NO ACTION which will prevent deletion of records from table 2 if any children of it still exists in table 1.
You can can do this by:
ALTER TABLE TABLE1 ADD FOREIGN KEY (A) REFERENCES TABLE2 (B) ON DELETE NO ACTION;
You don't have a constraint violation if you delete records from the child table and not the parent. It is normal to delete child records. For instance if I have a user table and a photos tables that contains the userid from the users table, why would I want to stop that action and throw an error if I want to delete a photo? Deleting a child record doesn't also delete the parent.
If you really want to do that, then you must do it through a trigger (make sure to handle multiple record deletes) or if the FK is a required field, then simply don't grant permissions to delete to the table. Be aware that this may mean you can never delete any records even when you try to delete. A simple method may be to not have a delete function available in the application.
I suspect what you really need to a to get a better definition of what is needed in the requirements document. In over 30 years of dealing with hundreds of databases, I have never seen anyone need this functionality.

SQL Server - Maintain Referential Integrity without CASCADE and INSTEAD OF trigger

I have a table (TableB) that has a foreign key relationship with a parent table (TableA).
When I delete a record in Table A, I want to preserve referential integrity by deleting all records in TableB that reference the deleted record in TableA.
Normally I would ON DELETE CASCADE. However due to the table structure and the overprotective safeguards against multiple cascade paths in SQL Server, this is not possible for this specific relationship.
I also cannot use an INSTEAD OF trigger as TableA itself has a CASCADE foreign key relationship on it.
What I'm thinking of doing is changing the relationship between TableA and TableB to ON DELETE SET NULL, then creating an AFTER trigger to cleanup the NULL records in TableB.
Are there better ways of dealing with this scenario?
Can you change the other constraint that is preventing you from adding this one as ON DELETE CASCADE?
Can you add a DeleteMe column, then issue an UPDATE A SET DeleteMe = 1, then use an after trigger to delete Table B rows first, then the requested table A rows?
Can you split or merge tables (vertically, that is) in some way that separates their mutually exclusive dependencies?

Resources