- 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 - SELECT Database, USE Statement
To work with a database in SQL, we need to first select the database we want to work with using the USE statement. The USE statement in SQL is used to specify the name of the database we want to select. After selecting the database, we can perform various operations on it such as creating tables, inserting data, updating data, and deleting data.
To retrieve the data from the database, we can use the SELECT statement in SQL, which is used to retrieve data from one or more tables.
Syntax
Following is the syntax of the USE statement in SQL −
USE DatabaseName;
Here, the “DatabaseName” is the name of the database that we want to create.
Always the database name should be unique within the RDBMS.
Example
First of all we are creating a database in the database system using the following query −
SQL> CREATE DATABASE testDB;
We can pst all the available databases using the SELECT statement as shown below −
SQL> SELECT * FROM SYS.DATABASES; +------------+ | Database | +------------+ | master | | tempdb | | model | | msdb | | testDB | +------------+
Following query is used to select/switch the current database to <testDB> −
SQL> USE testDB;
Output
Commands completed successfully.
Once we finish switching to the database <testDB> we can perform operations such as creating a table as shown below −.
SQL> CREATE TABLE CALENDAR(MONTHS DATE NOT NULL);
Now, let us insert some records in the CALENDAR table using INSERT statements as shown in the query below −
SQL> INSERT INTO CALENDAR(MONTHS) VALUES( 2023-01-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-02-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-03-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-04-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-05-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-06-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-07-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-08-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-09-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-10-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-11-01 ); INSERT INTO CALENDAR(MONTHS) VALUES( 2023-12-01 );
Let’s verify whether the table is created or not using the SELECT statement as shown below −
SQL> SELECT * FROM CALENDAR;
Output
The table CALENDAR is successfully created.
+-------------+ | MONTHS | +-------------+ | 2023-01-01 | | 2023-02-01 | | 2023-03-01 | | 2023-04-01 | | 2023-05-01 | | 2023-06-01 | | 2023-07-01 | | 2023-08-01 | | 2023-09-01 | | 2023-10-01 | | 2023-11-01 | | 2023-12-01 | +-------------+
In SSMS
The following image shows that the table (CALENDAR) we created right now is stored inside the database(testDB) that we made as default database.
Selecting a non existent database
An attempt to select a non-existent database will result in an error. In the following query we are trying to switch to the database which does not exist −
SQL> CREATE DATABASE unknownDatabase;
On executing the above query, the output is displayed as follows −
Database unknownDatabase does not exist. Make sure that the name is entered correctly.Advertisements