How to create and select a MySQL database?

Learn, how to create and select a MySQL database?
Submitted by Apurva Mathur, on November 30, 2022

MySQL is simply a database management system, the two terms which are important here is database and management. So, the database is a collection of data, we can assume it is a hard copy file but when it comes to manage the data electronically the database comes into the picture. And, the other term is management which means a way to store that crucial data.

What is Database?

A database is a collection of data, we can assume it is a hard copy file but when it comes to manage the data electronically then the database comes into the picture.

You can consider a database is like a folder where you'll store all your tables and under your tables, you'll have your rows and column

How to Create a MySQL Database?

Let us see how we can create a database by writing a SQL query in the MySQL command line client.

If you are a beginner, then I strongly suggest that work on MySQL command line client as this is the best way to learn how queries work when we fire them.

Syntax:

Here is the syntax to create a database,

CREATE DATABASE database_name;

Here, DATABASE_NAME can be anything but while writing the DATABASE_NAME kindly keep the following things in your mind,

  • Always start your database name with an alphabet.
  • Do not start your name with any space, numeric value, or symbols.
  • Spaces are allowed in the database but only between the characters.
  • It is always considered a good practice when you use underscore _ in place of space. For example, student_details looks good than Student Details.
  • Database names are case sensitive which means if you have written your database name in camel case so whenever you are using the database name in a query you have to write the name in camel case only, else it will show you an error.

Now we'll see this in the MYSQL command line client.

MySQL | create and select a database (1)

As you can see 1 row is affected which means our database named as the student has been made.

Now let us see, how we can select a database?

In MySQL command-line client, if we want to work on a specific database then in that case, we have to use the following statement.

To select a database, we can use SQL's USE command.

Syntax:

Follow the given syntax to select (use) a database from the list of multiple databases.

USE DATABASE_NAME;

Example:

MySQL | create and select a database (2)

In the given picture we have many databases and at a time we can work on only a single database. So, if I want to use the "student" database then I'll simply write,

USE student;
MySQL | create and select a database (3)

Note: It is not important to write semicolon with USE command, but if we are creating a table then in such case it is important to use a semicolon at the end of the query.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.