Databases: SQL & MySQL for Beginners (step-by-step guide)
Overview:
Part I | Background & Introduction
Part II | Set-up of MySQL and PopSQL
Part III | Core fundamentals of SQL
Part IV | Advance concepts of SQL
Part V | Conclusion
Part I | Background & Introduction
Background
Today, the topic of discussion is SQL and MySQL. Before we get too techy, the primary reason for creating this article is to provide a step-by-step guide and an overview for current Bootcamp students, recent Bootcamp graduates like myself and others who are curious about databases and are eager to learn SQL. Due to limited time and a heavy workload during Bootcamps, students do not have adequate time to learn SQL or learn about databases in depth. The secondary reason why this post is created is to see how databases fit in the web development ecosystem.
Introduction
The three tech words that stand out from the paragraph above are databases, SQL and MySQL. Let’s explain what each one is. A database is a collection of organized data that can be accessed, managed and updated. With a database, CRUD operations are available to us just like with a RESTful API. Creating, Reading, Updating or Deleting data is therefore possible in databases. Databases are essential to every app since that’s where all the data gets stored from users' information to information about any other entity that exists in the app. SQL stands for Structured Query Language. SQL is used to communicate with databases and is the standard language of relational database management systems (RDBMS). SQL is written so it can be understood by an RDBMS, a software that understands SQL and in return communicates with the server and returns what was asked for via SQL statements called queries. Some common RDBMS are Oracle, Sybase, and MySQL. These various RDBMS are just like different programming languages, where the logic is the same, but the syntax or the implementation of each differs slightly from one to the other. There are two types of databases: Relational and Non-relational. In layman terms, relational is in the form of tables where non-relational is any form that’s not a table like a document-based database, a graph and many more. Today, the focus will be on MySQL, a relational database.
Part II | Set-up of MySQL and PopSQL
The best way to learn in programming is by doing. So, without further ado, let’s install MySQL and start learning SQL.
Installation of MySQL
- Go to: https://dev.mysql.com/downloads/mysql/
- For MacOS, download the DMG Archive version and for Windows, download the ZIP Archive version (MacOS will be used in today’s example).
- Follow the instructions in order to complete the installation. When prompted to create a password for the root user, just create one that you will remember since it will be needed later.
- Next, head to the terminal, type in “mysql -u root -p” without the quotes. Then, you will enter the password you created in Step 3.
- Some of the common errors faced are:
solution: This error means the password created in Step 3 is not being used. Repeat Step 4. If a similar error is seen but instead of (using password: NO), it’s (using password: YES), this means the password was entered incorrectly. Repeat Step 4.
solution: This error means that the MySQL server is not on. Let’s fix that now, go to system preferences, click on MySQL application and turn the server is ON.
If any other error is encountered, just google it.
6. Now, MySQL server is up and we can start by creating a database in the terminal.
PopSQL
The reason for using PopSQL is because it’s a text editor for SQL and it connects to the MySQL server, so the same results are obtained as using MySQL in a terminal when querying. Being able to run multiple queries and seeing how the behavior of one differs from another is what was most helpful using this tool. The user’s visual experience as a learner of SQL enhanced greatly using PopSQL .
Installation of PopSQL
- Go to: https://popsql.com/
- Download.
- Once the download has been completed, sign in with Google or create an account.
- Once signed in, open the app.
- Next, let’s connect the database that was created earlier. In this example, it’s name. When prompted, nickname can be anything you please, hostname is localhost, port is 3306, database is name, username is root and the password is the same password created during the installation of MySQL.
- Press Connect and now PopSQL is connected to the database, in this example, name.
7. Protip: In the app, for better visualization of data, select the middle icon from the three icons on the top right corner of the app so the results will be shown below the queries once the RUN button is clicked.
Hopefully, it makes a bit more sense of what SQL and MySQL are at this point of our journey at a higher level. This concludes the setup portion of the guide. Let’s dive into SQL now.
Part III | Core Fundamentals of SQL
Student
For this section of the guide, a single table, student table (left) will be used to show the behavior of different basic SQL commands. Okay, so far, we have a database, name, but there are no tables in it currently. Let’s change that now.
Creating a Table
Primary Keys
A primary key is a column on the table that uniquely identifies each row in the table. For example,
After creating the table, running the query below confirms that the table has been created correctly
The result is:
The field column represents different columns of the student table (see Student table above). The type column shows the expected data types for each column in the student table. Some common data types for columns are:
INT, VARCHAR(), DECIMAL(M,N), BLOB, DATE, and TIMESTAMP
INT simply means the values of the column will be integers. VARCHAR(20) means that the values for this column will be a string with a maximum string length of 20. Choosing this number wisely can save some space in the database. Let me explain. Imagine, we have a name column and instead of VARCHAR(20), what if we had VARCHAR(200). This means that for each row of the name column, the maximum length of 200 is being saved, even though not too many names have character length anywhere close to 200. Decide the length of VARCHAR depending on the values that will go under the given column. The maximum length for strings is 255. DECIMAL(M, N) is used for decimals, M is the total length of the decimal and N is the length of numbers from the decimal point to the end of the decimal. For example, 3.14, this is a decimal data type and M is 3 and N is 2 so it would be written as DECIMAL(3,2). BLOB is a data type that’s uncommon in use, but it’s good to know that this data type is for images, other multimedia files and for storage of other binary data. DATE values are in ‘YYYY-MM-DD’ format, meaning no time part. The supported range is ‘1000–01–01’ to ‘999–12–31’. TIMESTAMP includes both time and date. The values are in ‘YYYY-MM-DD HH:MM:SS’ UTC format. The supported range is ‘1970–01–01 00:00:01’ UTC to ‘2038–01–19 03:14:07’ UTC.
Deleting a Table
Adding Columns to a Table
To confirm run:
As a result,
Dropping Columns from a Table
Inserting Data into a Table
To check if data is inserted correctly, run:
For example, run:
Now, run the SELECT query to confirm the insertion was correct,
Inserting available data into a Table
Let’s say we want to add a third student, but he’s undecided and doesn’t have a major yet. We can insert data that we do have by running the following,
Again, run the SELECT query to get back all the data and the output should be:
Common SQL constraints can be added when defining the schema of a table in CREATE TABLE query to make inserting of data easier.
For example,
If we drop the previous student table and create this version, the result will be the following after running the DESCRIBE query:
Run the following queries,
after running the SELECT query, the result will be:
Updating Data in a Table
Let’s say we want to update the table for students who had major as ‘Biology’ to ‘Bio’ for all records. Running the following query,
After running the SELECT query, the result will be:
Deleting Data in a Table
Let’s say Rob from our student table has left the school and his record needs to be deleted in order to update records. This can be achieved by running,
In result, after running the SELECT query, we get:
Getting Information from Database
In this section, we will look at the SELECT keyword of SQL. Imagine we have tons of data, so far we have been looking at getting everything (*) from the table. What if we only wanted to get back certain rows or columns from the table?
This approach can be implemented by running:
The result is,
Getting Back Ordered Data
If for some reason, we want to get back data in a certain order, this can be achieved by doing the following,
First, let’s add another student,
Second, let’s confirm by running,
In result,
Imagine, we want to get back every column from the student table where the records returned are in an ascending order for column, name.
If multiple records have the same value for the column we are ordering by, a second column can be added to order records even further by adding a comma and the second column in the ORDER BY section of the query.
Quick snapshot of how this may look:
Get Limited Data Back
Student table has 3 total students, but we only want two, the query above does exactly that:
Filtering of Data with SELECT Queries
Imagine, we want to get back records of students who are Math majors only in today’s example. This can be done by running,
Other Comparison Operators
In WHERE section of SELECT queries, other common comparison operators can be used: <, >, < =, > =, =, <>, AND and OR. Most of these operators we already know, but <> is an unusual one and it denotes in-equation or not equals to.
Filtering Based on Group of Values
Let’s say we have values, ‘Math’ and ‘Biology’, and we want to get back any records from the student table with majors that match any one of those values. How can we do that?
By running,
This concludes the core fundamentals of SQL section of this guide. Yes, it’s lengthy, but the goal is to really grasp the basic commands of SQL and I hope that’s the case at this point. Take a quick break if needed and we will jump into multiple tables and complex table schemas next!
Part IV | Advance Concepts of SQL
In this section, we will be looking at a complex database schema in order to see behaviors of advanced SQL queries. We will be creating multiple tables for a Company database. Let’s do that now!
Creating Database Tables
This Company database will have 5 tables.
First, is the employee table. Before we do that, let’s drop the student table created in the section before. Then, run
Running,
Will return,
Next, we have a branch table.
Now that the branch table is created, we can go ahead and specify the FOREIGN KEY details in the employee table,
Now, let’s create the client table.
Then, it’s the works_with table. This table shows relationships between employees and clients. Let’s explore,
After running the DESCRIBE query, we get
Lastly, we have our final, the 5th, branch_supplier table.
After running the DESCRIBE query, we get
This concludes the creating tables part of this section. Let’s start inserting!
Inserting of Data into Tables
Due to having various foreign keys in tables, the order of insertion has to be specific,
The above example takes care of all employees who work at the Corporate branch. By following this pattern, we are able to add data into both employee and branch tables.
By repeating the above process, employees who work at Scranton and Stamford branches can also be added,
Inserting continues to branch_supplier table,
In result, after running the SELECT query,
Next is the insertion of data into the client table,
Lastly, insertion of works_with table,
Whew! That was a lot of inserting! We now have all 5 tables created and populated with company’s data. Let’s start with a review of:
Querying of Data using SELECT keyword
Imagine we want to get back all employees from the employee table, this is an easy one,
If you want to find all clients, just replace the table name from employee to client in SELECT query to retrieve all clients.
Next, let’s say we want to get all employees ordered by salary,
Now, we would like to get all employees, but this time ordered by sex then name. How can we do this?
The next prompt is to find the first 5 employees from the employee table,
Imagine, instead of getting all columns returned from the employee, we only wanted first and last name columns,
Instead of getting the column names back as first_name and last_name, we want to get back as forename and surname respectively. Aliasing column names is possible,
Next prompt is to find all different genders in the employee table, we can look at a new SQL keyword DISTINCT,
This wraps up this section, let’s move onto:
SQL Functions
A SQL function is a block of SQL statements, a query, that returns a value. Let’s see it in action!
This time around, we want to find the number of employees, a value,
Next, we want to find out how many employees have supervisors, this can be done using the above query, except replace emp_id column name with super_id to count how many entries have values for this column.
Kicking up a notch, next prompt is to find female employees born after 1970.
In result,
Let’s check out the AVG keyword in SQL functions,
The above query can be updated to
The next prompt is to instead find the sum of all employees' salaries (SUM keyword).
Aggregation
Aggregation enables the display of returned data from SQL functions in a more helpful way.
For example, the prompt is to find out how many males and females there are in the company.
Just to bring the logic home, another example,
The employee table has been used a ton, let’s look at the works_with table now,
The next prompt is to find the total sales of each salesman.
By looking at the works_with table, we can see that each employee appears multiple times in the table because that employee has multiple clients and for each client, there’s a value for total_sales. Our goal is to sum various total_sales for each employee. Looking at the previous example, how can this be done?
We can replace emp_id with client_id in the above-aggregated query to get the total amount each client spent instead. These aggregate queries are great, play around with the Company’s database schema to get better at using them.
Wildcards & LIKE keyword in SQL
Wildcards are used to grab data that match a certain pattern. Let’s explain!
The prompt is to find all clients that are LLC. Looking at the client table, we can’t use anything we know so far, this is when wildcards come in:
If you are familiar with regular expressions, this syntax is a simplified version of that.
Let’s look at another prompt, find any branch suppliers who are in the label business.
The next prompt is to find any employee born in October.
Lastly, let’s find clients that have school in their names.
Part V | Conclusion
You can generate powerful queries from SQL statements as seen in this blog post. Remember, the best way to cement the concepts and get better at SQL is by practicing and solving SQL problems, just like most things in software development.
The more you practice the better you’ll be, the harder you train the great in you they’ll see. — Alcurtis Turner
Happy Coding!
Resources
https://media.giphy.com/media/110dhxfJebYOTm/giphy.gif
https://giphy.com/gifs/statement-HaIWYsLmuPm5G
https://media.giphy.com/media/o2su0spy4u2zK/giphy.gif
https://media.giphy.com/media/28cgfmjINcromuwDjs/giphy.gif
https://searchsqlserver.techtarget.com/definition/database
http://www.sqlcourse.com/intro.html
https://www.youtube.com/watch?v=HXV3zeQKqGY&t=7893s