Reputation: 131
So I'm really new to learning SQL and I'm interested about the command
SELECT DATABASE();
At my point in learning, I know that command is used to show what database I'm currently working with. I accidentally made the typo,
SELECT DATABASE() L;
and the MySQL terminal displayed the letter L above the name of the database I was working with. Why does this happen? And what exactly does SELECT DATABASE() mean?
I tried this again and did the command
SELECT DATABASE() HI;
and it did the same thing and showed "HI" above the name of my selected database. Using the correct syntax, it normally shows, "DATABASE()" above the db that I'm currently working with.
Upvotes: 3
Views: 4930
Reputation: 1269953
database()
is an information function provided by MySQL to return the name of the current database. It can be used anywhere a string constant would be expected.
MySQL does not require a from
clause, so a select
is a complete query that returns one row -- the values calculated in the select
.
Databases allow you to assign a name ("column alias") to an expression in the select
. Normally, this would be written as:
select database() as database_name
However, the as
is optional (although I strongly recommend using it for column aliases). And nothing enforces a reasonable name.
So:
select database() l
returns a result set with one row and one column. The column is called l
and the value in the one row is the database name.
Upvotes: 3
Reputation: 3437
You are selecting a dataset with one column. The contents of the column is the current database. You are naming that column L.
Compare with
select 4 label;
Upvotes: 0
Reputation: 23
I am not expert in database. But I am quite sure that show database(); will show you the selected database in other words the database which you have chosen.
Upvotes: -1