MySQL Tutorial
MySQL Tutorial
MySQL tutorial provides basic and advanced concepts of MySQL. Our MySQL tutorial
is designed for beginners and professionals.
Our MySQL tutorial includes all topics of MySQL database that provides for how to
manage database and to manipulate data with the help of various SQL queries. These
queries are: insert records, update records, delete records, select records, create
tables, drop tables, etc. There are also given MySQL interview questions to help you
better understand the MySQL database.
What is Database?
It is very important to understand the database before learning MySQL. A database is
an application that stores the organized collection of records. It can be accessed and
manage by the user very easily. It allows us to organize data into tables, rows,
columns, and indexes to find the relevant information very quickly. Each database
contains distinct API
for performing database operations such as creating, managing, accessing, and searching the
data it stores. Today, many databases available like MySQL, Sybase, Oracle
, MongoDB
, PostgreSQL
, SQL Server
, etc. In this section, we are going to focus on MySQL mainly.
What is MySQL?
MySQL is currently the most popular database management system software used
for managing the relational database. It is open-source database software, which is
supported by Oracle Company. It is fast, scalable, and easy to use database
management system in comparison with Microsoft SQL Server and Oracle Database.
It is commonly used in conjunction with PHP
scripts for creating powerful and dynamic server-side or web-based enterprise applications.
It is developed, marketed, and supported by MySQL AB, a Swedish company, and
written in C programming language and C++ programming language
. The official pronunciation of MySQL is not the My Sequel; it is My Ess Que Ell. However,
you can pronounce it in your way. Many small and big companies use MySQL. MySQL
supports many Operating Systems like Windows
, Linux
, MacOS, etc. with C, C++, and Java languages
.
1. MySQL creates a database that allows you to build many tables to store and
manipulate data and defining the relationship between each table.
2. Clients make requests through the GUI screen or command prompt by using
specific SQL expressions on MySQL.
3. Finally, the server application will respond with the requested expressions and
produce the desired result on the client-side.
. But, it is making sure that your GUI should be lighter and user-friendly to make your data
management activities faster and easier. Some of the most widely used MySQL GUIs are
MySQL Workbench, SequelPro, DBVisualizer, and the Navicat DB Admin Tool. Some GUIs
are commercial, while some are free with limited functionality, and some are only compatible
with MacOS. Thus, you can choose the GUI according to your needs.
History of MySQL
The project of MySQL was started in 1979 when MySQL's inventor Michael
Widenius developed an in-house database tool called UNIREG for managing
databases. After that, UNIREG has been rewritten in several different languages and
extended to handle big databases. After some time, Michael Widenius
contacted David Hughes, the author of mSQL, to see if Hughes would be interested
in connecting mSQL to UNIREG's B+ ISAM handler to provide indexing to mSQL.
That's the way MySQL came into existence.
History by Year:
Yea Happenings
r
1995 MySQL AB, founded by Michael Widenius (Monty), David Axmark, and Allan Larsson in
Sweden.
2000 MySQL goes open-source and releases software under the terms of the GPL. Revenues
dropped 80% as a result, and it took a year to make up for it.
2001 Marten Mickos elected CEO at age 38. Marten was the CEO of several nordic companies
before joining MySQL and comes with a sales and marketing background. 2 million active
installations. Raised series with an undisclosed amount from Scandinavian venture capitalists.
It was estimated to be around $1 to $2 million.
2002 MySQL launched its headquarters in addition to Swedish headquarters. At that time, 3 million
active users. MySQL was ended this year with $6.5 million in revenue with 1,000 paying
customers.
2003 This year raised a $19.5 million series b from benchmark capital and index ventures. At this
time, 4 million active installations and over 30,000 downloads per day. It ended the year with
$12 million in revenue.
2004 With the main revenue coming from the OEM dual-licensing model, MySQL decides to move
more into the enterprise market and to focus more on recurring revenue from end-users
rather than one-time licensing fees from their OEM partners. It ended the year with $20
million in revenue.
2005 MySQL launched the MySQL network model after the Redhat network. The MySQL network is
a subscription service targeted at end-users that provide updates, alerts, notifications, and
product-level support designed to make it easier for companies to manage hundreds of
MySQL servers. MySQL 5 ships and includes many new features to go after enterprise users
(e.g., stored procedures, triggers, views, cursors, distributed transactions, federated storage
engines, etc.) Oracle buys innobase, the 4-person, and a Finland's company behind MySQL's
InnoDB storage backend, ended the year with $34 million in revenue based on 3400
customers.
2006 Marten Mickos confirms that Oracle tried to buy MySQL. Oracle' CEO Larry Ellison
commented: "we've spoken to them, in fact, we've spoken to almost everyone. Are we
interested? It's a tiny company. I think the revenues from MySQL are between $30 million and
$40 million. Oracle's revenue next year is $15 billion." Oracle buys sleepycat, the company
that provides MySQL with the Berkeley db transactional storage engine. Marten Mickos
announces that they are making MySQL ready for an IPO in 2008 on a projected $100 million
in revenues. 8 million active installations. MySQL has 320 employees in 25 countries, 70
percent of whom work from home, raised an $18 million series c based on a rumored
valuation north of $300 million. MySQL is estimated to have a 33% market share measured in
install base and 0.2% market share measured in revenue (the database market was a $15
billion market in 2006). It ended the year with $50 million in revenue.
2008 Sun Microsystems acquired MySQL AB for approximately $1 billion. Michael Widenius (Monty)
and David Axmark, two of MySQL AB's co-founders, begin to criticize Sun publicly and leave
Sun shortly after.
2009 Marten Mickos leaves Sun and becomes entrepreneur-in-residence at Benchmark Capital. Sun
has now lost the business and spiritual leaders that turned MySQL into a success.
Sun Microsystems and Oracle announced that they have entered into a definitive agreement
under which Oracle will acquire Sun common stock for $9.50 per share in cash. The
transaction is valued at approximately $7.4 billion.
MySQL Features
MySQL is a relational database management system (RDBMS) based on the SQL (Structured
Query Language) queries. It is one of the most popular languages for accessing and managing
the records in the table. MySQL is open-source and free software under the GNU license.
Oracle Company supports it.
Easy to use
MySQL is easy to use. We have to get only the basic knowledge of SQL. We can build
and interact with MySQL by using only a few simple SQL statements.
It is secure
MySQL consists of a solid data security layer that protects sensitive data from
intruders. Also, passwords are encrypted in MySQL.
Free to download
MySQL is free to use so that we can download it from MySQL official website without
any cost.
It is scalable
MySQL supports multi-threading that makes it easily scalable. It can handle almost
any amount of data, up to as much as 50 million rows or more. The default file size
limit is about 4 GB. However, we can increase this number to a theoretical limit of 8
TB of data.
Speed
MySQL is considered one of the very fast database languages, backed by a large
number of the benchmark test.
High Flexibility
Allows roll-back
Memory efficiency
Its efficiency is high because it has a very low memory leakage problem.
High Performance
MySQL is faster, more reliable, and cheaper because of its unique storage engine
architecture. It provides very high-performance results in comparison to other
databases without losing an essential functionality of the software. It has fast loading
utilities because of the different cache memory.
High Productivity
MySQL uses Triggers, Stored procedures, and views that allow the developer to give
higher productivity.
Platform Independent
t can download, install, and execute on most of the available operating systems.
Partitioning
This feature improves the performance and provides fast management of the large
database.
GUI Support
MySQL provides a unified visual database graphical user interface tool named
"MySQL Workbench" to work with database architects, developers, and Database
Administrators. MySQL Workbench
provides SQL development, data modeling, data migration, and comprehensive administration tools
for server configuration, user administration, backup, and many more. MySQL has a fully GUI
supports from MySQL Server version 5.6 and higher.
MySQL version 8.0 provides support for dual passwords: one is the current password,
and another is a secondary password, which allows us to transition to the new
password.
Disadvantages/Drawback of MySQL
Following are the few disadvantages of MySQL:
o MySQL version less than 5.0 doesn't support ROLE, COMMIT, and stored procedure.
o MySQL does not support a very large database size as efficiently.
o MySQL doesn't handle transactions very efficiently, and it is prone to data corruption.
o MySQL is accused that it doesn't have a good developing and debugging tool
compared to paid databases.
o MySQL doesn't support SQL check constraints.
The release history of previous versions of MySQL through the following table:
MySQL 5.1 14-11-2008 December This version contains the releases of MySQL 5.0 to MySQL
2013 5.1.73 versions. To read about the first version of MySQL,
click here.
MySQL 5.5 03-12-2010 December This version contains the releases of MySQL 5.5 to MySQL
2018 5.5.62 versions.
MySQL 5.6 05-02-2013 February This version contains the releases of MySQL 5.6 to MySQL
2021 5.5.45 versions.
MySQL 5.7 21-10-2015 October This version contains the releases of MySQL 5.7 to MySQL
2023 5.6.27 versions.
MySQL 8.0 19-04-2018 April 2026 This version contains the releases of MySQL 8.0 to MySQL
8.0.21 versions.
We can determine the data type in MySQL with the following characteristics:
MySQL has all essential SQL numeric data types. These data types can include the
exact numeric data types (For example, integer, decimal, numeric, etc.), as well as the
approximate numeric data types (For example, float, real, and double precision). It
also supports BIT datatype to store bit values. In MySQL, numeric data types are
categories into two types, either signed or unsigned except for bit data type.
The following table contains all numeric data types that support in MySQL:
SMALLINT It is a small integer that can be signed or unsigned. If signed, the allowable range is from
-32768 to 32767. If unsigned, the allowable range is from 0 to 65535. We can specify a width
of up to 5 digits. It requires 2 bytes for storage.
MEDIUMINT It is a medium-sized integer that can be signed or unsigned. If signed, the allowable range is
from -8388608 to 8388607. If unsigned, the allowable range is from 0 to 16777215. We can
specify a width of up to 9 digits. It requires 3 bytes for storage.
INT It is a normal-sized integer that can be signed or unsigned. If signed, the allowable range is
from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to 4294967295.
We can specify a width of up to 11 digits. It requires 4 bytes for storage.
BIGINT It is a large integer that can be signed or unsigned. If signed, the allowable range is from
-9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is from 0
to 18446744073709551615. We can specify a width of up to 20 digits. It requires 8 bytes for
storage.
FLOAT(m,d) It is a floating-point number that cannot be unsigned. You can define the display length (m)
and the number of decimals (d). This is not required and will default to 10,2, where 2 is the
number of decimals, and 10 is the total number of digits (including decimals). Decimal
precision can go to 24 places for a float type. It requires 2 bytes for storage.
DOUBLE(m,d) It is a double-precision floating-point number that cannot be unsigned. You can define the
display length (m) and the number of decimals (d). This is not required and will default to
16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a double.
Real is a synonym for double. It requires 8 bytes for storage.
DECIMAL(m,d) An unpacked floating-point number that cannot be unsigned. In unpacked decimals, each
decimal corresponds to one byte. Defining the display length (m) and the number of
decimals (d) is required. Numeric is a synonym for decimal.
BIT(m) It is used for storing bit values into the table column. Here, M determines the number of bit
per value that has a range of 1 to 64.
BOOL It is used only for the true and false condition. It considered numeric value 1 as true and 0 as
false.
BOOLEAN It is Similar to the BOOL.
The following table illustrates all date and time data types that support in MySQL:
YEAR[(2|4)] Year value as 2 digits or 4 digits. The default is 4 digits. It takes 1 byte for
storage.
The following table illustrates all string data types that support in MySQL:
CHAR(size) It can have a maximum size of 255 Here size is the number of characters to
characters. store. Fixed-length strings. Space padded
on the right to equal size characters.
VARCHAR(size) It can have a maximum size of 255 Here size is the number of characters to
characters. store. Variable-length string.
TINYTEXT(size) It can have a maximum size of 255 Here size is the number of characters to
characters. store.
MEDIUMTEXT(size) It can have a maximum size of Here size is the number of characters to
16,777,215 characters. store.
LONGTEXT(size) It can have a maximum size of 4GB Here size is the number of characters to
or 4,294,967,295 characters. store.
BINARY(size) It can have a maximum size of 255 Here size is the number of binary characters
characters. to store. Fixed-length strings. Space
padded on the right to equal size
characters.
(introduced in MySQL 4.1.2)
VARBINARY(size) It can have a maximum size of 255 Here size is the number of characters to
characters. store. Variable-length string.
(introduced in MySQL 4.1.2)
ENUM It takes 1 or 2 bytes that depend It is short for enumeration, which means
on the number of enumeration that each column may have one of the
values. An ENUM can have a specified possible values. It uses numeric
maximum of 65,535 values. indexes (1, 2, 3…) to represent string values.
SET It takes 1, 2, 3, 4, or 8 bytes that It can hold zero or more, or any number of
depends on the number of set string values. They must be chosen from a
members. It can store a maximum predefined list of values specified during
of 64 members. table creation
The following table shows all Binary Large Object data types that support in MySQL:
Data Type Syntax Maximum Size
GEOMETRY It is a point or aggregate of points that can hold spatial values of any type that has a
location.
POLYGON It is a planar surface that represents multisided geometry. It can be defined by zero
or more interior boundary and only one exterior boundary.
LINESTRING It is a curve that has one or more point values. If it contains only two points, it always
represents Line.
GEOMETRYCOLLECTIO It is a kind of geometry that has a collection of zero or more geometry values.
N
string values.
MULTIPOINT It is a collection of multiple point elements. Here, the point cannot be connected or
ordered in any way.
MULTIPLYGON It is a multi surface object that represents a collection of multiple polygon elements.
It is a type of two-dimensional geometry.
The JSON data type has the following advantages over storing JSON-format strings
in a string column:
M
180
History of Java
MySQL Variables
Variables are used for storing data or information during the execution of a program.
It is a way of labeling data with an appropriate name that helps to understand the
program more clearly by the reader. The main purpose of the variable is to store data
in memory and can be used throughout the program.
MySQL can use variables in three different ways, which are given below:
1. User-Defined Variable
2. Local Variable
3. System Variable
Next
ay
User-Defined Variable
Sometimes, we want to pass values from one statement to another statement. The
user-defined variable enables us to store a value in one statement and later can refer
it to another statement. MySQL
Syntax
1. By using the SET statement
Note: We can use either '=' or ':=' assignment operator with the SET
statement.
1. SELECT @var_name := value;
Example1
Here, we are going to assign a value to a variable by using the SET statement.
1. mysql> SET @name='peter';
Then, we can display the above value by using the SELECT statement.
mysql> SELECT @name;
Local Variable
It is a type of variable that is not prefixed by @ symbol. The local variable is a
strongly typed variable. The scope of the local variable is in a stored program block
in which it is declared. MySQL uses the DECLARE keyword to specify the local
variable. The DECLARE statement also combines a DEFAULT clause to provide a
default value to a variable. If you do not provide the DEFAULT clause, it will give the
initial value NULL. It is mainly used in the stored procedure program.
Syntax
1. DECLARE variable_name datatype(size) [DEFAULT default_value];
Example
1. mysql> DECLARE total_price Oct(8,2) DEFAULT 0.0;
We can also define two or more variables with the same data type by using a single
DECLARE statement.
1. mysql> DECLARE a,b,c INT DEFAULT 0;