When delete a master record a cascade trigger will get fired and delete the records in the child tables.
And I cannot delete a record manually from the table as it got a foreign key relation with child table.
But how can I prevent the child record from being deleted manually from the table. Currently I am able to delete the child record manually and on page loads as the child record is missing and page load fails.
I'm a bit unclear as to why you'd want to have something like this done. You simply don't allow for the deletion of child records on the UI aside from your cascade delete - just don't give the user the option.
If you're worried about some random DBA going in to your database and writing:
delete from childTable where parentId = 5 -- or whatever
then i think you have more things to worry about... such as why people with production database write access are even thinking about manually writing and executing statements such as this on your prod database.
If you still really needed to do something like this. You could potentially write a before delete trigger on all of your child tables to ensure that the parentId doesn't exist in the parent table prior to delete. This would likely cause your cascade delete to fail (i would guess) so you would need to update your cascade delete functionality to disable the trigger before deletion, re-enabling the trigger after. But this would not prevent your "random dba" from just doing something similar by disabling the trigger, deleting a record, and re-enabling.
If you provide a little more information around the specific scenarios of why a child record would "manually" be deleted, might be able to offer more.
I'm not sure who or what would manually delete records from this table, but I would lean towards using DENY permissions with this requirement, and ensure all roles on that database have no delete permissions on that table.
Sample script:
USE [YOUR_DB]
GO
DENY DELETE ON [dbo].[YOUR_TABLE] TO [DOMAIN\user]
GO
Replace [dbo] with relevant schema name and replace [DOMAIN\user] with the relevant user.
Related
I have a table COMPANY where companies are kept. I want to create a view of that table, let's name it COMPANY_CDC but with one caveat:
When an entry in the original table is deleted, I want to set a deleted flag on the view entry instead of deleting it.
EDIT Why soft deletes? The point is that im performing change data capture using JDBC, and JDBC is only able to capture soft deletes. Inserts / updates are no problem.
If this cannot be done by using a view, what would be an alternative solution?
You can insert deleted values in another table using trigger
, and with join of these two table you can create your view.
I have several bases on multiple servers with tables in the same column pattern.
What I need to do is create a trigger in the database to audit delete, update, insert is there any way I can dynamically leave the table name that was given update?
There is more than one table in each database to be monitored.
Every help is welcome
You can check with this answer this might help you. Here I have given idea to create a trigger to monitor the update value in table.
https://stackoverflow.com/a/54229188/10532500
The output is as shown below of the trigger in the form of an audit table.
In SQL Server 2014, I'm trying to add CASCADE DELETING on 3 FK. If I add a Cascade Delete in one relationship, it works fine. If I add more Cascade Deletes, it doesn't work (Cycle detected error message).
In the above diagram, you can see the Users table, and a Tasks table ("Tareas" in spanish). So, what I need to acomplish is when the user is deleted, I need to set the marked field in Tasks to NULL.
This is something common in a database, so I thought there is a way to handle this.
In my case, most of my tables have a pair of fields holding the UserId of the user that Created or Modified the record. So, I need to solve this pattern to apply it several places.
CASCADE DELETE means that in your situation if you delete a User, then SQL Server will delete any attached Tasks too. That is, entire rows will be deleted. Apart from issues such as unexpected losses of data, loss of referential integrity or the potential of infinitely recursive deletions, this behaviour is not what you want anyway. You have stated you only want to mark the associate User columns in your Tasks table to null.
As a suggestion, have you considered implementing a TRIGGER? Something like this (haven't tested this, treat it as pseudo-code!):
CREATE TRIGGER DeleteTareasTrigger
ON Users
BEFORE DELETE AS
BEGIN
UPDATE t
SET t.CreadaPor = NULL
FROM DELETED as d
INNER JOIN Tareas t ON d.UserID = t.CreadaPor
UPDATE t
SET t.ModifcadaPor = NULL
FROM DELETED as d
INNER JOIN Tareas t ON d.UserID = t.ModifcadaPor
END
GO
Or as another approach, add a bit field on the User table to indicate whether the person is active/deleted.
I have 3 tables: Notifications, NewItemNotifications and Items.
I've set an ON DELETE rule on the NewItemNotifications and Items table which deletes the NewItemNotification row when I delete some item.
The problem is that the parent row in the Notifications table is still exists, how can I handle this?
NewItemNotification is dependent table - it can never trigger deletion of parent record through database constraint. The only way is to write a database trigger on NewItemNotification to perform delete in Notification table after the dependent record is deleted.
The problem is that such trigger can cause issue if EF will try to delete NewItemNotification because it doesn't know about trigger existence. It first deleted NewItemNotification record which trigger deletion of Notification item without EF to know it. EF will then try to delete Notification record again but the record was already deleted. I think it will result in concurrency exception.
The best option in this case is not using cascade delete and handle delete yourselves.
hi all i am getting a problem while i attenpting to delete a row from parent table, actuall i have created 2 table custmer and account.i make cutomer id(primary key) in customer and customer id (forigen key ) in account.after created it i have filled data inside both table.
at this point while i am trying to delete a row from first table (customer ) then it give failure message is that it can't be deleted bcs it is refrenced as forigen key some thing like that............but while we delete row from account table then it's delete sucess fully.
.......i want to function like that if i delete a row from parent table(customer) then its in child table that row which has same customer id (account table) is delete automatically............
watch out on the cascade deletes! a user will accidentally click on the application's little trash can icon and delete the customer, and then all the cascades will remove every trace of that customer, orders, invoices, payments, history, etc from your database. After the user call you to tell you about their little mistake, you'll have to restore a backup and try to pull the info back into the database.
I would look into "soft deletes" where you only change the customer's status from "active" to "inactive". the rows is not deleted, preserving all foreign key data. This allows reports to run on the data, because it still exists, as well as easy an "undo".
Soft deletes are not the end all only way to go, it is a business decision on how to handle this, purge the data or mark it inactive. That is only something you can decide, because I don't know your application or business logic. I just thought that I would offer it as an alternative.
You need to set up the foreign key with on delete cascade to achieve this.
For SQL Server 2008 see the article Cascading Referential Integrity Constraints
Edit Just to add a somewhat redundant health warning you should be aware that adding on delete cascade will mean that when you delete the row from the parent table associated rows from the child table will be deleted. However as this is exactly the behaviour you state that you want I can't see that would be an issue.