Delete rows from multiple joined tables - sql-server

I have a problem with removing rows from multiple joined tables in SQL Server.
This is my script:
DELETE d, di, dis
FROM Data d
JOIN DataItem di ON di.DataId = d.DataId
JOIN DataItemSend dis ON dis.DataItemId = di.DataItemId
WHERE d.CardId = 1555
But this syntax is not correct and commas on first line are underlined. How can I solve this problem?

SQL Server does not support deleting from multiple tables at once - unlike MySQL for example.
The way your query is built, however, makes it looks like you could just set up proper foreign keys between the tables, with the on delete cascade option, like so:
alter table DataItem
add constraint fk_DataItem_Data
foreign key (DataId) references Data(DataId)
on delete cascade
;
alter table DataItemSend
add constraint fk_DataItemSend_DataItem
foreign key (DataItemId) references DataItem(DataItemId)
on delete cascade
;
With this set up in place, you can just delete from the top-parent table:
delete from Data where CardId = 1555
... And rest assured that all related records in the children table will be deleted.

Related

Rename table in Sql Server and updating references

Hi I need to rename a table, and add some columns to it.
This table has a PK column (Id) and a Self-referencing column (ParentId). These constraints use the old table name in their names.
There are also other tables that use this tables PK as foreign keys.
So, what would be the correct way to do this, asssuming that I need to rename TableA->TableB?
Drop constraints in all tables that reference TableA.Id
sp_rename 'dbo.TableA', 'TableB'
Add constraints that were dropped with new names?
Or, there is some other way?

How to delete the data in two rows in different table in SQL Server 2012?

How to delete the data in a row in table 1 whose primary key is using as a foreign key in 2nd table.. I am facing the problem here
delete from ASSIGNMENT
where proj_num=18
delete from PROJECT
where proj_num=18
proj_num is the primary key in project and foreign key in assignment.
How to do this task in one delete query ?
You can create/edit the relationship between the two tables in order to set the delete behaviour to ON DELETE CASCADE, then you can just delete the proj_num=18 from the PROJECT table, and it will automatically delete the related records from the ASSIGNMENT table. Hope it works for you.
ALTER TABLE ASSIGNMENT
ADD CONSTRAINT fk_proj
FOREIGN KEY (proj_num)
REFERENCES PROJECT (proj_num)
ON DELETE CASCADE;
Or if you don't have access to the table design you may use this.
DELETE proj
FROM Project AS proj INNER JOIN Assignment AS assn
ON proj.proj_num = asn.proj_num
WHERE proj_num = 18

Create script for deleting tables with foreign keys in order

I need to create a script (SQL Server 2012) that deletes tables from one specific schema, I'm not using cascade delete. I'm for example getting all the tables from sys.tables for that specific schema. Is there any way of getting the tables in order that I delete first the ones with FK and after the main ones? like cascade delete but in a script. I know I can use "nocheck constraint all" but I prefer to do it directly.
Thank you.
Not possible sadly.
You can have a loop of FKs so that no sequence of deletes will work.
The simplest is a pair of tables where each references the other.
-- Pathological foreign keys
-- There is no order in which you can drop these tables
CREATE TABLE one(a INT PRIMARY KEY, b INT)
CREATE TABLE two(a INT PRIMARY KEY, b INT)
ALTER TABLE one ADD FOREIGN KEY (b) REFERENCES two(a)
ALTER TABLE two ADD FOREIGN KEY (b) REFERENCES one(a)

Delete from multiple tables SQL

I trying to figure out how I can delete from multiple tables in SQL Server.
I have one table containing only one primary key and three foreign keys for the three table I want to delete from. The other three table does not contain any foreign keys.
The stored procedure have one parameter, a specific primary key from one of the tables. I want to delete from the other tables WHERE tableID = #tableID. The constraints between the tables are set to cascade.
Is it possible with only that parameter to delete from all four tables?
I've tried with inner join, outer join, temptable..
Table Table Table Table
Pk Pk(Fk1) Pk(Fk2) Pk(Fk3)
Fk1 Column Column Column
Fk2 Column Column Column
Fk3
I have the table 2 Pk as parameter.
if your foreign keys were created with DELETE CASCADE, once you delete from the main table, all the related rows on the foreign tables will be delete too.
It seems like this is how it is configured, isnt it working?

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