简体   繁体   English

从没有主键的表中删除记录

[英]Deleting records from a table without primary key

I need to delete some specific record from database table but table itself does not have primary key. 我需要从数据库表中删除一些特定的记录,但表本身没有主键。 So condition depends on other table. 所以条件取决于其他表。 So what is the correct way to do that? 那么这样做的正确方法是什么?

  delete from table_1 
    where exists 
         (select distinct tb.* 
          from table_1 tb, table_2 tb_2, table_3 tb_3
          where tb1.col = tb2.col
          and tb3.col = tb2.col
          and tb3.col_2= 10)

is that correct way to do that? 这是正确的方法吗? Lets say table_1 has 4 columns and first two columns should be the criteria to remove. 让我们说table_1有4列,前两列应该是要删除的标准。

If the select version of your query returns the results you want to delete, then you're good. 如果查询的选择版本返回您要删除的结果,那么您就是好的。 A couple things though.. 虽然有几件事......

Use the ANSI compliant explicit join syntax instead of the comma delineated, implicit syntax (which is long since depreciated). 使用ANSI兼容的显式连接语法而不是逗号描述的隐式语法(自折旧以来很长时间)。 The explicit syntax looks better and is easier to read, anyway. 无论如何,显式语法看起来更好,更容易阅读。

Correlate your EXISTS back to the main table. 将您的EXISTS关联回主表。 And you don't need a distinct, it will return positive whether there is 1 matching row or 10 billion. 并且你不需要一个明显的,它将返回正数,无论是1个匹配行还是100亿个。

SELECT *
FROM table_1 tb_1
WHERE EXISTS (SELECT *
              FROM table_2 tb_2
              JOIN table_3 tb_3 ON tb_2.col = tb_3.col
              WHERE tb_1.col = tb_2.col
              AND tb_3.col_2 = 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM