Fixing Error 1066 When Using the HANDLER Statement in MySQL

If you’re getting MySQL error 1066 which reads something like “1066 (42000): Not unique table/alias: ‘products’” when using the HANDLER statement in MySQL, it could be that you’re trying to open a table that’s already open.

If this is the case, be sure to close the table before trying to open it again. Or simply continue working without opening the table again.

Example of Error

Here’s an example of code that produces the error:

HANDLER products OPEN;
HANDLER products OPEN; --This causes the error

Output:

1066 (42000): Not unique table/alias: 'products'

I got the error because I tried to open the same table twice. The first line opened the table, so I didn’t need to open it again.

Solution

How we deal with this issue depends on whether or not we want to keep working with the table. If we do, then we should simply continue working with it without trying to open it again. For example:

-- Example of reading from the table
HANDLER products READ FIRST;
...

--Close once you've finished with the table
HANDLER products CLOSE;

Otherwise, if you decide that you’re already finished with the table, you can just go ahead and close it:

HANDLER products CLOSE;

Obviously you need to replace products with the name of your table (or its alias).