Introduction
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.
Databases
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.
Tables and columns
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:
username
,name
, andactive
, 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,
task_name
,status
,due_date
andpriority
.
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:
id
,username
,name
andactive
- We also have 3 entries/users
- The
id
column is a unique identifier of each user and is auto-incremented.
Next we will learn how to install MySQL and create our first database.
MySQL
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.
Installing MySQL
As we are going to use Ubuntu, in order to install MySQL run the following commands:
- First update your
apt
repository:
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:
sudo mysql_secure_installation
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.
Install MySQL on Mac
I would recommend installing MySQL using Homebrew:
brew install mysql
After that start MySQL:
brew services start mysql
And finally, secure it:
mysql_secure_installation
In case that you ever need to stop the MySQL service, you could do so with the following command:
brew services stop mysql
Install MySQL on Windows
In order to install MySQL on Windows, I would recommend following the steps from the official documentation here:
https://dev.mysql.com/doc/refman/8.0/en/windows-installation.html
Accessing MySQL via CLI
To access MySQL run the mysql
command followed by your user:
mysql -u root -p
Creating a database
After that, switch to the demo
database that we created in the previous chapter:
USE demo_db;
To exit the just type the following:
exit;
Configuring .my.cnf
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:
touch ~/.my.cnf
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:
nano ~/.my.cnf
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.
The mysqladmin command
As a quick test, you could check all of your open SQL connections by running the following command:
mysqladmin proc
The 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 CTRL+C
GUI clients
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:
Worry-free managed database hosting
Tables
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:
-
id
- Integer -
username
- Varchar -
name
- Varchar -
status
- Number
Data types
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. -
INTEGER
(size) orINT
(size): A medium integer. -
BOOLEAN
orBOOL
: 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 toINT
. -
name
: The name should fit in aVARCHAR
column. -
about
: As the about section could be longer, we could set the column data type toTEXT
. -
birthday
: For the birthday column of the user, we could useDATE
.
For more information on all data types available, make sure to check out the official documentation here.
Creating a database
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
demo_db
:
CREATE DATABASE demo_d;
Note: the database name needs to be unique, if you already have a database named
demo_db
you 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:
USE demo_db;
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_db
first and then run a select statement:
USE demo_db;
SELECT username FROM demo_db.users;
- Alternatively, rather than using the
USE
command first, specify the database name followed by the table name separated with a dot:db_name.table_name
:
SELECT username FROM demo_db.users;
We are going to cover the SELECT
statement more in-depth in the following chapters.
Creating tables
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 -
birthday
: Date -
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
USE
command 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:
SHOW TABLES;
Output:
+-------------------+
| Tables_in_demo_db |
+-------------------+
| users |
+-------------------+
Dropping tables
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)
Allowing NULL values
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.
Specifying a primary key
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
);
Updating tables
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 email
column with type varchar to our users
table.
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:
DESCRIBE users;
Output:
+----------+--------------+------+-----+---------+
| 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 TEXT
to 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.
Basic Syntax
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 theSELECT
keyword, which indicates that we want to select some data from the database. Other popular keywords are:INSERT
,UPDATE
andDELETE
. -
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. - The
;
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.
INSERT
To add data to your database, you would use the INSERT
statement.
Let's use the table that we created in the last chapter and insert 1 user into our users
table:
INSERT INTO users(username, email, active) VALUES('bobby', 'bobby@bobbyiliev.com', true);
Rundown of the insert statement:
-
INSERT INTO users
: first, we specify theINSERT INTO
keyword, which tells MySQL that we want to insert data into theusers
table. -
users (username, email, active)
: then, we specify the table nameusers
and the columns that we want to insert data into. -
VALUES
: then, we specify the values that we want to insert in.
SELECT
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
statement:
SELECT * FROM users;
Output:
+----+----------+-------+----------+--------+---------------+
| 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 users
table.
If we wanted to the only the username
and the email
columns instead, we would change the statement to:
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 | bobby@bobbyiliev.com |
+----------+----------------------+
UPDATE
In order to modify data in your database, you could use the UPDATE
statement.
The syntax would look like this:
UPDATE users SET username='bobbyiliev' WHERE id=1;
Rundown of the statement:
-
UPDATE users
: first, we specify theUPDATE
keyword 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 theWHERE
clause, we specify which user should be updated. In our case it is the user with ID 1.
NOTE: If we don't specify a
WHERE
clause, all of the entries inside theusers
table would be updated, and all users would have theusername
set tobobbyiliev
. You need to be careful when you use theUPDATE
statement without aWHERE
clause, as every single row will be updated.
We are going to cover WHERE
more in-depth in the next few chapters.
DELETE
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.
Comments
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.
Conclusion
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:
Free Introduction to SQL eBook
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.
Top comments (11)
Great blog. Enjoyed it throughout..!! 👌👌👌
Really happy to hear that! 🙌
Great post! Looking forward to find time for the eBook, as well!
Thank you 🙌
I imagine this took a long time to put together 😉
Awesome job on the piece 👍
Thank you! Appreciate this! 🙌
Awesome post bro!
Thanks brother 🙌
Great work!!!
Thank you! 🙌
Learn SQL basics quickly with this crash course! Master essential SQL concepts and commands for efficient database querying and manipulation