MYSQL Guide for Beginner
MYSQL Guide for Beginner
Preface
Overview of MySQL
Chapter 1: Introduction to MySQL
1.1 Introduction
7.1.1 Syntax 1
7.1.2 Syntax 2
7.1.3 Syntax 3
7.1.4 Syntax 4
9.2.2 OR operator
9.2.5 IN operator
Chapter 10: Some important MySQL commands
10.7 Subqueries
What is Database?
Data is a raw fact and figure that is used everywhere in our real world. A
group of data makes a specific information or database. In our day-to-day
life if we visit a mall, we see that a shopkeeper is handling a product
database using a scanner. Another example, let us consider a bank, they are
handling customer database. Third example is in schools they are handling
student’s database. Henceforth the database can be handled either manually
in a register or through computer software such as MySQL.
What is relational database?
It is a collection of organized data stored in one or more tables. We can
easily see and understand how different data structures relate to each other.
Difference between MySQL and SQL
We can create, read, update, and delete the database using MySQL. But
SQL (structured query language) is a Compact-English-Statement used to
access, manipulate, and coordinate the database. In this eBook we are going
to discuss about interactive environment that is how to work on entered
database.
Types of Command used in MySQL
Following are the types of commands used in MySQL
• DDL (Data definition language)- These commands are used to
create, drop, alter and describe the Table.
• DML (Data manipulation language) -These commands are used to
manipulate data such sum, average and many more
• DCL (Data control language) – It provides security to the database
such as Grant and Revoke.
• DQL (Data query language) – It allows user to pass query to the
relational database.
• TCL (Transaction Control Language) – It allows user to save and
restore databases.
Note: We are about to discuss these commands in upcoming chapters.
1.2 Installation of MySQL
In this section you will learn the installation of MySQL work bench.
Let’s get started by downloading MySQL from dev.mysql.com. Select
the operating system and download MySQL installer 8.0.19. Save it in
appropriate location for e.g. Desktop.
MySQL data types specify the type of value that can be store in
database table. Data types are mainly classified into three categories.
• Numeric
• String
• Date and Time
2.1.1 Numeric data type
It refers to the data that is in the form of numbers such as Integer, Float and
Boolean. The following table lists the MySQL Numeric data type.
Numeric data Type Range
BIT 0-1
TINYINT 0-255
SMALLINT -32,768 - 32,767
BIGINT -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
FLOAT -1.79E+308 - 1.79E+308
REAL -3.40E+38 - 3.40E+38
2.1.2 String data type
It allows us to store fixed or variable character values such as char, varchar, and text. The following
table lists the MySQL String data type.
String data Description
Type
CHAR Fixed length of 8000 characters
VARCHAR Variable- length of 8000 characters
TEXT Variable- length of 2GB data
2.1.3 Date and Time data type
It is used for storing values that can contain both date as well as time. The
following table lists the MySQL Date and Time data type.
Date and Time data Description
Type
DATE It stores date in the format (YYYY-MM-DD)
TIME It stores time in the format (HH:MI:SS)
DATETIME It stores date and time information (YYYY-MM-DD HH:MI:SS)
2.2 Operators in MySQL
The Logical operator performs Boolean operation which gives two results
either true or false. The following table lists the MySQL Logical operators.
Logical operators Description
AND True if both expressions are true
IN TRUE if the operand is equal to any one from
the list of expression
OR One expression must be true to get TRUE result
NOT Reverses the value
Like if the operand matches a pattern than true
BETWEEN if operand is within a range than true
2.2.3 Comparison operators
It converts two integer values to binary bits, perform the AND, OR, and
NOT operation on each bit. The following table lists the MySQL Bitwise
operators.
Bitwise operators Description
AND True if both values are true
OR True if any one value is true
NOT Reverses the value
2.2.5 Set operators
Set operators used to combine rows from two or more table. The following
table lists the MySQL Set operators.
Set operators Description
UNION Combines two or more result set without duplicating values
UNION ALL Combines two or more result set including duplicating values
INTERSECT It includes only the common values present in two or more result set
EXCEPT It includes only results from first result set that are not included in
second result set
CHAPTER 3: DATA DEFINITION
LANGUAGE (DDL)
In previous chapter we have seen the syntax of MySQL which includes
datatypes and operators used in MySQL, now we will learn about integrity
constraints used in MySQL than we will learn about DDL and create a table
using DDL commands.
3.1 Integrity constraints in MySQL
Integrity constraints are set of rules that a table’s data or column must
follow. We can use integrity Constraints with commands at the column or
table level. The table-level Integrity constraints will be applied to the entire
table, but the column level constraints are only applied to one column.
Table shows the list of Integrity constraints used in MySQL.
Integrity constraints Description
NOT NULL It prevents a column from having a NULL value
DEFAULT When no value is defined for a column, the DEFAULT Constraint
provides a default value.
UNIQUE It ensures that any value in a column is unique.
PRIMARY KEY Uniquely identifies each row/record.
FOREIGN KEY It recognizes a row/record in any database table uniquely.
3.2 Introduction to MySQL DDL
MySQL DDL statements are used to define and manipulate data base
structure. It allows us to create a table, modify and maintain databases
using commands.
Following are the commands of DDL.
• CREATE- It is used to create a table and schema.
• ALTER - It is used to add, modify or delete columns or
constraints from the database table.
• TRUNCATE - It is used to delete the data present in the table
but this will not delete the table.
• DROP – It is used to delete the entire Table.
• RENAME – It is used to rename the Table.
3.2.1 CREATE command
If we need to alter the table, let’s add the new column phone_number,
type
ALTER TABLE employees
ADD phone_number VARCHAR (15);
Execute and get the result in MYSQL workbench.
Let’s add data type to email column, here we have to use MODIFY
command, type
ALTER TABLE employees
MODIFY COLUMN email VARCHAR (100)
Execute and get the result in MYSQL workbench.
MySQL DML deals with the modification of data within the tables of a
database. DML statements used for adding(inserting), deleting, and
modifying (updating) data in a database.
Following are the commands of DML
• INSERT – It is used to add rows to a table manually.
• SELECT – We can access and manipulate data using Select
command.
• DELETE – We can remove rows of existing table using Delete
command.
• UPDATE - To change the actual data in a table.
4.2 DML command execution
In previous chapter we have created an employees table, now let’s
manipulate the table using DML commands.
4.2.1 INSERT command
In this topic, I am going to discuss about how we can insert rows into a
table. We have a table named employees. I will select everything from my
table employees.
SELECT * FROM employees;
employee_id first_name last_name hourly_pay hire_date
Output
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-
02
2 Squidward Tentacles 15.00 2023-01-
03
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
5 Sandy Cheeks 17.25 2023-01-
06
4.2.2 SELECT command
Output
first_name last_name
Eugene Krabs
Squidward Tentacles
Spongebob Squarepants
Patrick Star
Sandy Cheeks
Output
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-02
2 Squidward Tentacles 15.00 2023-01-03
5 Sandy Cheeks 17.25 2023-01-06
Output
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-
02
2 Squidward Tentacles 15.00 2023-01-
03
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
5 Sandy Cheeks 17.25 2023-01-
06
4.2.3 UPDATE command
In this section, we are going discuss how to update data from a table. In our
table employee Id 6 missing some data.
Output
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 10.25 2023-01-
02
2 Squidward Tentacles 10.25 2023-01-
03
3 Spongebob Squarepants 10.25 2023-01-
04
4 Patrick Star 10.25 2023-01-
05
5 Sandy Cheeks 10.25 2023-01-
06
CHAPTER 5: DATA CONTROL
LANGUAGE (DCL)
5.1 Introduction to MySQL DCL
Let us control the root database by giving access permission. DCL gives us
facility to implement security on created database.
5.2.1 GRANT command
Note: New user (User_1) having permission to access, insert and delete
data from root database.
5.2.2 REVOKE command
Note: WHERE clause use to filter the rows depending upon condition.
7.1.4 Syntax 4
The unique constraint shows the values of a column are all different.
We add this constraint when we create a table, so let’s a create a new
table
1 CREATE TABLE products (
2 product_id INT,
3 product_name VARCHAR (25) UNIQUE,
4 price DECIMAL (4,2)
5 );
Since we have used UNIQUE constraint so we cannot insert any
product names that are the same they all have to be unique.
In case if we forgot to add UNIQUE constraint during table creation
than we can use the following sequence of commands to add UNIQUE
constraint
1 ALTER TABLE products
2 ADD CONSTRAINT
3 UNIQUE (product_name);
Execute and get the result.
UNIQUE constraint is successfully added to column product_name.
If we insert the same product twice in product_name column than an
error message will be displayed. So, all the values in product_name
need to be different.
8.2 NOT NULL constraint
If we use NOT NULL constraint than the value within a specific column
should not be null. If we forgot to add value than error will occur. We
cannot enter a null value. We can set to be zero that’s acceptable. It’s a
useful constraint to verify input if there’s any column that you don’t want to
have any null values.
8.3 CHECK constraint
Foreign key is a PRIMARY KEY from one table that can be found
within a different table using a foreign key we can establish a link
between two tables. There are two primary benefits to this in our
transaction table if I were to take a look at the customer ID of who
initiated this transaction, I could refer to the customers table then find
the first and last name of that customer.
Let’s create transaction and customer table, establish a link between
them using foreign key
I am going to create a new table of customers, type
1 CREATE TABLE customers (
2 customer_id INT PRIMARY KEY AUTO_INCREMENT.
3 first_name VARCHAR (50),
4 last_name VARCHAR (50)
5 );
Execute and get the result
Now insert some values in to the table, type
1 INSERT INTO customers (first_name, last_name)
2 VALUES (“Fred”, “Fish”),
3 (“Larry”, “Lobster”),
4 (“Bubble”, “Bass”);
5
6 SELECT * FROM customers;
Output
customer_id first_name last_name
1 Fred Fish
2 Larry Lobster
3 Bubble Bass
Now, we are going to create a link between our customers table and our
transactions table via customer ID. Let’s create a transaction table with
FOREIGN KEY constraint, type
1 CREATE TABLE transactions (
2 transaction_id INT PRIMARY KEY AUTO_INCREMENT,
3 amount DECIMAL (5,2),
4 customer_id INT,
5 FOREIGN KEY (customer_id) REFERENCES
customers(customer_id)
6 );
Execute and get the result
transaction_id amount customer_id
A JOIN is a clause that is used to combine rows from two or more tables
based on a related column between them such as a foreign key. Let’s join
two previously created table. We have two tables a table of transaction and
a table of customers.
1. Table of Transaction
transaction_id amount customer_id
1000 4.89 2
1001 2.68 3
1002 3.32 1
1003 4.99 3
2. Table of Customers
customer_id first_name last_name
1 Fred Fish
2 Larry Lobster
3 Bubble Bass
A stored program that accepts parameter from the user and returns the
value known as Function . MySQL provides lot of functions. Some of
them shown below.
Let’s count number of rows in amount column of transaction table,
type
1 SELECT COUNT (amount)
2 FROM transaction;
Execute and get the result
The Logical operator is a keyword use to combine more than one condition.
Let’s use our employees table to demonstrate logical operators.
SELECT * FROM employees;
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-
02
2 Squidward Tentacles 15.00 2023-01-
03
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
5 Sandy Cheeks 17.25 2023-01-
06
6 Sheldon Plankton 10.25 2023-01-
07
9.2.1 AND operator
1 SELECT *
2 FROM employees
3 WHERE hire_date< “2023-01-6” AND hourly_pay<15;
It will return any results that match these two criteria.
employee_id first_name last_name hourly_pay hire_date
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
9.2.2 OR operator
1 SELECT *
2 FROM employees
3 WHERE hire_date< “2023-01-6” OR hourly_pay<15;
It will return any results that match one of these two criteria.
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-
02
2 Squidward Tentacles 15.00 2023-01-
03
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
9.2.3 NOT operator
1 SELECT *
2 FROM employees
3 WHERE NOT hourly_pay = 12.50;
Here, NOT basically reverses anything you say.
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-02
2 Squidward Tentacles 15.00 2023-01-03
9.2.4 Between operator
1 SELECT *
2 FROM employees
3 WHERE hire_date BETWEEN “2023-01-04” AND “2023-01-07”;
Execute and get the result according to criteria.
employee_id first_name last_name hourly_pay hire_date
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
5 Sandy Cheeks 17.25 2023-01-
06
6 Sheldon Plankton 10.25 2023-01-
07
9.2.5 IN operator
1 SELECT *
2 FROM employees
3 WHERE hourly_pay IN (17.25,10.25);
We can find any values that are within a set.
employee_id first_name last_name hourly_pay hire_date
5 Sandy Cheeks 17.25 2023-01-06
6 Sheldon Plankton 10.25 2023-01-07
CHAPTER 10: SOME IMPORTANT
MYSQL COMMANDS
In this section, we are going to discuss about some important clauses used
in MySQL to handle databases or schema object.
10.1 Wild card character
Wild card is a special character used to handle the database. There are two
wild card character – precent and underscore.
Let’s use % in our employees table to retrieve first name begins with S, type
1 SELECT * FROM employees
2 WHERE first_name LIKE “s%”;
Let’s find the employee hired in January from employees table, type
1 SELECT * FROM employees
2 WHERE hire_date LIKE “____-01-__”;
Note: Year uses four underscore and month uses two underscore.
Output
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-
02
2 Squidward Tentacles 15.00 2023-01-
03
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
5 Sandy Cheeks 17.25 2023-01-
06
6 Sheldon Plankton 10.25 2023-01-
07
10.2 Order by clause
The LIMIT clause use to limit the number of records. It is very useful if you
are working with a large database.
Let’s limit the number of employees that are displayed from employees
table, type
1 SELECT * FROM employees
2 LIMIT 2;
Note: Only first two employee’s details will be displayed.
Output
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-02
2 Squidward Tentacles 15.00 2023-01-03
10.4 Union operator
The UNION operator combines the results of two or more select statements.
Let’s combine the first_name and last_name of our two tables- Employees
table and Customers table.
Employees table
employee_id first_name last_name hourly_pay hire_date
1 Eugene Krabs 25.50 2023-01-
02
2 Squidward Tentacles 15.00 2023-01-
03
3 Spongebob Squarepants 12.50 2023-01-
04
4 Patrick Star 12.50 2023-01-
05
5 Sandy Cheeks 17.25 2023-01-
06
6 Sheldon Plankton 10.25 2023-01-
07
Customers table
customer_id first_name last_name
1 Fred Fish
2 Larry Lobster
3 Bubble Bass
We can create a virtual table using VIEWS in MySQL. The fields in a view
are fields from one or more real tables in the database.
Let’s view our employees table as employees_attendance table, type
1 CREATE employees_attendance AS
2 SELECT first_name, last_name
3 FROM employees;
So everything was successful, we have a new view, type
1 SELECT * FROM employees_attendance;
Output
first_name last_name
Eugene Krabs
Squidward Tentacles
Spongebob Squarepants
Patrick Star
Sandy Cheeks
Sheldon Plankton
10.6 Indexes in MySQL
Indexes are used to find the values within a specific column more quickly.
MySQL normally searches sequentially through a column.
Customer’s table
customer_id first_name last_name
1 Fred Fish
2 Larry Lobster
3 Bubble Bass
Output
customer_id first_name last_name
3 Bubble Bass
10.7 Subqueries
THE END