No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you will most likely have to use SQL at some point in your career.
The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of SQL.
Before we dive deep into SQL, let's quickly define what a database is.
The definition of databases from Wikipedia is:
A database is an organized collection of data, generally stored and accessed electronically from a computer system.
In other words, a database is a collection of data stored and structured in different database tables.
You've most likely worked with spreadsheet systems like Excel or Google Sheets. At the very basic, database tables are quite similar to spreadsheets.
Each table has different columns which could contain different types of data.
For example, if you have a todo list app, you would have a database, and in your database, you would have different tables storing different information like:
- Users - In the users table, you would have some data for your users like:
active, for example.
- Tasks - The tasks table would store all of the tasks that you are planning to do. The columns of the tasks table would be for example,
The Users table will look like this:
+----+----------+---------------+--------+ | id | username | name | active | +----+----------+---------------+--------+ | 1 | bobby | Bobby Iliev | true | | 2 | greisi | Greisi I. | true | | 3 | devdojo | Dev Dojo | false | +----+----------+---------------+--------+
Rundown of the table structure:
- We have 4 columns:
- We also have 3 entries/users
idcolumn is a unique identifier of each user and is auto-incremented.
Next we will learn how to install MySQL and create our first database.
Now that you know what a database, table, and column are, the next thing that you would need to do is install a database service where you would be running your SQL queries on.
We would be using MySQL as it is free, open-source, and very widely used.
As we are going to use Ubuntu, in order to install MySQL run the following commands:
- First update your
sudo apt update -y
- Then install MySQL:
sudo apt install mysql-server mysql-client
We are installing 2 packages, one is the actual MySQL server, and the other is the MySQL client, which would allow us to connect to the MySQL server and run our queries.
In order to check if MySQL is running, run the following command:
sudo systemctl status mysql.service
In order to secure your MySQL server, you could run the following command:
Then follow the prompt and finally choose a secure password and save it in a secure place like a password manager.
With that, you would have MySQL installed on your Ubuntu server. The above should also work just fine on Debian.
I would recommend installing MySQL using Homebrew:
brew install mysql
After that start MySQL:
brew services start mysql
And finally, secure it:
In case that you ever need to stop the MySQL service, you could do so with the following command:
brew services stop mysql
In order to install MySQL on Windows, I would recommend following the steps from the official documentation here:
To access MySQL run the
mysql command followed by your user:
mysql -u root -p
After that, switch to the
demo database that we created in the previous chapter:
To exit the just type the following:
By configuring the
~/.my.cnf file in your user's home directory, MySQL would allow you to login without prompting you for a password.
In order to make that change, what you need to do is first create a
.my.cnf file in your user's home directory:
After that, set secure permissions so that other regular users could not read the file:
chmod 600 ~/.my.cnf
Then using your favorite text editor, open the file:
And add the following configuration:
[client] user=YOUR_MYSQL_USERNAME password=YOUR_MYSQL_PASSWORD
Make sure to update your MySQL credentials accordingly, then save the file and exit.
After that, if you run just
mysql, you will be authenticated directly with the credentials that you've specified in the
~/.my.cnf file without being prompted for a password.
As a quick test, you could check all of your open SQL connections by running the following command:
mysqladmin tool would also use the client details from the
~/.my.cnf file, and it would list your current MySQL process list.
Another cool thing that you could try doing is combining this with the
watch command and kind of monitor your MySQL connections in almost real-time:
watch -n1 mysqladmin proc
To stop the
watch command, just hit
If you prefer using GUI clients, you could take a look a the following ones and install them locally on your laptop:
This will allow you to connect to your database via a graphical interface rather than the
mysql command-line tool.
If you want to have a production ready MySQL database, I would recommend giving DigitalOcean a try:
Before we get started with SQL, let's learn how to create tables and columns.
As an example, we are going to create a
users table with the following columns:
id- this is going to be the primary key of the table and would be the unique identifier of each user.
username- this column would hold the username of our users
name- here, we will store the full name of users
status- here, we will store the status of a user, which would indicate if a user is active or not.
You need to specify the data type of each column.
In our case it would be like this:
The most common data types that you would come across are:
CHAR(size): Fixed-length character string with a maximum length of 255 bytes.
VARCHAR(size): Variable-length character string. Max size is specified in parenthesis.
TEXT(size): A string with a maximum length of 65,535 bytes.
INT(size): A medium integer.
BOOL: Holds a true or false value.
DATE: Holds a date.
Let's have the following users table as an example:
id: We would want to set the ID to
name: The name should fit in a
about: As the about section could be longer, we could set the column data type to
birthday: For the birthday column of the user, we could use
For more information on all data types available, make sure to check out the official documentation here.
As we briefly covered in the previous chapter, before you could create tables, you would need to create a database by running the following:
- First access MySQL:
mysql -u root -p
- Then create a database called
CREATE DATABASE demo_d;
Note: the database name needs to be unique, if you already have a database named
demo_dbyou would receive an error that the database already exists.
You can consider this database as the container where we would create all of the tables in.
Once you've created the database, you need to switch to that database:
You can think of this as accessing a directory in Linux with the
cd command. With
USE, we switch to a specific database.
Alternatively, if you do not want to 'switch' to the specific database, you would need to specify the so-called fully qualified table name. For example, if you had a
users table in the
demo_db, and you wanted to select all of the entries from that table, you could use one of the following two approaches:
- Switch to the
demo_dbfirst and then run a select statement:
USE demo_db; SELECT username FROM demo_db.users;
- Alternatively, rather than using the
USEcommand first, specify the database name followed by the table name separated with a dot:
SELECT username FROM demo_db.users;
We are going to cover the
SELECT statement more in-depth in the following chapters.
In order to create a table, you need to use the
CREATE TABLE statement followed by the columns that you want to have in that table and their data type.
Let's say that we wanted to create a
users table with the following columns:
id: An integer value
username: A varchar value
about: A text type
active: True or false
The query that we would need to run to create that table would be:
CREATE TABLE users ( id INT, username VARCHAR(255), about TEXT, birthday DATE, active BOOL );
Note: You need to select a database first with the
USEcommand as mentioned above. Otherwise you will get the following error:
ERROR 1046 (3D000): No database selected.
To list the available tables, you could run the following command:
+-------------------+ | Tables_in_demo_db | +-------------------+ | users | +-------------------+
You can drop or delete tables by using the
DROP TABLE statement.
Let's test that and drop the table that we've just created:
DROP TABLE users;
The output that you would get would be:
Query OK, 0 rows affected (0.03 sec)
And now, if you were to run the
SHOW TABLES; query again, you would get the following output:
Empty set (0.00 sec)
By default, each column in your table can hold NULL values. In case that you don't wanted to allow NULL values for some of the columns in a specific table, you need to specify this during the table creation or later on change the table to allow that.
For example, let's say that we want the
username column to be a required one, we would need to alter the table create statement and include
NOT NULL right next to the
username column like this:
CREATE TABLE users ( id INT, username VARCHAR(255) NOT NULL, about TEXT, birthday DATE, active BOOL );
That way, when you try to add a new user, MySQL will let you know that the
username column is required.
The primary key column, which in our case is the
id column, is a unique identifier for our users.
We want the
id column to be unique, and also, whenever we add new users, we want the ID of the user to autoincrement for each new user.
This can be achieved with a primary key and
AUTO_INCREMENT. The primary key column needs to be
NOT NULL as well.
If we were to alter the table creation statement, it would look like this:
CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(255) NOT NULL, about TEXT, birthday DATE, active BOOL );
In the above example, we created a new table and then dropped it as it was empty. However, in a real-life scenario, this would really be the case.
So whenever you need to add or remove a new column from a specific table, you would need to use the
ALTER TABLE statement.
Let's say that we wanted to add an
The syntax would be:
ALTER TABLE users ADD email VARCHAR(255);
After that, if you were to describe the table, you would see the new column:
+----------+--------------+------+-----+---------+ | Field | Type | Null | Key | Default | +----------+--------------+------+-----+---------+ | id | int | NO | PRI | NULL | | username | varchar(255) | NO | | NULL | | about | text | YES | | NULL | | birthday | date | YES | | NULL | | active | tinyint(1) | YES | | NULL | | email | varchar(255) | YES | | NULL | +----------+--------------+------+-----+---------+
If you wanted to drop a specific column, the syntax would be:
ALTER TABLE table_name DROP COLUMN column_name;
Note: keep in mind that this is a permanent change, and if you have any critical data in the specific column, it would be deleted instantly.
You can use the
ALTER TABLE statement to also change the data type of a specific column. For example, you could change the
about column from
LONGTEXT type, which could hold longer strings.
Note: Important thing to keep in mind is that if a specific table already holds a particular type of data value like an integer, you can't alter it to varchar, for example. Only if the column does not contain any values, then you could make the change.
In this chapter, we will go over the basic SQL syntax.
SQL statements are basically the 'commands' that you run against a specific database. Through the SQL statements, you are telling MySQL what you want it to do, for example, if you wanted to get the
username of all of your users stored in the
users table, you would run the following SQL statement:
SELECT username FROM users ;
Rundown of the statement:
SELECT: First, we specify the
SELECTkeyword, which indicates that we want to select some data from the database. Other popular keywords are:
username: Then we specify which column we want to select
users: After that, we specify the table that we want to select the data from.
;is required. Every SQL statement needs to end with a semicolumn.
If you run the above statement, you will get no results as the new
users table that we've just created is empty.
As a good practice, all SQL keywords should be with uppercase, however, it would work just fine if you use lower case as well.
Let's go ahead and cover the basic operations next.
To add data to your database, you would use the
Let's use the table that we created in the last chapter and insert 1 user into our
INSERT INTO users(username, email, active) VALUES('bobby', 'email@example.com', true);
Rundown of the insert statement:
INSERT INTO users: first, we specify the
INSERT INTOkeyword, which tells MySQL that we want to insert data into the
users (username, email, active): then, we specify the table name
usersand the columns that we want to insert data into.
VALUES: then, we specify the values that we want to insert in.
Once we've inserted that user, let's go ahead and retrieve the information.
To retrieve information from your database, you could use the
SELECT * FROM users;
+----+----------+-------+----------+--------+---------------+ | id | username | about | birthday | active | email | +----+----------+-------+----------+--------+---------------+ | 1 | bobby | NULL | NULL | 1 | bobby@b...com | +----+----------+-------+----------+--------+---------------+
As we specify
* right after the
SELECT keyword, this means that we want to get all of the columns from the
If we wanted to the only the
username and the
SELECT username,email FROM users;
This will return all of the users, but as of the time being we have only 1:
+----------+----------------------+ | username | email | +----------+----------------------+ | bobby | firstname.lastname@example.org | +----------+----------------------+
In order to modify data in your database, you could use the
The syntax would look like this:
UPDATE users SET username='bobbyiliev' WHERE id=1;
Rundown of the statement:
UPDATE users: first, we specify the
UPDATEkeyword followed by the table that we want to update
username='bobbyiliev'Then we specify the columns that we want to update and the new value that we want to set.
WHERE id=1: Finally, by using the
WHEREclause, we specify which user should be updated. In our case it is the user with ID 1.
NOTE: If we don't specify a
WHEREclause, all of the entries inside the
userstable would be updated, and all users would have the
bobbyiliev. You need to be careful when you use the
UPDATEstatement without a
WHEREclause, as every single row will be updated.
We are going to cover
WHERE more in-depth in the next few chapters.
As the name suggests, the
DELETE statement would remove data from your database.
The syntax is as follows:
DELETE FROM users WHERE id=1;
Similar to the
UPDATE statement, if you don't specify a
WHERE clause, all of the entries from the table will be affected, meaning that all of your users will be deleted.
In case that you are writing a larger SQL script, it might be helpful to add some comments so that later on, when you come back to the script, you would know what each line does.
As with all programming languages, you can add comments in SQL as well.
There are two types of comments:
- Inline comments:
To do so, you just need to add
-- before the text that you want to comment out:
SELECT * FROM users; -- Get all users
- Multiple-line comments
Similar to some other programming languages in order to comment multiple lines, you could wrap the text in
*/ as follows:
/* Get all of the users from your database */ SELECT * FROM users;
You could write that in
.sql file and then run it later on, or execute the few lines directly.
Those were some of the most common basic SQL statements.
If you want to learn more about SQL make sure to check out my free eBook on GitHub:
To support me, you could follow me on Twitter: @bobbyiliev_.
If you already know the basics, I would suggest checking out Materialize. Materialize is a reactive database that delivers incremental view updates. Materialize helps developers easily build with streaming data using standard SQL.