Skip to content
Advertisement

MySQL keyword aliases

I would like to set up aliases for MySQL keywords for faster typing of repetitive (or any, really) queries. Example:

select status, count(*) from orders group by status order by created_at;

-> becomes:

s status, c(*) f orders gb status ob created_at;

How could I go about achieving the above? I have looked around but unfortunately, most searches point to column and table aliases and I did not see a mention in MySQL documentation.

I am interested in MySQL specifically, but really, this probably concerns the SQL DDL in general.

Advertisement

Answer

SQL keywords are parsed by the code in the RDBMS server. To change the keywords recognized by the parser, you would need to change the code of the RDBMS and recompile it with your custom changes.

In MySQL, the spelling of keywords are defined in this code file: https://github.com/mysql/mysql-server/blob/8.0/sql/lex.h

Another solution that is a bit less invasive is to use the Query Rewrite Plugin. This was introduced in MySQL 5.7, and in MySQL 8.0.12 it supports more types of SQL statements.

https://dev.mysql.com/doc/refman/8.0/en/rewriter-query-rewrite-plugin.html

But with the Query Rewrite Plugin, you’d have to enumerate all your queries without the abbreviations, and how you want them to be rewritten. This doesn’t sound like it would save you any typing, it would require more typing.

In any case, using abbreviated keywords would introduce new syntax challenges because if you use identifiers (table names, column names, etc.) conflicting with reserved keywords, you have to delimit them with back-ticks:

s * f `s` w `gb` = 123 gb `s`.date;

It would become pretty confusing to tell the difference between keywords and your identifiers.

I once traded emails with one of the people who were on the ANSI SQL committee in the 1980’s, and he said that many SQL keywords were chosen deliberately to be uncommon English words so they would be less likely to conflict with words you’d use as table or column identifiers.

I agree with the other folks in the comments above that you should solve this with your editor, if you do it at all. There must be editor features to map abbreviated words to full words.

For example, you can even do this in vim with the :ab command. See https://vim.fandom.com/wiki/Using_abbreviations

I would not try to change the SQL grammar.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement