SQL - interview
Q1. What is the CREATE DATABASE command in SQL?
The CREATE DATABASE command is used to create a new database in a relational database management system like MySQL. A database acts as a container that holds tables, views, procedures, and other database objects.
Before storing any data, it is necessary to create a database so that all related tables and records remain organized. Each database is independent and can store data for a specific application or project.
When this command is executed, the database system allocates storage space and registers the database internally.
Example:
This command creates a database named SchoolDB, which can later contain tables such as Students, Teachers, and Courses.
Before storing any data, it is necessary to create a database so that all related tables and records remain organized. Each database is independent and can store data for a specific application or project.
When this command is executed, the database system allocates storage space and registers the database internally.
Example:
CREATE DATABASE SchoolDB;This command creates a database named SchoolDB, which can later contain tables such as Students, Teachers, and Courses.
Q2. What is the USE command and why is it required?
The USE command is used to select a database and make it the active working database. In MySQL, you can have multiple databases, but you can work with only one database at a time.
After executing the USE command, all subsequent SQL commands such as CREATE TABLE, INSERT, UPDATE, or SELECT will apply to the selected database.
Without using the USE command, MySQL does not know which database you want to work with, and it may result in errors.
Example:
This tells MySQL that all upcoming operations should be performed inside the SchoolDB database.
After executing the USE command, all subsequent SQL commands such as CREATE TABLE, INSERT, UPDATE, or SELECT will apply to the selected database.
Without using the USE command, MySQL does not know which database you want to work with, and it may result in errors.
Example:
USE SchoolDB;This tells MySQL that all upcoming operations should be performed inside the SchoolDB database.
Q3. Explain the DROP DATABASE command and its risks.
The DROP DATABASE command is used to permanently delete an existing database along with all its tables, data, and objects.
This command is dangerous because once executed, the database and all its data are lost permanently and cannot be recovered using SQL rollback commands.
Because of this risk, the DROP DATABASE command should always be executed carefully, especially in production environments.
Example:
This command deletes the SchoolDB database completely from the system.
This command is dangerous because once executed, the database and all its data are lost permanently and cannot be recovered using SQL rollback commands.
Because of this risk, the DROP DATABASE command should always be executed carefully, especially in production environments.
Example:
DROP DATABASE SchoolDB;This command deletes the SchoolDB database completely from the system.
Q4. What is the purpose of SHOW DATABASES?
The SHOW DATABASES command is used to display a list of all databases available in the MySQL server.
It is commonly used to:
Example:
It is commonly used to:
- Verify whether a database has been created successfully
- Check existing databases before selecting or deleting one
- Avoid accidental deletion of important databases
Example:
SHOW DATABASES;Q5. Explain database management commands using a real-life analogy.
A database can be compared to a folder on your computer.
- CREATE DATABASE - Creating a new folder
- USE DATABASE - Opening the folder to work inside it
- DROP DATABASE - Deleting the folder permanently
- SHOW DATABASES - Viewing all folders on the system