Is `Delete From Join` Standard SQL?

前端 未结 6 1278
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 02:15

Among other questions, this one asked how to delete from a join.

My question: How much of this is standard SQL? On which databases would this actually work (most not

6条回答
  •  不知归路
    2021-01-18 02:45

    The Standard SQL-92 syntax required a scalar subquery. This is a relationally solid solution because there is no ambiguity (unlike the proprietary alternatives which can yield unpredictable/unexpected results).

    However, one of the drawbacks of the SQL-92 approach is that it can be verbose and involve repetitive constructs e.g.

    UPDATE MyTable
       SET col1 = (
                   SELECT T.colA
                     FROM OtherTable T
                    WHERE T.ID = MyTable.ID
                  ), 
           col2 = (
                   SELECT T.colB
                     FROM OtherTable T
                    WHERE T.ID = MyTable.ID
                  )
     WHERE EXISTS (
                   SELECT *
                     FROM OtherTable T
                    WHERE T.ID = MyTable.ID
                  );
    

    The idea is that the optimizer can recognize the same subquery being reused but in practice vendors have typically not implemented this.

    SQL-99 addressed this verbosity and repetition by introducing MERGE, which was further refined in SQL:2003. It is interesting that nowadays the vendors typically choose to implement new features as per the Standard spec then extend them to provide the features they require, rather than writing wholly proprietary features.

提交回复
热议问题