SQL - Show Database


A database is a collection of data stored and organized in a way that the data can be retrieved, inserted, and deleted. Nowadays, databases are used by most organizations to store data such as financial transactions, customer records, employee records, etc.

Database in SQL

In an SQL server, the database is a collection of tables, views, stored procedures, and other objects that are used to store and manage data.

When we install the SQL server, some databases are automatically created; hence, they are called “System Databases”. The databases which are created by the user are called “User Databases”.

  • System Databases − The system databases such as Master, Model, MSDB, Tempdb, etc. are created automatically when we install the MS SQL Server on our system. These system databases are crucial for the functioning of the MS SQL Server and these cannot be deleted or dropped.

  • User Databases − The user databases in the SQL server are created and managed by the database users.

Listing databases in SQL server

There is no particular command in SQL to show or list the databases that exist in an SQL server. So, we use the SELECT…FROM command.

The “SELECT…FROM” command is used to retrieve records from the sys.databases table. This table contains all the information about the databases in the SQL server such as name of the database, database_id, owner_sid, create_date, compatibility_level, etc.

SELECT command is one of the T-SQL commands. The Transact-SQL or T-SQL Commands in SQL are used to interact with the SQL server databases.

Syntax

Following is the syntax of the SELECT…FROM command to list the databases −

SELECT column_name, column_name1,…… FROM sys.databses;

Here, the “column_name” is the name of the columns from the sys.databases table.

Example

First of all, let us try to list the databases that are present in the SQL server using the query below −

SQL> SELECT * FROM sys.databases;
+----------+-------------+--------------------+-------------+
|   name   | database_id | source_database_id | owner_sid   |
+----------+-------------+--------------------+-------------+
|  master  | 1           |  NULL              | 0×01        |
|  tempdb  | 2           |  NULL              | 0×01        |  
|  model   | 3           |  NULL              | 0×01        |
|  msdb    | 4           |  NULL              | 0×01        |
+----------+-------------+--------------------+-------------+

Now, let us create some databases in the SQL server using the query below −

SQL> CREATE DATABASE testDB1;
CREATE DATABASE testDB2;
CREATE DATABASE testDB3;

Note − Since the above databases are created by the user, they are called "user databases".

Once the databases are created, you can list all the databases that are present in the SQL server using the query below −

SQL> SELECT * FROM sys.databases;

Output

If we execute the above query, it returns a table that lists all the databases and also the information about the databases.

+----------+-------------+--------------------+-------------+
|   name   | database_id | source_database_id | owner_sid   |
+----------+-------------+--------------------+-------------+
|  master  | 1           |  NULL              | 0×01        |
|  tempdb  | 2           |  NULL              | 0×01        |  
|  model   | 3           |  NULL              | 0×01        |
|  msdb    | 4           |  NULL              | 0×01        |
|  testDB1 | 5           |  NULL              | 0x010500……  |
|  testDB2 | 6           |  NULL              | 0x010500……  |
|  testDB3 | 7           |  NULL              | 0x010500……  |
+----------+-------------+--------------------+-------------+

Stored procedure to show all the Databases

Following is the query to show all the databases that are present in the SQL server using the stored procedure −

SQL> EXEC sp_databases;

Output

After executing the above query, it returns a table that contains all the databases and also the information about the databases.

+---------------+---------------+--------+
| DATABASE_NAME | DATABASE_SIZE | REMARKS|
+---------------+---------------+--------+
| master        |  5376         |  NULL  |
| model         |  16384        |  NULL  | 
| masdb         |  16960        |  NULL  |
| tempdb        |  16384        |  NULL  |
| testDB1       |  16384        |  NULL  |
| testDB2       |  16384        |  NULL  |
| testDB3       |  16384        |  NULL  |
+---------------+---------------+--------+

Showing selected information about the databases

The sys.databases table consists of several columns such as name, database_id, source_database_id, owner_sid, create_date, compatibility_level, etc.

Using the below query, we are trying to show only some particular columns from the sys.databases table like name and compatibility_level.

SQL> SELECT name, compatibility_level FROM sys.databases;

Output

If we execute the above query, the result is produced as follows −

+-----------+--------------------+
| name      | compatibility_level|
+---------------+----------------+
| master    |  160               |
| model     |  160               |
| masdb     |  160               |
| tempdb    |  160               |
| testDB1   |  160               |
| testDB2   |  160               |
| testDB3   |  160               |
+---------------+----------------+

Showing only User-Created Databases

The databases that are created by the user on the SQL server are known as "User-created databases".

There is no particular command in SQL to retrieve only the user-created databases from the SQL server instead, we use the query below −

SQL> SELECT name   
FROM sys.databases  
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');

Here, the master, tempdb, model, msdb are the system-created databases. So, we are eliminating them to show the user-created databases.

Output

On executing the given query, the output is displayed as follows −

+-------+
| name  |
+-------+
|testDB1|
|testDB2|
|testDB3|
+-------+

Showing only System-Created Databases

The system databases are automatically created at the time of the SQL server installation.

To show only the system-created databases in the SQL server, we use the following query −

SQL> SELECT name   
FROM sys.databases  
WHERE name NOT IN ('testDB');

Output

When we execute the above query, the output is obtained as follows −

+--------+
| name   |
+--------+
| master |
| model  |
| msdb   |
| tempdb |
+--------+
Advertisements