Home > OS >  Stop MySQL table backticks from preventing cross schema queries
Stop MySQL table backticks from preventing cross schema queries

Time:06-01

I've noticed weird behavior in MySQL, cross schema queries are only possible if the {schema}.{table_name} isn't quoted in backticks(`). Now this is technically defined behavior according to the MySQL's technique to escape reserved words being used as tablenames etc., however it seems counter intuitive that one feature (safely using reserved words) prevents another feature (cross schema queries). Is there some way to disable this or is my only option to just not use backticks around my tablenames in cross schema queries.

CodePudding user response:

The schema name and the table name are individual tokens, and should be delimited separately.

https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html says:

If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.

The same applies when using schemas and tables, for example:

WRONG — tries to find a table whose name is literally db1.mytable in the current schema.

SELECT * FROM `db1.mytable`;

RIGHT — uses the qualified table named mytable in the schema db1.

SELECT * FROM `db1`.`mytable`;
  • Related