Xaprb

Stay curious!

Why multi-table cross-database deletes fail in MySQL

with 4 comments

Sometimes multi-table deletes fail in MySQL with a message about an unknown table. In this article I’ll explain the exact combination of circumstances that cause it to happen.

First, the symptoms:

delete a from db1.t1 as a
    inner join db1.t2 as b  on a.c1 = b.c1;
ERROR 1109: Unknown table 'b' in MULTI DELETE

This will happen if the following are true:

  1. No database is selected or one of the tables is not in the current database
  2. The tables are aliased

According the the MySQL documentation,

Cross-database deletes are supported for multiple-table deletes, but in this case, you must refer to the tables without using aliases.

That’s correct, but it might not seem right. If the tables are fully qualified by database and table name, but are in the current database, it works. If I switch to another database and run the same statement, it won’t work.

This bit me when I was scripting out a large procedure running from a Perl script that doesn’t specify a database. I scripted it while I was connected and had a database specified. Then I ran it, and it bombed out when it got to the delete statement.

The solution is not to alias the tables. It’s less convenient, but it’s the only thing to do sometimes.

Written by Xaprb

May 12th, 2006 at 10:01 pm

Posted in Uncategorized

4 Responses to 'Why multi-table cross-database deletes fail in MySQL'

Subscribe to comments with RSS

  1. This is fixed in 5.5.

  2. Great!

    Xaprb

    7 Dec 10 at 9:56 am

  3. Hi!

    Do you have a full test case for this?

    Thanks,
    -Brian

    Brian Aker

    11 Dec 10 at 4:30 pm

  4. while what you describe is true MOST of the time, I have had multi deletes fails with unknown table…
    1) while “using” the correct database.
    2) while fully qualifying all tables with `database`.`tablename`.`field`
    e.g.
    DELETE FROM `database`.`table2`
    USING `database`.table1`
    WHERE `database`.`table1`.`field` = `database`.`table2`.`field`;

    RonFinnerty

    28 Mar 12 at 10:03 am

Leave a Reply