Fix “Unknown table … in HANDLER” in MySQL (Error 1109)

If you’re getting MySQL error 1109 which reads something like “1109 (42S02): Unknown table ‘products’ in HANDLER” in MySQL, it appears that you’re trying to reference a table that’s not currently open when using MySQL’s HANDLER statement.

This can happen when you try to read from a table that you haven’t yet opened. It can also happen when you’ve assigned an alias to the table but you try to reference it without the alias. And it can happen if you try to close a table that isn’t actually open.

Regardless of the exact scenario, to fix this issue be sure to reference the table by its correct name, and that the table is open.

Example of Error

Here are examples of code that produce the error.

Scenario 1: Reading a table that isn’t open:

HANDLER products READ FIRST;

Output:

1109 (42S02): Unknown table 'products' in HANDLER

I got the error because I tried to read from a table that I hadn’t yet opened.

Scenario 2: Closing a table that isn’t open:

HANDLER products OPEN;
HANDLER product CLOSE;

Output:

1109 (42S02): Unknown table 'product' in HANDLER

Pretty much the same thing, but this time I simply tried to close a table that wasn’t open. Here, I opened the correct table (products), but when I tried to close it, I accidentally used product instead of products (plural).

Scenario 3: Not referencing the alias:

-- Open the table with an alias (no error)
HANDLER products OPEN AS p;

-- Reference the table without using its alias (causes error)
HANDLER products READ FIRST;

-- Closing the table without using its alias (causes error)
HANDLER products CLOSE;

Output:

ERROR 1109 (42S02): Unknown table 'products' in HANDLER

ERROR 1109 (42S02): Unknown table 'products' in HANDLER

The last two lines resulted in the error. That’s because, when we open a table with an alias, we must use that alias when referring to the table.

Solution

Although the above scenarios are all slightly different, the solution is the same: be sure to open the table before using it, and once you do, make sure you’re using the correct table name when referencing that table, or if you’ve assigned an alias to the table, use that instead of the table name.

Therefore, we can fix the above errors as follows.

Scenario 1: Reading a table that isn’t open

In the first scenario, we simply need to open the table before trying to read from it:

HANDLER products OPEN;
HANDLER products READ FIRST;
HANDLER products CLOSE;

In this case I opened the table, read from it, then closed it. No error.

Scenario 2: Closing a table that isn’t open

Similar to the previous scenario, we need to ensure that the table is open before trying to close it. In my example above, the reason for the error was that I made a typo on the table I was trying to close. I was trying to close product, but the actual table name was products.

So in that case, we simply need to ensure that the name we provide for the table when trying to close it is the same name that we used when opening it:

HANDLER products OPEN;
HANDLER products CLOSE;

It’s also possible that you’re simply trying to close a table that you’ve already closed. In that case you’re done – no need to do anything more.

Scenario 3: Not referencing the alias

To fix scenario three, we simply need to provide the alias when referencing a table that we assigned an alias to:

HANDLER products OPEN AS p;
HANDLER p READ FIRST;
HANDLER p CLOSE;