Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete rows in a Teradata table that are not in another table?

Tags:

sql

teradata

What makes my situation tricky is that I don't have a single column key, with a simple list of primary keys to delete (for instance, "delete from table where key in ([list])"). I have multiple columns together as the primary key, and would need to join on all of them.

Using what I know of other databases, I thought this might be done as:

DELETE FROM
    table1 t1
  LEFT OUTER JOIN
      table2 t2
    ON
      t2.key1 = t1.key1 AND
      t2.key2 = t1.key2
  WHERE
    t2.key1 IS NULL;

But Teradata (v12) responds with error number 3706, saying "Syntax error: Joined Tables are not allowed in FROM clause."

like image 263
Paul Hooper Avatar asked Dec 01 '10 05:12

Paul Hooper


People also ask

Can you remove rows from a table based on values from another table?

Deleting rows based on another table. Sometimes we need to delete rows based on another table. This table might exist in the same database or not. We can use the table lookup method or SQL join to delete these rows.

How do I delete rows in Teradata table?

DEL <table-name> ; In the earlier releases of Teradata, the ALL was required to delete all rows. Now, ANSI rules say that ALL is the default for all rows. The ALL is optional and with or without it, all rows are deleted.

Can we use qualify in delete statement in Teradata?

I came to know that qualify cannot be used in delete statement.

How do I delete a row in two tables?

The syntax also supports deleting rows from multiple tables at once. To delete rows from both tables where there are matching id values, name them both after the DELETE keyword: DELETE t1, t2 FROM t1 INNER JOIN t2 ON t1.id = t2.id; What if you want to delete nonmatching rows?


1 Answers

Found this is done by:

DELETE FROM
    table1
  WHERE
    (key1, key2) NOT IN (
      SELECT UNIQUE key1, key2 FROM table2
    );
like image 51
Paul Hooper Avatar answered Sep 28 '22 15:09

Paul Hooper