Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to create a command alias in mysql?

This question is similar to what I'm asking: Is there a way to create an SQL "alias"?

But not quite the same - I'm learning mysql and am going to be running the select command a lot as I screw with different commands. Is there a way I can alias something like:

select * from testdb

to

ls

Or something equally similar? Ideally, I'd be able to alias "select * from" to something like "show" and then just type "show testdb".

like image 800
Locane Avatar asked May 25 '13 01:05

Locane


People also ask

What is an alias command in MySQL?

SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.

How do I start a new alias?

The alias syntax The syntax for creating an alias is easy. You type the word "alias", followed by the name you want to give the alias, stick in an = sign and then add the command you want it to run – generally enclosed in single or double quotes. Single word commands like "alias c=clear" don't require quotes.

What is the benefit of using an alias in MySQL?

Advantages of MySQL AliasesIt makes the column or table name more readable. It is useful when you use the function in the query. It can also allow us to combines two or more columns. It is also useful when the column names are big or not readable.

Can I use alias in WHERE MySQL?

Standard SQL disallows references to column aliases in a WHERE clause.


2 Answers

Three ideas come to mind:

  1. a stored procedure, so you'd end up doing "call show('testdb')"

  2. a shell alias, so you run "show testdb" have it map to "mysql -e 'select * from testdb'"

  3. suck it up and type the command.

like image 193
Alain Collins Avatar answered Oct 19 '22 10:10

Alain Collins


using alias with function can help you pass parameter to a alias like
example query = select * from testdb where param = 'test param'
created alias = sftb test param
to create such alias you can write something like this in your bashrc file.
alias sftb='function sftb(){ mysql -e "select * from testdb where param = '$1'"; };sftb'

like image 30
Naseeruddin V N Avatar answered Oct 19 '22 09:10

Naseeruddin V N