Sample .Introduction To SQL Light
Sample .Introduction To SQL Light
Databases ........................................................................................ 15
Tables and columns ..................................................................... 16
MySQL ............................................................................................... 18
Installing MySQL .......................................................................... 19
Accessing MySQL via CLI ............................................................. 21
Creating a database .................................................................... 22
Configuring .my.cnf ..................................................................... 23
The mysqladmin command ......................................................... 24
GUI clients ................................................................................... 25
Tables ............................................................................................... 26
Data types ................................................................................... 27
Creating a database .................................................................... 28
Creating tables ............................................................................ 30
Rename tables ............................................................................ 32
Dropping tables ........................................................................... 33
Allowing NULL values .................................................................. 34
Specifying a primary key ............................................................. 35
Index Optimization for Database Queries .................................... 36
Updating tables ........................................................................... 38
Truncate table ............................................................................. 40
SELECT .............................................................................................. 50
SELECT all columns ..................................................................... 52
Pattern matching ......................................................................... 54
Formatting ................................................................................... 56
SELECT specific columns only ..................................................... 57
SELECT with no FROM Clause ...................................................... 58
SELECT with Arithmetic Operations ............................................. 59
LIMIT ............................................................................................ 60
COUNT ......................................................................................... 61
MIN, MAX, AVG, and SUM ............................................................ 62
DISTINCT ..................................................................................... 64
Conclusion ................................................................................... 66
WHERE .............................................................................................. 67
WHERE Clause example .............................................................. 68
Operators .................................................................................... 70
AND keyword ............................................................................... 71
OR keyword ................................................................................. 72
LIKE operator ............................................................................... 73
IN operator ...................................................................................... 74
IS operator ................................................................................... 75
BETWEEN operator ...................................................................... 76
Conclusion ................................................................................... 77
INSERT .............................................................................................. 84
Inserting multiple records ........................................................... 86
Inserting multiple records using another table ............................ 87
UPDATE ............................................................................................ 88
Updating records using another table ......................................... 91
DELETE ............................................................................................. 92
Delete from another table ........................................................... 94
JOIN ................................................................................................... 95
CROSS JOIN .................................................................................. 98
INNER JOIN ................................................................................. 100
LEFT JOIN ................................................................................... 103
RIGHT JOIN ................................................................................. 104
The Impact of Conditions in JOIN vs. WHERE Clauses ................ 106
Equivalence of RIGHT and LEFT JOINs ....................................... 108
Conclusion ................................................................................. 109
SQL | DDL, DQL, DML, DCL and TCL Commands ...................... 110
This open-source introduction to SQL guide will help you learn the
basics of SQL and start using relational databases for your SysOps,
DevOps, and Dev projects. Whether 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.
8
About the author
9
Sponsors
Materialize
DigitalOcean
If you are new to DigitalOcean, you can get a free $100 credit and spin
up your own servers via this referral link here:
10
DevDojo
The DevDojo is a resource to learn all things web development and web
design. Learn on your lunch break or wake up and enjoy a cup of coffee
with us to learn something new.
Join this developer community, and we can all learn together, build
together, and grow together.
Join DevDojo
11
Ebook PDF Generation Tool
12
Book Cover
13
License
MIT License
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
14
Databases
Before we dive deep into SQL, let's quickly define what a database is.
15
Tables and columns
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, and 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, task_name, status, due_date and priority.
+----+----------+---------------+--------+
| id | username | name | active |
+----+----------+---------------+--------+
| 1 | bobby | Bobby Iliev | true |
| 2 | grisi | Greisi I. | true |
| 3 | devdojo | Dev Dojo | false |
+----+----------+---------------+--------+
16
In the next chapter, we will learn how to install MySQL and create our
first database.
17
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.
18
Installing MySQL
We are installing two 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.
To secure your MySQL server, you could run the following command:
sudo mysql_secure_installation
19
Then follow the prompt and 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.
mysql_secure_installation
In case that you ever need to stop the MySQL service, you could do so
with the following command:
https://dev.mysql.com/doc/refman/8.0/en/windows-installation.html
20
Accessing MySQL via CLI
mysql -u root -p
21
Creating a database
After that, switch to the demo database that we created in the previous
chapter:
USE demo;
exit;
22
Configuring .my.cnf
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:
nano ~/.my.cnf
[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.
23
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:
24
GUI clients
If you prefer using GUI clients, you could take a look a the following
ones and install them locally on your laptop:
MySQL Workbench
Sequel Pro
TablePlus
This will allow you to connect to your database via a graphical interface
rather than the mysql command-line tool.
25
Tables
Before we get started with SQL, let's learn how to create tables and
columns.
id - Integer
username - Varchar
name - Varchar
status - Number
26
Data types
The most common data types that you would come across are:
For more information on all data types available, make sure to check
out the official documentation here.
27
Creating a database
mysql -u root -p
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;
28
following two approaches:
USE demo_db;
SELECT username FROM 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:
29
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:
The query that we would need to run to create that table would be:
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;
30
This is a sample from "Introduction to SQL" by Bobby Iliev.