Is there any way to generate a cascade delete statement? - sql

Let's say I have a row in Table A I wish to delete but there a multiple foreign key constraints in other tables. Is there a way to automatically generate the delete command?
I think a tool that would recursively look at the foreign keys and in turn generate a delete statement to take care of all foreign keys should exist, but I can't find one.
I'm on MSSql server2008

When setting up your Foreign Key relationships there is an ON DELETE CASCADE you can add.
MSDN Cascading Referential Integrity Constraints
ON DELETE CASCADE
Specifies that if an attempt is made to delete a row with a key referenced by foreign
keys in existing rows in other tables, all rows containing those foreign keys are also
deleted. If cascading referential actions have also been defined on the target tables,
the specified cascading actions are also taken for the rows deleted from those tables.
SO even has a solution where you are not adding it to the table:
In SQL Server 2005, can I do a cascade delete without setting the property on my tables?

Related

How to delete relational records in mutliple tables

I am tidying up an Access 2007 database with a central key table whose Primary key is a foreign key in numerous tables (about 20 in all) but the person who set this up obviously did not set up the constraints with DELETE CASCADE because when I attempt to delete a record in the central table I am warned of foreign key relations. Is there a way I can perform a delete and search through all tables deleting the relevant foreign key records. It appears I cannot add DELETE CASCADE without recreating all the referential entegrity
You can edit the relationship between each table by going to database tools -> relationship -> edit the relevant relationship between tables to make it cascade.

Delete row with foreign key constraints and no cascade deleting SQL

Trying to figure out how to delete a row in a SQL table that has multiple foreign keys pointing to it, and more keys pointing to those, etc, etc. Cascading Deletes aren't turned on (I have no control to turn them on), and I'm trying to avoid performing a delete on EVERY single row that is affected by this one delete.
So if I have table XXX, with columns YYY and ZZZ, where YYY is the primary key and ZZZ is a column that has multiple foreign keys pointing to it, how would I go about deleting a row based on the primary key value?
Syntax would be:
DELETE FROM XXX
WHERE YYY = some_value
Is this even possible (without performing a ton of individual deletes)? And if so, how would I do it?
No.
Either you need the foreign key constraints to cascade the delete (something I'm not terribly fond of, it makes it too easy for some application/ developer to think they can delete and re-insert some data rather than updating it in place without inadvertently causing all the child rows to be deleted) or you have to delete the child rows before you delete the parent.
Normally, if you want to delete the data from the child tables, it is easier to just manually write the various DELETE statements. It would be possible to query the data dictionary (dba_constraints, dba_cons_columns, etc.) and dynamic SQL to walk all the constraints and generate the appropriate DELETE statements. In the vast majority of cases, it wouldn't make sense to do that unless you're trying to generate delete statements for a large number of tables.
How about turning off the foreign key constraint check?
SET FOREIGN_KEY_CHECKS = 0;
Then turning it on back when you delete the row?
SET FOREIGN_KEY_CHECKS = 1;

Delete Multiple Entires In SQL With Other Tables Using The Data As Foreign Keys

I have several tables which reference one main table using foreign keys.
My goal is for one blanket delete statement from the main table, which will then delete all those with the data as foreign keys as well.
However I get constraint errors as I expect since the reference is deleted before its deleted from the other tables. Is there any way to tell SQL I would like to delete from all tables where the data is used as a foreign key as well?
To have deletes on the parent table cascade to referencing tables, use an ON DELETE CASCADE foreign key. However, if you're deleting all rows from the main table there's a better way to solve your problem using TRUNCATE; see the end of this post.
You can ALTER TABLE to change a default ON DELETE NO ACTION foreign key to ON DELETE CASCADE. Drop the constraint then re-create it with the ON DELETE CASCADE modifier. See CREATE TABLE and the constraints documentation for information on the foreign key constraint syntax.
In brief, instead of col coltype REFERENCES fktable(fkcol) you use col coltype REFERENCES fktable(fkcol) ON DELETE CASCADE.
If you use ON DELETE CASCADE then to get even vaguely decent performance it is vital to create an index on the foreign key column. Even then, for bulk deletes from the target table it's often much faster to go and delete from each foreign key referencing table first, rather than relying on cascades.
You may want to also use ON UPDATE CASCADE if you're using ON DELETE CASCADE.
If you're deleting all rows from the main table, not just a selection of them, you're probably better off truncating the tables with TRUNCATE ... CASCADE:
CASCADE
Automatically truncate all tables that have foreign-key references to
any of the named tables, or to any tables added to the group due to
CASCADE.
The only way to have a DELETE cascade to tables with a foreign key linked to the table you are running your statement on is to have the ON DELETE CASCADE option set on the key, you will need to run an ALTER TABLE statement to change them from their current value to this option:
http://www.postgresql.org/docs/current/interactive/ddl-constraints.html

Is it possible to alter the cascading behaviour of existing mysql foreign keys?

I have two tables in mysql, both are type InnoDB and there is an existing foreign key from the first to the second.
However, the foreign key was created without any cascading behaviour. The table is large, and any changes to indexes or new keys locks the table for ~20 minutes.
Is it possible to add "ON DELETE CASCADE" behaviour to an existing foreign key without dropping the key and recreating it?
Unfortunately, no. On Delete and On Update are systemic attributes of the foreign key itself and can only be specified when creating the constraint.

Changing a record in a table (sql server) that has foreign keys?

Does anyone know if there is a quicker way of editing a record that has foreign keys in a table (in sql server).. i will explain.. i have approx 5 tables that have there own ID but are linked together using a foreign key...
Hence i needed to change the foreign key (the contract number in my case), but i had to copy each record to a new record and edit it that way...
As if i try to edit the contract number it gives me the standard error of being associated and violates a foreign key etc
Surly there must be a better way?
ANy ideas?
are you talking about changing the PK and then updating all the Fks? In that case enable cascade updates and this will be done automagically
same with deletes, you enable cascade deletes
ON DELETE CASCADE
Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted. If cascading referential actions have also been defined on the target tables, the specified cascading actions are also taken for the rows deleted from those tables.
ON UPDATE CASCADE
Specifies that if an attempt is made to update a key value in a row, where the key value is referenced by foreign keys in existing rows in other tables, all of the foreign key values are also updated to the new value specified for the key. If cascading referential actions
I'm not an SQL expert, but can't you set something like ON UPDATE CASCADE to automatically update the foreign key when the primary key is changed?
Or try disabling the integrity constraint, do your changes and attempt to re-enable the constraint. Basically, if you didn't do it right you will get an error then (can't enable a constraint that would be violated).