MySQL MariaDB

MySQL Commands Tutorial

MySQL is a database that is behind many applications nowadays. This relational database can be accessed from the terminal and helps dev-ops engineers and system administrators. In this article, we will try out some of the most common MySQL commands in the Linux terminal.

Connect to MySQL

If you have already installed MySQL on your Ubuntu machine, you can easily connect to the MySQL shell by issuing the following command in your Linux terminal.
[cc lang=”bash” width=”100%” height=”100%” escaped=”true” theme=”blackboard” nowrap=”0″]
$ sudo mysql -u root -p
[/cce_bash]
In the above command:

  • -u is the user
  • root is the MySQL username
  • -p is the password

After running the above command, you will be asked to enter the password. Once you have entered the root user password, you will be logged in to the MySQL shell.

If you have recently installed MySQL, then you will not be asked for the password.

Set or Change Password

To set or change the password for any MySQL database user, issue the following command:

mysql> ALTER USER 'user_name'@'localhost' IDENTIFIED BY 'new_password';

Replace the user_name and new_password with your username and desired password. For example:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';

View Databases

In MySQL, you can view the list of databases under the user’s granted privileges by running the simple command stated below:

mysql> SHOW DATABASES;

The above command will list all the databases.

Create a Database

You can create a database by issuing the following command in the MySQL shell:

mysql> CREATE DATABASE IF NOT EXISTS database_name;

The above statement will not create a database if there already exists a database with the name that has been provided. For example:

mysql> CREATE DATABASE IF NOT EXISTS db1;

As you can see in the screenshot, the db1 database has been created.

Select a Database

You can use any of the listed databases by running the following command in the MySQL shell:

mysql> USE DATABASE db1;

Create a Table

A table can be created by using the CRETE TABLE command and defining the column names and their types in the parentheses, as follows:

mysql> CREATE TABLE IF NOT EXISTS tb1 (
col1 INT,
col2 VARCHAR(20),
...
PRIMARY KEY (col1)
);

In this creation query:

  • tb1 is the name of the table
  • col1, col2 are the names of the columns in the tb1 table
  • INT and VARCHAR are the datatypes of the specified columns
  • col1 is defined as the primary key

View Tables

To list all the tables in the present database, run the simple SHOW TABLES command in the MySQL shell.

mysql> SHOW TABLES;

View Table Structure

The structure of the table, including column names, their types, and their default values, can be seen by running the DESC command.

mysql> DESC tb1;

Insert Data

Data can be inserted into the table by providing the column names into which you want to insert data, as well as the data or values, using the following syntax:

mysql> INSERT INTO tb1(col1, col2)
VALUES (1, "value1"),
(2, "value2");

View Table Data

The SELECT statement is used to view the data in a table. You can either provide the asterisk symbol (*) to select all the columns, or you can provide the specific columns that you want to view.

mysql> SELECT * FROM tb1;
mysql> SELECT col2 FROM tb1;


Delete Data from Table

To delete a row from a table, provide the table name to the DELETE FROM command, along with the WHERE conditional clause.

mysql> DELETE FROM tb1 WHERE col1 = 1;

Delete a Table

Deleting a table is as easy as saying it. You can delete any table in the present database by running the statement given below.

mysql> DROP TABLE tb1;


Delete a Database

Deleting or dropping a database is the same as deleting a table in MySQL. Simply use the DROP DATABASE command with the database name.

mysql> DROP DATABASE IF EXISTS db1;

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.