A database is a structured set of data stored electronically. The concept of a database was known to our ancestors even when there were no computers. However, creating and maintaining such databases was a very tedious job. For instance, in a manual database of 100 pages, searching for all employees whose salaries were less than 10,000 would have been quite difficult.
In today’s world, you cannot escape databases. Right now, millions of databases are working around the world to store and fetch data of every kind, whether it be strategic data, employee records, or web technologies.
Databases are often termed as backend processes because they are neither visible to end users nor do end users interact directly with the database. Instead, they work on frontend processes like PHP, VB, ASP.NET, etc., and ask the frontend to deal with the database in the backend.
There are several database servers and clients available, such as Oracle, MySQL, MySQLi, MariaDB, and MongoDB. The syntax for all of these is more or less the same. Mastering one means gaining control over most of them, and learning the queries of a database is both easy and enjoyable.
Let’s start with simple queries on databases. We will use MySQL, which comes bundled with most Linux distributions by default. You can install it manually from the repository if it is not installed by default in your case.
A database query is a simple piece of code sent to the database to obtain custom and refined results as required.
Install MySQL Database in Linux
Use the “yum” or “apt” package manager to install the MySQL database.
Once installed, start the MySQL database service with:
Installing a fresh copy of the MySQL database will take you to a configuration step where you will be asked to set up an root password and answer questions regarding security settings.
Once you finish installing and securing the server, go to your MySQL prompt.

Now, executing queries at this prompt is both educational and enjoyable.
Create a MySQL Database
Create a database named “tecmint“.
Note: The message indicates that the query was successful, meaning the database is created.
You can verify your newly created database by running:
Notice your database in the output below.

Create Tables in MySQL Database
Now you need to select the database to work on:
Here we will create a table called “minttec” with three fields:
Note: The above query returns “OK“, indicating that the table was created without any errors.
To verify the table, run the following query:
You can view the columns you created in the “minttec” table as follows:

Anyway, let me explain the types of declarations and their meanings.
INTis an Integer.VARCHARis a character type with a variable length as defined. The value after the type indicates the maximum length of the field in which it can store data.
Add Columns in MySQL Database
Now, we need to add a column named ‘last_name‘ after the column ‘first_name‘:
Verify the change in your table:

Now we will add a column named ‘country‘ to the right of the email field:
Verify the column addition:

Insert Values into Fields of MySQL Database
Now let’s insert values into the fields:
Now let’s insert multiple values at once into the table.
Verify the inserted values:

The values have been inserted successfully.
Update Values in MySQL Table
Now, how about changing the last name of the user whose first name is “Narad“?
Check to verify the changes.

Delete Values from MySQL Table
What about deleting a row from the table? For example, let’s delete the last entry of the user whose first name is “tecmint“.
Now, check the entries after the deletion.

Now, as you can see, the user “tecmint” has been deleted successfully.
Rename Table in MySQL
To rename a table in MySQL, you can use the RENAME TABLE statement. Let’s say you want to rename the table minttec to users. You would run the following command.
After renaming the table, you can verify the change by listing the tables in your current database:

Backup a Database
To back up a MySQL database, you can use the mysqldump command, which creates a logical backup by generating a SQL script file containing all the commands to recreate the database.
You can verify that the backup file was created by listing the files in your current directory:
Restore a Database
To restore a MySQL database from a backup file, you can use the mysql command.
Make sure the database you are trying to restore to already exists. If it doesn’t, you can create it using:
No comments:
Post a Comment