Learn MySQL: Create, Update, and Delete Data in Database

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.

sudo yum install mysql mysql-client mysql-server  (on Yum-based systems)
sudo apt install mysql mysql-client mysql-server  (on Apt-based systems)

Once installed, start the MySQL database service with:

sudo systemctl start mysqld
Or
sudo systemctl start mysql

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.

sudo mysql_secure_installation

Once you finish installing and securing the server, go to your MySQL prompt.

sudo mysql -u root -p
MySQL Shell Prompt
MySQL Shell Prompt

Now, executing queries at this prompt is both educational and enjoyable.

Create a MySQL Database

Create a database named “tecmint“.

create database tecmint;

Note: The message indicates that the query was successful, meaning the database is created.

You can verify your newly created database by running:

show databases; 

Notice your database in the output below.

Create a MySQL Database
Create a MySQL Database

Create Tables in MySQL Database

Now you need to select the database to work on:

use tecmint;

Here we will create a table called “minttec” with three fields:

CREATE TABLE minttec (
    id INT(3), 
    first_name VARCHAR(15), 
    email VARCHAR(20)
);

Note: The above query returns “OK“, indicating that the table was created without any errors.

To verify the table, run the following query:

show tables; 

You can view the columns you created in the “minttec” table as follows:

Create Tables in MySQL Database
Create Tables in MySQL Database

Anyway, let me explain the types of declarations and their meanings.

  • INT is an Integer.
  • VARCHAR is 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‘:

ALTER TABLE minttec ADD last_name VARCHAR(20) AFTER first_name;

Verify the change in your table:

show columns from minttec; 
Add Column in MySQL Database
Add Column in MySQL Database

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

ALTER TABLE minttec ADD country VARCHAR(15) AFTER email; 

Verify the column addition:

show columns from minttec; 
Verify Column in MySQL Database
Verify Column in MySQL Database

Insert Values into Fields of MySQL Database

Now let’s insert values into the fields:

INSERT INTO minttec VALUES ('1', 'Ravi', 'Saive', '[email protected]', 'India');

Now let’s insert multiple values at once into the table.

INSERT INTO minttec (id, first_name, last_name, email, country) VALUES 
  ('2', 'Narad', 'Shrestha', '[email protected]', 'India'), 
  ('3', 'user', 'singh', '[email protected]', 'Aus'), 
  ('4', 'tecmint', 'com', '[email protected]', 'India');

Verify the inserted values:

select * from minttec; 
Verify Values in MySQL Database
Verify Values in MySQL Database

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“?

UPDATE minttec SET last_name = 'Shrestha' WHERE first_name = 'Narad';

Check to verify the changes.

select * from minttec; 
Update Values in Table
Update Values in Table

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“.

DELETE FROM minttec WHERE first_name = 'tecmint';

Now, check the entries after the deletion.

select * from minttec; 
Delete Values in Table
Delete Values in Table

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.

RENAME TABLE minttec TO users;

After renaming the table, you can verify the change by listing the tables in your current database:

SHOW TABLES;
Rename Table in Database
Rename Table in 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.

mysqldump -u root -p tecmint > tecmint_backup.sql

You can verify that the backup file was created by listing the files in your current directory:

ls -l tecmint_backup.sql

Restore a Database

To restore a MySQL database from a backup file, you can use the mysql command.

mysql -u root -p tecmint < tecmint_backup.sql

Make sure the database you are trying to restore to already exists. If it doesn’t, you can create it using:

mysql -u root -p -e "CREATE DATABASE tecmint;"
Conclusion

You have now learned the basics of creating and manipulating a MySQL database, including creating a database, creating tables, inserting records, updating records, deleting records, and dropping tables. This foundation is essential for working with databases effectively.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

48 Comments

Leave a Reply
  1. Moreover: “Note: The above query, as performed is not a good idea. It will change the id to ‘4‘ where ever the first name is ‘tecmint’. “. Should it be “3” instead of “4”?

    Reply
  2. The second time this command is run (“UPDATE minttec SET id = 3 WHERE first_name = ‘tecmint’;”) it should show “Change: 0”, and not “Change: 1”. That’s the whole point, isn’t it? Or am I getting it wrong?

    Reply
  3. It is a pity that I am using this site from a third world country, otherwise I would donate generously to Tecmint. Tecmint is one of few websites that tutors and teaches people just exactly how teaching should be delivered. I stumbled on MySQL tutorial at tecmint.com, as a beginner I followed the steps from installation of MySQL to creating database, table, and from there to creating records and manipulating the records on the database through different queries; the result of which is 100% accurate. I think one day I will be able to donate. This is a 5 stars rating. Please explain installation and administration of MySQL on Microsoft Windows platform. Thank you tecmint.com

    Reply
    • @Jelil,

      Thanks for appreciating our work, we will continue to work hard and produce 100% working quality articles for users like you. Keep connecting and support tecmint..

      Reply
    • @Anand,

      Just connect to MySQL prompt and type show databases; query to get all the available databases in MySQL, if you want to see which database is curring running, you can use show processlist; to see currently running queries on database.

      Reply
  4. A very simple to read and understand article for the basic commands in mysql database. A helpful stuff for the beginners. This is what i exactly needed for my interview. Thank you sir.

    Reply
  5. Very simple article, yet a nice page to bookmark for a quick review of a few basic commands.

    Mentioning the backup & restore commands is definitely a plus!

    Reply
  6. typo in the apt install command:
    # apt-get install mysql mysql-client mysql-serevr (on Apt based Systems)
    should be
    # apt-get install mysql mysql-client mysql-server (on Apt based Systems)

    Reply
  7. Hello Tecmint,

    I was just having a doubt that I heard, MySQL has been purchased by Oracle now. Does it can cause any problem to those Linux servers on which MySQL is installed ?

    Thank You in advance.

    Regards,
    Suvarna

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.