When you’re writing an application or configuring one for a server, eventually, you will need to store persistent information. Sometimes, a configuration file, such as an INI or YAML file  will do. Other times, a custom file format designed in XML or JSON or similar is better.

But sometimes you need something that can validate input, search through information quickly, make connections between related data, and generally handle your users’ work adeptly. That’s what a database is designed to do, and MariaDB (a fork of MySQL by some of its original developers) is a great option. I use MariaDB in this article, but the information applies equally to MySQL.

It’s common to interact with a database through programming languages. For this reason, there are SQL libraries for Java, Python, Lua, PHP, Ruby, C++, and many others. However, before using these libraries, it helps to have an understanding of what’s happening with the database engine and why your choice of database is significant. This article introduces MariaDB and the mysql command to familiarize you with the basics of how a database handles data.

If you don’t have MariaDB yet, follow the instructions in my article about installing MariaDB on Linux. If you’re not on Linux, use the instructions provided on the MariaDB download page.

Interact with MariaDB

You can interact with MariaDB using the mysql command. First, verify that your server is up and running using the ping subcommand, entering your MariaDB password when prompted:

$ mysqladmin -u root -p ping
Enter password:
mysqld is alive

To make exploring SQL easy, open an interactive MariaDB session:

$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.
Commands end with ; or \g.
[...]
Type 'help;' or '\h' for help.
Type '\c' to clear the current input statement.

MariaDB [(none)]>

This places you in a MariaDB subshell, and your prompt is now a MariaDB prompt. Your usual Bash commands don’t work here. You must use MariaDB commands. To see a list of MariaDB commands, type help (or just ?). These are administrative commands for your MariaDB shell, so they’re useful for customizing your shell, but they aren’t part of the SQL language.

Learn SQL basics

The Structured Query Language (SQL) is named after what it provides: a method to inquire about the contents of a database in a predictable and consistent syntax in order to receive useful results. SQL reads a lot like an ordinary English sentence, if a little robotic. For instance, if you’ve signed into a database server and you need to understand what you have to work with, type SHOW DATABASES; and press Enter for the results.

SQL commands are terminated with a semicolon. If you forget the semicolon, MariaDB assumes you want to continue your query on the next line, where you can either do so or terminate the query with a semicolon.

#code

Improve your database knowledge with this MariaDB and MySQL cheat sheet
1.75 GEEK