Answers:
在此示例中,您可以利用“已删除”伪表。就像是:
begin transaction;
declare @deletedIds table ( id int );
delete from t1
output deleted.id into @deletedIds
from table1 as t1
inner join table2 as t2
on t2.id = t1.id
inner join table3 as t3
on t3.id = t2.id;
delete from t2
from table2 as t2
inner join @deletedIds as d
on d.id = t2.id;
delete from t3
from table3 as t3 ...
commit transaction;
显然,您可以执行“已删除输出”。如果您需要加入第三张表的内容,也可以在第二张表上删除。
附带说明一下,您也可以在插入语句上执行insert。*,在更新语句上同时进行insert。*和Deleted。*。
编辑: 另外,您是否考虑过在table1上添加触发器以从table2 + 3中删除?您将处于隐式事务中,并且还将具有“插入的”和“删除的 ”伪表。
您始终可以在表的关系上设置级联删除。
您可以将多个删除封装在一个存储过程中。
您可以使用事务来确保一个工作单元。
您可以在SQL Server的DELETE中的FROM子句中使用JOIN语法,但仍仅从第一个表中删除,它是专有的Transact-SQL扩展,可替代子查询。
从这里的例子:
-- Transact-SQL extension
DELETE
FROM Sales.SalesPersonQuotaHistory
FROM Sales.SalesPersonQuotaHistory AS spqh INNER JOIN
Sales.SalesPerson AS sp ON spqh.BusinessEntityID = sp.BusinessEntityID
WHERE sp.SalesYTD > 2500000.00;
从主表中删除一些记录并从两个明细表中删除相应记录的示例:
BEGIN TRAN
-- create temporary table for deleted IDs
CREATE TABLE #DeleteIds (
Id INT NOT NULL PRIMARY KEY
)
-- save IDs of master table records (you want to delete) to temporary table
INSERT INTO #DeleteIds(Id)
SELECT DISTINCT mt.MasterTableId
FROM MasterTable mt
INNER JOIN ...
WHERE ...
-- delete from first detail table using join syntax
DELETE d
FROM DetailTable_1 D
INNER JOIN #DeleteIds X
ON D.MasterTableId = X.Id
-- delete from second detail table using IN clause
DELETE FROM DetailTable_2
WHERE MasterTableId IN (
SELECT X.Id
FROM #DeleteIds X
)
-- and finally delete from master table
DELETE d
FROM MasterTable D
INNER JOIN #DeleteIds X
ON D.MasterTableId = X.Id
-- do not forget to drop the temp table
DROP TABLE #DeleteIds
COMMIT
SELECT INTO #DeleteIds
而不是CREATE TABLE 'DeleteIds
跟随INSERT INTO 'DeleteIds...
吗?
只是想知道..在MySQL中这真的可能吗?会删除t1和t2吗?或者我只是误解了这个问题。
但是,如果您只想删除具有多个连接条件的table1,则不要为要删除的表添加别名
这个:
DELETE t1,t2
FROM table1 AS t1
INNER JOIN table2 t2 ...
INNER JOIN table3 t3 ...
应该这样写才能在MSSQL中工作:
DELETE table1
FROM table1
INNER JOIN table2 t2 ...
INNER JOIN table3 t3 ...
对比其他两个常见的RDBMS如何执行删除操作:
http://mssql-to-postgresql.blogspot.com/2007/12/deleting-duplicates-in-postgresql-ms.html
这是删除记录而不离开孤儿的另一种方法。
声明@user表(keyValue int,someString varchar(10)) 插入@user 值(1,'1值') 插入@user values(2,'2 value') 插入@user 值(3,'3值') 声明@password表(keyValue int,详细信息varchar(10)) 插入@password 值(1,'1密码') 插入@password 值(2,'2密码') 插入@password 值(3,'3密码') -删除前 选择* from @password a内部联接@user b 在a.keyvalue = b.keyvalue上 选择*到@user的#deletedID中,其中键值= 1-这类似于输出示例 删除@user,其中keyvalue = 1 删除@password其中的键值(从#deletedid中选择键值) -删除后- 选择* from @password a内部联接@user b 在a.keyvalue = b.keyvalue上
一切都已指出。只需DELETE ON CASCADE
在父级上使用table
或从中删除child-table
即可parent
。
根据约翰·吉布(John Gibb)的答案,在具有FK关系的两个表中删除一组数据:
--*** To delete from tblMain which JOINs to (has a FK of) tblReferredTo's PK
-- i.e. ON tblMain.Refer_FK = tblReferredTo.ID
--*** !!! If you're CERTAIN that no other rows anywhere also refer to the
-- specific rows in tblReferredTo !!!
BEGIN TRAN;
--*** Keep the ID's from tblReferredTo when we DELETE from tblMain
DECLARE @tblDeletedRefs TABLE ( ID INT );
--*** DELETE from the referring table first
DELETE FROM tblMain
OUTPUT DELETED.Refer_FK INTO @tblDeletedRefs -- doesn't matter that this isn't DISTINCT, the following DELETE still works.
WHERE ..... -- be careful if filtering, what if other rows
-- in tblMain (or elsewhere) also point to the tblReferredTo rows?
--*** Now we can remove the referred to rows, even though tblMain no longer refers to them.
DELETE tblReferredTo
FROM tblReferredTo INNER JOIN @tblDeletedRefs Removed
ON tblReferredTo.ID = Removed.ID;
COMMIT TRAN;
DELETE TABLE1 LIN
FROM TABLE1 LIN
INNER JOIN TABLE2 LCS ON CONDITION
WHERE CONDITION
$的SQL = “DELETE FROM basic_tbl
,education_tbl
,
personal_tbl
,address_tbl
,department_tbl
使用
basic_tbl
,education_tbl
,
personal_tbl
,address_tbl
,department_tbl
WHERE
b_id
= e_id
= p_id
= a_id
= d_id
=”“ $ ID。””“; $ rs = mysqli_query($ con,$ sql);