How to delete record from proxy table(toxi solution)? - database

There are next tables images, posts, users, images_posts, images_users. When I want to delete images, I send id of image to server, I can't delete image record, because I also have to delete record from proxy table. How to find out record from which proxy table should be deleted?

What you are looking for is a feature of the database called cascade delete. This will make sure related records also get deleted when you try to delete a record from the "main" table.

Related

MS Access <> SQL Server : save current record which creates GUID in SQL and go back to this record

The title is maybe not the best. My current setting: MS Access frontend with a SQL Server backend.
The SQL Server backend will create the PK with a GUID as soon the row is saved.
I need to save my record and go back to that record to use the created PK GUID.
When I do a Me.Refresh or Me.Requery, it will loose the connection to the record showing #Name? on all fields.
I need to get to GUID because I want to upload a file which is done in a FILESTREAM table and the link to the content table is done via the GUID of the content table.
So I need the content table GUID but as the record is not yet saved, I don't have one.
How to save the record, get its GUID and "stay" on that record?
I tried following which is not working:
DoCmd.RunCommand acCmdSaveRecord 'Here it will show '#Name? on all fields
Me.Refresh
Use Me.Requery instead of Me.Refresh and hope you stay on the same record. You can, of course, navigate to the last record, which opens a small door for race conditions (someone could in theory insert a record between you saving yours and you requerying).
Access does strange things to add records and then fetch the default data, as can be read here. It doesn't play well with GUID columns (or, really, anything that isn't a numeric identity primary key column without triggers). By forcing a requery you refetch all data.
Alternatively, you can bind the form to an ADO recordset and work with that, but that approach has some problems on its own, such as trouble with the built-in refresh/filter/sort options.

Is it possible to create a view where deleted rows in the original table are kept

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.

How to prevent a child record from being deleted

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.

SQL Server ON DELETE and inheritance

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.

how we delete a row from parent table when child table connected with paren table by forigen key

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.

Resources