MariaDB - Tutorial For Beginners
MariaDB - Tutorial For Beginners
MariaDB - Tutorial For Beginners
EXAMPLES
What is MariaDB?
MariaDB is a fork of the MySQL database management system. It is created by its original
developers. This DBMS tool offers data processing capabilities for both small and enterprise tasks.
MariaDB is an improved version of MySQL. It comes with numerous inbuilt powerful features and
many usabilities, security and performance improvements that you cannot find in MySQL.
What is MariaDB?
MariaDB vs. MySQL
Installation of MariaDB
Working with Command Prompt
Data Types
Create a Database and Tables
Selecting a Database
Creating a Table
CRUD and Clauses
Advanced Tasks
JOIN
More Options for MariaDB has 12 new storage engines that you won't It has fewer options for storage
Storage Engines find in MySQL. compared to MariaDB.
Speed Improvements MariaDB shows an improved speed when compared MySQL exhibits a slower speed
to MySQL. It comes with numerous features for when compared to MariaDB. It
optimizing speed. Such features include derived relies on only a few features for
views/tables, subquery, execution control, disk speed optimization, for example,
access, and optimizer control. hash indexes.
Faster cache/indexes With the Memory storage engine of MariaDB, an The memory storage engine of
INSERT statement can be completed 24% than in MySQL is slower compared to that
the standard MySQL. MariaDB.
Larger and Faster MariaDB comes with an advanced thread pool The thread pool provided by
Connection Pool capable of running faster and supporting up to MySQL cannot support up to
200,000+ connections. 200,000 connections per time.
Improved Replication In MariaDB, replication can be done safer and faster. MySQL's community edition allows
Updates can also be done 2x faster compared to the a static number of threads to be
traditional MySQL. connected. MySQL's enterprise plan
comes with thread capabilities.
New MariaDB comes with new features and extensions The new MariaDB features are not
Features/Extensions including the JSON, WITH and KILL statements. provided in MySQL.
Missing Features MariaDB lacks some of the features provided by the The Enterprise Edition of MySQL
MySQL enterprise edition. To address this, it offers uses a proprietary code. Only users
alternative open-source plugins. Hence, MariaDB of MySQL Enterprise Edition have
users are able to enjoy the same functionalities as access to this.
MySQL Enterprise Edition users.
Installation of MariaDB
Install as a Standalone Application
In order to use MariaDB, you have to install it on your computer.
https://downloads.mariadb.org/
Step 2) Once the download is complete, double click the file to start the installation.
Step 3) On the window that pops up, click the Next button:
Step 4) Next, accept the license agreement by activating the checkbox then click the Next button:
Step 5) Next, choose the features that are to be installed and click the Next button.
Step 6) In the next window, you will be required to change the password for the root user.
1. Enter the password and confirm it by retyping the same password. If you want to permit
access from remote machines, activate the necessary checkbox.
2. Once done, click the Next button.
Step 7) In the next window, type a name for the instance, choose the port number, and set the
necessary size. Click the Next button:
Step 8) In the next window, simply click the Next button.
Step 9) Launch the installation by clicking the Install button.
Step 10) A progress bar showing the progress of the installation will be shown:
Step 11) Once the installation is complete, you will see the Finish button. Click the button to close
the window:
Step 12) Congratulations! You now have MariaDB installed on your computer.
MySQL -u root -p
Step 4) Enter the password and hit the return key. You should be logged in, as shown below:
Data Types
MariaDB supports the following data types:
To create a new database, you should use the CREATE DATABASE command which takes the
following syntax:
In this case, you need to create a database and give it the name Demo.
Start the MariaDB command prompt and login as the root user by typing the following command:
mysql -u root -p
Type the root password and hit the return key. You will be logged in.
You have then created a database named Demo. It will be good for you to confirm whether the
database was created successfully or not. You only have to show the list of the available databases
by running the following command:
SHOW DATABASES;
The above output shows that the Demo database is part of the list, hence the database was created
successfully.
Selecting a Database
For you to be able to use or work on a particular database, you have to select it from the list of the
available databases. After selecting a database, you can perform tasks such as creating tables within
the database.
To select a database, you should use the USE command. It takes the syntax given below:
USE database_name;
You need to use the Demo database. You can select it by running the following command:
USE Demo;
The above image shows that the MariaDB command prompt has changed from none to the name of
the database that has been selected.
You can now go ahead and create tables within the Demo database.
Creating a Table
For you to be able to create a table, you must have selected a database. The table can be created
using the CREATE TABLE statement. Here is the syntax for the command:
You can set one of the columns to be the primary key. This column should not allow null values.
We will create two tables within the Demo database, Book, and Price tables. Each table will have
two columns.
Let's begin by creating the Book table with two columns, id and name. Run the following
command:
The id column has been set as the primary key for the table.
Showing Tables
Now that you have created the two tables, it will be good for you to conform whether the tables
were created successfully or not. You can show the list of tables contained in a database by running
the following command:
SHOW TABLES;
The above screenshot shows that the two tables were created successfully within the Demo
database.
For example, to see the structure of the table named Book, you can run the following command;
DESC Book;
The table has two columns. To see the structure of the Price table, you can run the following
command:
DESC Price;
The above syntax shows that you have to specify the table columns into which you want to insert
data as well as the data that you need to insert.
You have inserted a single record into the table. Insert a record into the Price table:
SELECT
The SELECT statement helps us to view or see the contents of a database table. To see the contents
of the Book table, for example, you need to run the following command:
You can query the table to check whether the records were inserted successfully:
The records were inserted successfully. Insert multiple records into the Price table by running this
example:
UPDATE
The UPDATE command helps us to change or modify the records that have already been inserted
into a table. You can combine it with the WHERE clause to specify the record that is to be updated.
Here is the syntax:
The UPDATE command can also be combined with clauses such as SET, WHERE, LIMIT, and
ORDER BY. You will see this shortly:
Let's change the price of the book with an id of 1 from 200 to 250:
UPDATE price
SET price = 250
WHERE id = 1;
The command ran successfully. You can now query the table to see whether the change took place:
The above screenshot shows that the change has been implemented. Consider the table Book with
the following records:
Let us change the name of the book named Book to MariaDB Book1. Notice that the book has an id
of 1. Here is the command for this:
UPDATE book
SET name = “MariaDB Book1”
WHERE id = 1;
In the above examples, we have only changed one column at a time. However, it is possible for us
to change multiple columns at a go. Let us demonstrate this using an example.
Let us change both the id and the price of the book with an id of 5. We will change its id to 6 and
price to 6. Run the following command:
UPDATE price
SET id = 6,
price = 280
WHERE id = 5;
Now, query the table to check whether the change was made successfully:
The change was made successfully.
Delete
We use the DELETE command when we need to delete either one or many records from a table.
Here is the syntax for the command:
We need to delete the last record from the table. It has an id of 6 and a price of 280. Let us delete
the record:
The command ran successfully. Let us query the table to confirm whether the deletion was
successful:
The output shows that the record was deleted successfully.
Where
The WHERE clause helps us to specify the exact location where we need to make a change. It is
used together with statements such as INSERT, SELECT, UPDATE, and DELETE. Consider the
Price table with the following data:
Suppose we need to see the records in which the price is less than 250. We can run the following
command:
SELECT *
FROM price
WHERE price < 250;
All the records in which the price is below 250 have been returned.
The WHERE clause can be combined with the AND statement. Suppose we need to see all records
in the Price table where the price is below 250 and id is above 3. We can run the following
command:
SELECT *
FROM price
WHERE id > 3
AND price < 250;
Only one record has been returned. The reason is that it has to meet all the conditions that have
been specified, that is, id above 3 and price below 250. If any of these conditions is violated, then
the record will not be returned.
The clause can also be combined with the OR command. Let us replace the AND in our previous
command with OR and see the kind of output that we receive:
SELECT *
FROM price
WHERE id > 3
OR price < 250;
We now get 2 records rather than 1. This is because, for a record of qualifying, it only has to meet
one of the specified conditions.
Like
This clause is used to specify the data pattern when accessing table data in which an exact match is
necessary. It can be combined with the INSERT, UPDATE, SELECT and DELETE statements.
You should pass the pattern of data you are looking for to the clause, and it will return either true or
false. Here are the wildcard characters that can be used together with the clause:
Let us demonstrate how to use the clause with the % wildcard character. Let us use the Book table
with the following records:
We need to see all records in which the name begins with M. We can run the following command:
SELECT name
FROM book
WHERE name LIKE 'M%';
All records have been returned because their names begin with the letter M. To see all names that
end with 4, you can run the following command:
SELECT name
FROM book
WHERE name LIKE '%4';
Only one name has been returned because it's the only one meeting the condition.
SELECT name
FROM book
WHERE name LIKE '%DB%';
Other than the % wildcard, the LIKE clause can be used together with the _ wildcard. This is the
underscore wildcard, and it will only look for a single character.
Let's work with the Price table with the following records:
Let us check for the record in which the price is like 1_0. We run the following command:
SELECT *
FROM price
WHERE price LIKE '1_0';
It has returned the record in which the price is 190. We can also try another pattern:
SELECT *
FROM price
WHERE price LIKE '_2_';
It is possible for us to use the LIKE clause together with the NOT operator. This will return all the
records that don't meet the specified pattern. For example:
Let us find all the records where the price does not start with 2:
SELECT *
FROM price
WHERE price NOT LIKE '2%';
Only one record does not meet the specified pattern.
Order By
This clause helps us to sort out our records in either ascending or descending order. We use it with
the SELECT statement, as shown below:
SELECT expression(s)
FROM tables
[WHERE condition(s)]
ORDER BY exp [ ASC | DESC ];
It is possible for us to use this clause without adding either the ASC or DESC part. For example:
The records have been sorted with the price in descending order as we have specified.
Let us use the ORDER BY clause together with the ASC attribute:
The records have been ordered but with the prices in ascending order. This is similar to when we
use the ORDER BY clause without either ASC or DESC attributes.
DISTINCT
This clause helps us to do away with duplicates when selecting records from a table. This means
that it helps us get unique records. Its syntax is given below:
When we select the price column from the table, we get the following result:
We have two records with a price of 250, creating a duplicate. We need to have only unique
records. We can filter these by use of the DISTINCT clause as shown below:
From
The FROM clause used for fetching data from a database table. It can also help when joining
tables. Here is the syntax for the command:
To see the contents of the book table, run the following command:
SELECT * FROM price;
The clause can help you to fetch only a single column from a database table. For example:
Advanced Tasks
Stored Procedure
A procedure is a MariaDB program that you can pass parameters to. A procedure doesn't return
values. To create a procedure, we use the CREATE PROCEDURE command.
To demonstrate how to create and call a procedure, we will create a procedure named
myProcedure() that helps us select the name column from the book table. Here is the procedure:
DELIMITER $$
CREATE PROCEDURE myProcedure()
BEGIN
SELECT name FROM book;
END;
;
The procedure has been created. We have simply enclosed the SELECT statement within the
BEGIN and END clauses of the procedure.
CALL myProcedure();
The procedure returns the name column of the book table when called.
We can create a procedure that takes in a parameter. For example, we need to select the name of the
book and filter using the book id. We can create the following procedure for this:
DELIMITER $$
CREATE PROCEDURE myProcedure2(book_id int)
BEGIN
SELECT name FROM book WHERE id = book_id;
END;
;
Above, we have created a procedure named myProcedure2(). This procedure takes one integer
parameter named book_id which is the id of the book whose name we need to see. To see the name
of the book with an id of 3, we can call the procedure as follows:
CALL myProcedure2(3);
Function
Unlike procedures, we must pass parameters to functions and a function must return a value. To
create a function in MariaDB, we use the CREATE FUNCTION statement. The statement takes the
following syntax:
CREATE
[ DEFINER = { CURRENT-USER | username } ]
FUNCTION function-name [(parameter datatype [, parameter datatype]) ]
RETURNS datatype [LANGUAGE SQL
| DETERMINISTIC
| NOT DETERMINISTIC
| {CONTAINS SQL
| NO SQL
| READS SQL DATA
| MODIFIES SQL DATA}
| SQL SECURITY {DEFINER | INVOKER}
| COMMENT 'comment'
BEGIN
declaration-section
executable-section
END;
Parameter Description
DEFINER clause This parameter is optional. If you don't specify it, the
definer will become the user who created the function. If
there is a need to specify a different definer, include the
DEFINER clause in which the user_name will be the
definer of the function.
function_name The name that is to be assigned to this function in the
MariaDB.
DETERMINISTIC The function will return one result only when given a
number of parameters.
CONTAINS SQL Informs MariaDB that this function contains SQL. The
database will not verify whether this is true.
READS SQL DATA Tells MariaDB that this function will use SELECT
statements to read data, but it won't modify the data.
MODIFIES SQL DATA Tells MariaDB that this function will use INSERT,
DELETE, UPDATE, and other DDL statements to
modify SQL data.
BEGIN
select sumFunc(1000);
Once you are done with a function, it will be good for you to delete it. This is easy as you only have
to call the DROP FUNCTION statement that takes the following syntax:
For example, to drop the function named myFunc, we can run the following command:
DROP FUNCTION myFunc;
JOIN
When you need to retrieve data from more than one tables at a go, use MariaDB JOINS. This
means that a JOIN works on two or more tables. The following three types of JOINS are supported
in MariaDB:
INNER/SIMPLE JOIN
LEFT OUTER JOIN/LEFT JOIN
RIGHT OUTER JOIN/RIGHT JOIN
INNER JOIN
The inner join returns all rows from the tables in which the join condition is true. Its syntax is as
follows:
SELECT columns
FROM table-1
INNER JOIN table-2
ON table-1.column = table-2.column;
For example:
SELECT columns
FROM table-1
LEFT [OUTER] JOIN table-2
ON table-1.column = table-2.column;
The OUTER keyword has been placed within square brackets because it is optional.
For example:
The last record in the above table has no matching value on the left. That is why it has been
replaced with NULL.
SELECT columns
FROM table-1
RIGHT [OUTER] JOIN table-2
ON table-1.column = table-2.column;
The OUTER keyword has been placed within square brackets because it is optional.
For example: