- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pst 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, compatibipty_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 pst 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 pst 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 pst 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 psts 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, compatibipty_level, etc.
Using the below query, we are trying to show only some particular columns from the sys.databases table pke name and compatibipty_level.
SQL> SELECT name, compatibipty_level FROM sys.databases;
Output
If we execute the above query, the result is produced as follows −
+-----------+--------------------+ | name | compatibipty_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 epminating 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