©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The DATABASE SQL Function in MySQL and MariaDB - Get current database name

DATABASESyntax:DATABASE()
Return value:CHAR
Synonyms:SCHEMA
Function type:Information function
Function Description

The DATABASE() function returns the current database name as a string.

If no default database is defined, the DATABASE() function returns NULL.

Within stored routines, the default database is the database associated with the routine.

A database can be selected with the USE statement.

SQL Examples for the DATABASE function

 1
 2
 3
 4
 5
 6
 7
 8
 9

SELECT database();

use test;
SELECT database();

use information_schema;
SELECT database();

database()
varchar(34) BINARY
NULL
database()
varchar(34) BINARY
test
database()
varchar(34) BINARY
information_schema
The examples were created with the MyWAY SQL manager: Download

How to use the DATABASE() function in MySQL and MariaDB databases

In MySQL and MariaDB the DATABASE() function is used to retrieve the name of the currently used database. The function can be used directly in SQL statements or within SQL queries, requires no arguments, and returns a string representing the name of the current database.

The DATABASE() function can be used to easily get the current database name within a query or procedure, for example to switch between databases or to reference objects in the current database. The function can be used to organize queries or statements based on the current database, for example, different queries can be executed or different logic can be applied depending on the database context. When creating dynamic SQL statements, the function is useful to include the current database name in the generated SQL code, allowing for more flexible and dynamic queries that adapt to the current database context.
CURRENT_TIMESTAMPCURRENT_USERCURTIMEDATABASEDATEDATE_ADDDATE_FORMAT

Further MySQL und MariaDB SQL Information functions

BENCHMARKBENCHMARK(Number, Expression)
More about BENCHMARK Function

BINLOG_GTID_POSBINLOG_GTID_POS()

CHARSETCHARSET(String)
More about CHARSET Function

COERCIBILITYCOERCIBILITY(String)
More about COERCIBILITY Function

COLLATIONCOLLATION(String)
More about COLLATION Function

CONNECTION_IDCONNECTION_ID()
More about CONNECTION_ID Function

CURRENT_USERCURRENT_USER()
More about CURRENT_USER Function

CURRENT_ROLECURRENT_ROLE()
More about CURRENT_ROLE Function

DECODE_HISTOGRAMDECODE_HISTOGRAM()

DEFAULTDEFAULT(Column)
More about DEFAULT Function

FOUND_ROWSFOUND_ROWS()
More about FOUND_ROWS Function

LAST_INSERT_IDLAST_INSERT_ID([Expression])
More about LAST_INSERT_ID Function

LAST_VALUELAST_VALUE(Expression [, Expression ...])
More about LAST_VALUE Function

ROWNUMROWNUM()

ROW_COUNTROW_COUNT()
More about ROW_COUNT Function

SCHEMASCHEMA()
More about SCHEMA Function

SESSION_USERSESSION_USER()
More about SESSION_USER Function

SYSTEM_USERSYSTEM_USER()
More about SYSTEM_USER Function

USERUSER()
More about USER Function

VERSIONVERSION()
More about VERSION Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the DATABASE SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer