SQL – Creating and Opening Database
1. What is the purpose of the CREATE DATABASE statement in SQL?
Answer:
The CREATE DATABASE statement in SQL is used to create a new database in a relational
database management system (RDBMS). It establishes a new container for storing data
in the form of tables, views, and other database objects.
Syntax: CREATE DATABASE database_name;
Example: CREATE DATABASE SchoolDB;
2. How can you list all the databases available in an RDBMS?
Answer:
To list all the databases in an RDBMS, you can use the SHOW DATABASES; command. This
command retrieves a list of all databases present in the system.
Syntax: SHOW DATABASES;
3. What is the purpose of the USE statement in SQL?
Answer:
The USE statement in SQL is employed to select a specific database to work with. Once a
database is selected using this command, all subsequent SQL operations will be
performed on that database until another database is selected.
Syntax: USE database_name;
Example: USE SchoolDB;
4. How can you create a database only if it does not already exist?
Answer:
To create a database only if it does not already exist, you can use the CREATE DATABASE
IF NOT EXISTS statement. This prevents errors that would occur if a database with the
specified name already exists.
Syntax: CREATE DATABASE IF NOT EXISTS database_name;
Example: CREATE DATABASE IF NOT EXISTS SchoolDB;
5. What is the significance of the SHOW DATABASES command?
Answer:
The SHOW DATABASES command is used to display a list of all databases present in the
RDBMS. It helps users to see the available databases and verify if a specific database
exists.
Syntax: SHOW DATABASES;