0
0
MySQLquery~5 mins

Database creation and selection in MySQL

Choose your learning style9 modes available
Introduction
We create databases to store and organize data. We select a database to tell the system which data we want to work with.
When starting a new project and you need a place to store data.
When you want to organize data into separate groups for different purposes.
When you want to switch between different sets of data in your system.
When you want to prepare your system to add tables and data.
When you want to make sure you are working with the right data before running queries.
Syntax
MySQL
CREATE DATABASE database_name;
USE database_name;
Replace database_name with the name you want for your database.
The USE command tells MySQL to work with the chosen database.
Examples
Creates a database named 'school' and selects it for use.
MySQL
CREATE DATABASE school;
USE school;
Creates a database named 'shop' and switches to it.
MySQL
CREATE DATABASE shop;
USE shop;
Creates 'library' only if it does not exist, then selects it.
MySQL
CREATE DATABASE IF NOT EXISTS library;
USE library;
Sample Program
This creates a database named 'testdb' if it doesn't exist and then selects it for use.
MySQL
CREATE DATABASE IF NOT EXISTS testdb;
USE testdb;
OutputSuccess
Important Notes
Database names should be simple and meaningful.
You can check existing databases with the command: SHOW DATABASES;
Always select the database before creating tables or inserting data.
Summary
Create a database to store your data.
Use the database to tell MySQL which data to work with.
Use 'CREATE DATABASE' and 'USE' commands for these tasks.