0

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.

eshirvana
  • 23,227
  • 3
  • 22
  • 38
bkr879
  • 2,035
  • 2
  • 15
  • 23
  • 3
    Use autocomplete features of your IDE. I've seen languages where similar things exists and the result is a dissaster! – Alejandro Jun 18 '21 at 18:06
  • agreed. You'd have to feed it through a macro pre-processor, but that will greatly increase chances of conflicts with column names – Garr Godfrey Jun 18 '21 at 18:10
  • 2
    This from the school of single letter table / subquery aliases? I'll take readability over brevity every time. – 9bO3av5fw5 Jun 18 '21 at 18:14
  • I certainly understand the argument for readability in the case of collaborative environments. For workflows of individual nature, such as investigations that necessitate a large number of queries in succession, this provides value in my opinion. It's the same reason people alias "git" to "g". – bkr879 Jun 18 '21 at 18:19
  • @Alejandro I just tried the autocomplete functionality - it only acts on proprietary names (column, table names, etc.) as opposed to keywords. – bkr879 Jun 18 '21 at 18:20
  • which IDE are you using? – Luuk Jun 18 '21 at 18:26
  • `s` has too many options for an alias to be effective (like: savepoint, select, set, show, shutdown, signal, start, stop ) – Luuk Jun 18 '21 at 18:29
  • @Luuk The terminal with autocomplete as per https://stackoverflow.com/a/9736256/3000014. – bkr879 Jun 18 '21 at 18:37
  • @Luuk sp, s, st, sh, sd, sg, sr, sp :) – bkr879 Jun 18 '21 at 18:38
  • 4
    `s c1, c w c2=2 t 'y' el 'n' e cb f tab2 gb c1,cb order by cb d;` I hope to never encounter anything like this IRL. Honestly even seeing `Sel` in the dialects that allow it makes me itchy. I agree with others though to utilize an IDE that has customizable autocomplete! – JNevill Jun 18 '21 at 18:59
  • You mean this [BUG](https://bugs.mysql.com/bug.php?id=71338), from Januari 10, 2014 ? Or are you not using Windows? – Luuk Jun 18 '21 at 20:15
  • If you are working through an interface language (such as PHP) you can theoretically do exactly what you desire here by building a PHP class that does what you need, however, there is a huge amount of scope for this to go wrong - but in principle, this concept could help. Personally I would say what others have said and find an IDE solution that's tried, tested and works.... – Martin Jun 18 '21 at 21:00
  • I've been typing SQL for so many years that when I tell my fingers "s", they automatically type "SELECT". Problem solved. You'll get there. – Rick James Jun 18 '21 at 21:39

1 Answers1

1

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.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828