对于经常删除记录的 Web 应用程序,如果我们考虑性能,什么是最好的?
检查记录是否与其他表有任何关系,并防止用户删除。
例如:查询将是这样的
if not exist(select * from table1 where tableX_id = @id) and not exist(select * from table2 where tableX_id = @id) ... then delete from tableX where id = @id
或者
执行
delete
并让 RDBMS 由于foriegn key
约束引发错误try{ Service.DeleteRecord(id) }catch{ Handle the error here }
在这种情况下,查询将很简单
delete from TableX where id = @id