DOC-20250604-WA0029.
DOC-20250604-WA0029.
MySQL
MySQL is a relational database management system based on the Structured Query Language, which is the popular
language for accessing and managing the records in the database. MySQL is open-source and free software under
the GNU license. It is supported by Oracle Company.
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 mainlyPrime Ministers of India | List of Prime Minister of India
(1947-2020)
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.
MySQL is a Relational Database Management System (RDBMS) software that provides many things, which are as
follows:
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.
A client can use any MySQL GUI. 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.
o MySQL is an open-source database, so you don't have to pay a single penny to use it.
o MySQL is a very powerful program that can handle a large set of functionality of the most expensive
and powerful database packages.
o MySQL is customizable because it is an open-source database, and the open-source GPL license
facilitates programmers to modify the SQL software according to their own specific environment.
o MySQL is quicker than other databases, so it can work well even with the large data set.
o MySQL supports many operating systems with many languages like PHP, PERL, C, C++, JAVA, etc.
o MySQL uses a standard form of the well-known SQL data language.
o MySQL is very friendly with PHP, the most popular language for web development.
o MySQL supports large databases, up to 50 million rows or more in a table. The default file size limit
for a table is 4GB, but you can increase this (if your operating system can handle it) to a theoretical
limit of 8 million terabytes (TB).
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.
MySQL is named after the daughter of co-founder Michael Widenius whose name is "My".
Features of MySQL
There are various features of MySQL. Some of them are:
● It is based on SQL.
● It is open source and freely available.
● User-friendly and compatible with PHP, which means you can easily connect with PHP and create a
website and share which can be connected easily and manipulate databases. For example, using
XAMPP you can install PHP and MySQL.Create a new database in MySQL and connect both of them
using PHP my admin.
● It is based on client-server architecture.
● It’s very secure and flexible as it’s one of the most secure database systems.
● It is scalable and supports large databases, for example, you can create 50 million rows or more in a
table, the default file size limit for a table is 4GB and it can be increased according to the
requirements.
● It is also compatible with major operating systems like Windows, Mac, and Linux.
Editions of MySQL
There are five types of MySQL editions, these are discussed below:
● MySQL Enterprise Edition- There are several MySQL editions available to business customers to
meet specific and advanced technical requirements.
● MySQL Standard Edition- MySQL Standard Edition allows you to deliver good performance and
easy Online Transaction Processing (OLTP) applications.
● MySQL Classic Edition- It is the embedded database for ISVs, OEMs, and VARs developing
applications. It is determined that it has the best performance and zero administration database.
● MySQL Cluster Carrier Grade Edition- It is a distributed database combining easier scalability and
also for its availability.
● MySQL Embedded(OEM/ISV)- Over 2000 ISVs, OEMs, and VARs calculate MySQL as their
product’s database to make their operations, tackle, and appliances more competitive, bring them to
request, and lower their COGs( cost of Goods sold).
Step 1: At first you need to go to the official website of MySQL(mysql.com) and scroll down.
You will see an option to select Operating System.
You need to choose Microsoft Windows.
Step 2: Then you will see two options to download. You can select one of them or you can take a
reference from the below image.
Step 3: Once you click on the download option, you will be redirected to the next page and you
will have to click on the “No thanks, just start my download” option.
After that, you will see that MySQL Installer is getting downloaded.
Step 4: Once, MySQL Installer gets downloaded, you need to open that and you will see that the MySQL
installer Community is getting installed.
Then you will see the following screen. Next, all you need to do is to click on the radio button which is
asking for the acceptance of the license terms. After
Step 5: In the next process, you have to choose the setup type.
This means that you have to choose which features you wish to install. So, choose the Full button and
click on Next.
This process installs all of the products that are available in this catalog including MySQL Server, MySQL
Shell, MySQL Router, MySQL Workbench, MySQL Connectors, documentation, samples and examples,
and much more.
Step 6: In the next step, you will see that some of the features may fail to install due to a lack of
requirements.
So, either you can fix them, or can skip them, by clicking on Next as it willn’t interrupt the installation
process.
Next, you will see a dialog box asking for your confirmation of a few products not getting installed.
Next, you will see the list of the products which will get installed.
So you can go ahead and click on the Execute option, which we highlighted in the below image.
After clicking on the Execute option, you will see that the products are getting installed.
Next, you have to configure the Server and Router. After that click on Next.
Step 7: Based on your requirement, you can either choose the Standalone MySQL Server/ Classic
MySQL Replication or the InnoDB Cluster.
We clicked on the Standalone MySQL Server/ Classic MySQL Replication option and then click on Next.
Step 8: Once, you click on Next, you will get the Type and Networking page where you have to mention
the Server Configuration Type.
We left it as it is and then click on Next, you can take reference from the below image.
Step 9: Now, you will get to the Authentication Method page where you have to choose the authentication
mode.
Here, you can choose the first option as given in the below image, and click on Next.
Step 10: Next, you have to make the MySQL Root Password and then repeat the same password and
then click on Next.
● Remember the password that you have mentioned or you can take note of that password for the
future.
Step 11: After that, you will get the Windows Service page where you have to choose whether you want
to start the MySQL Server at system startup or not. Here, we are keeping it as it is and has not changed
anything.
Click on Next.
After clicking on Next, you will get the Apply Configuration page where you will get a list of the
configurations that you want to execute to apply the changes from the list of these configurations.
Step 12: On the next page you can choose to configure the Router.
So click on click on Finish.
Step 13: Once, you click on Finish, you will see the Connect to server page.
Here you have to give the root password, which you set earlier.
Now, you have to check if the connection is successful, by clicking on the Check button.
Step 15: In the next step, you will see the screen which is given below.
After the configurations get applied, click on Finish.
Here, you can check on the page that the installation procedure has been completed.
Step 16: After completing all of the above steps and procedures, you can finally check if MySQL has
been installed or not.
Now you just have to open MySQL Shell and fill in your root password.
The above output shows that MySQL has been installed in your system.
MySQL Structure
MySQL allows separating information into tables or “areas of pertinent information.”
In non-relational database systems, all the information is stored in one big area, which makes it much
more difficult and bulky to sort and extract required data.
In MySQL, each table consists of separate fields, which represent each bit of information.
For example, the field customer’s first name, and another field could contain his last name.
Fields can hold different types of data, such as text, numbers, dates, and so on.
The separate “areas” or tables of MySQL are then linked together with some common denominator,
where the values of the common field are the same.
Example:- customer table that includes a customer’s name, address, and ID number, and another table
that includes the customer’s ID number and past orders he has placed. The common field is the
customer’s ID number, and the information stored in the two separate tables would be linked together
via fields where this number is equal. This enables us to see all the information related to this
customer at one time.
Field Types
When you create a table initially, need to tell the MySQL server what types of information will be stored
in each field.
The different types of fields and some examples are listed in the table that follows.
In brief
❑ 1–255 characters, variable length: Use varchar if you want to delete any trailing spaces, or if want to
set a default value. Use tiny text if don’t care about trailing spaces or a default value or if your text does
not need to be case-sensitive. Use a tiny blob if don’t care about trailing spaces or a default value, but
your text does need to be case-sensitive.
❑ 256–65536 characters: Use text if your text does not need to be case-sensitive in searches, sorts, or
comparisons, use blob if the text is case-sensitive.
❑ 65537–1677215 characters: Use medium text if the text does not need to be case sensitive, use
mediumblob if the text is case-sensitive.
❑ 1677216–4294967295 characters: Use long text if the text does not need to be case sensitive, use long
blob if the text is case-sensitive.
❑ Yes. It may contain letters or numbers, but it must be one of a finite number of values. Use menu.
❑ No. It will consist of dates and/or times. Use timestamp for store the time and date the information
was entered or updated. To store date and time both, use DateTime. Only the year, use year.
❑ No. consist only of numbers, and mathematical functions will be performed on this field.
❑ No. It will consist of only numbers, but mathematical functions will not be performed on this field.
Use the preceding guidelines for text/number mix in the field.
Null tells MySQL that it is okay if nothing is stored in the field, and not null tells MySQL to require
something, anything, to be stored there.
If a field has been defined as not null and nothing is entered by the user, an error pops up.
Indexes
MySQL uses indexes to expedite the process of searching for a particular row of information.
MySQL can jump to the approximate location of your data much more quickly. It does through the use
of indexes, also known as keys.
MySQL requires at least one index on every table, use a primary key, or unique identifier that helps
keep the data separate. This field must be “not null” and “unique”; an example would be a customer ID
number to keep your customers separate.
MySQL also provides a feature that allows a value in a field to be automatically incremented by one.
This auto_increment parameter is useful for making sure your primary key is being populated with
unique numbers.
Unique
MySQL makes sure that absolutely no duplicates exist for a particular field. Typically, this is used for only the
primary key in your table, but it can be used with any field.
Auto Increment
A field that automatically increases by one whenever a new record is added. a useful function for
assigning ID numbers.
MySQL Table Types/Storage Engines
MySQL table types/storage engines are essential features that can be used effectively for maximizing the
database's performance.
It handles create, read, and update operations for storing and managing the information in a database.
We can use the below query that determines which table types/storage engines our
MySQL server supports.
1. mysql> SHOW ENGINES;
The Support column value indicates whether an engine can be used or not in the current server.
A value of YES, NO, or DEFAULT shows that table type is available, not available, and available & currently
set as the default table type/storage engine.
It is essential to understand each table type's features in MySQL that enable us to decide the most appropriate among
them for our table that maximizes the performance of our databases.
Each table type in MySQL has its own advantages and disadvantages. Let us learn each table type/storage engine
and its advantages and disadvantages for choosing which one to use among them.
ISAM Table
It is abbreviated as Indexed Sequential Access Method.
This table type/storage engine has been deprecated and removed from MySQL version 5.x.
MyISAM now replaces the functionalities of this table. The size of an ISAM table is 4 GB, which requires
expensive hardware. It is not portable.
MyISAM Table
It is an extension of the ISAM storage engine.
The MyISAM table types are optimized for compression and speed and can be easily portable between system to
system.
Before version 5.5, if we do not specify the table type during table creation, it was the default storage engine. From
version 5.x,
The MyISAM table size is dependent on the OS and can be up to 256 TB.
MyISAM table type can store 64 keys per table and contains 1024 bytes maximum key length.
The MyISAM tables work very fast, but they are not transaction-safe.
Advantages of MyISAM
If you are new, it will be best to start with MyISAM because it is simple to design and create.
Disadvantages of MyISAM
When we lock the entire table, in that case, MyISAM is slower than InnoDB.
InnoDB Table
The InnoDB tables in MySQL fully support transaction-safe storage engine with ACID-compliant.
InnoDB tables are also portable between systems to systems similar to MyISAM.
InnoDB tables can also be checked and repairs by MySQL whenever necessary.
Advantages of InnoDB
InnoDB tables arrange our data on the disk based on the primary key.
Disadvantages of InnoDB
MERGE Table
This table combines multiple MyISAM tables with a similar structure (identical column and index information with
the same order) into a single table.
This table uses indexes of the component tables because it does not have its own indexes.
When we join multiple tables, it can also be used to speed up the database's performance.
We can perform only INSERT, SELECT, DELETE, and UPDATE operations on the MERGE tables.
If we use the DROP TABLE query in this storage engine, MySQL only removed the MERGE specification, and the
underlying tables cannot be affected.
Advantages of MERGE
The main advantage of this table is to remove the size limitation from MyISAM tables.
Disadvantages of MERGE
MySQL allows us to use only identical (similar structure) MyISAM tables for the MERGE table.
It cannot support all MyISAM features, such as we cannot create FULLTEXT indexes on MERGE tables.
Memory Table
The memory table type/storage engine creates tables, which will be stored in our memory.
It is also known as HEAP before MySQL version 4.1. This table type is faster than MyISAM because it uses hash
indexes that retrieve results faster.
We already know that data stored in memory can be crashed due to power issues or hardware failure. Therefore, we
can only use this table as temporary work areas or read-only caches for data pulled from other tables. Hence, the
memory/heap tables will be lost whenever the MySQL server halts or restarts. The memory/heap table's data life
depends on the uptime of the database server.
Advantages of Memory
The main advantage of this table type is its speed, which is very fast. It is because it uses hash indexing that retrieves
results faster.
Disadvantages of Memory
It is not a good idea to use the MEMORY storage for the long term because the data would be lost easily as soon as
the power failure or hardware crash.
CSV Table
The CSV table type/storage engine stores data in comma-separated values in a file.
It provides a convenient way to migrate data into many different software packages, such as spreadsheet
software.
This table type is not as good as a general database engine; however, it enables us to exchange our data most
effectively and easily. In addition, it will scan the whole table during the read operation.
Advantages of CSV
This table type/storage engine is advantageous when we need to export complex data from one application to a CSV
file and then import it into another application.
Disadvantages of CSV
It is not good to store a large volume of data or larger data types like BLOB, although such types are supported.
FEDERATED Table
The FEDERATED table type/storage engine supports MySQL from version 5.03 that allows access data from a
remote MySQL server without using the cluster/replication technology. The federated storage engine located in local
storage does not store any data. If we will query data from a federated table stored in local memory, MySQL
automatically pulled data from the remote federated tables. It is to note that it's a way for a server, not for a client,
for accessing a remote database. It is an effective way to combine data from more than one host or copy data from
remote databases into local tables without using the data import and export method.
ARCHIVE Table
This table type/storage engine allows us to store a large volume of data in a compressed format to save disk space
and cannot be modified. Thus, it is the perfect storage engine to store log data that is no longer in active use, such
as the old invoice or sales data. It compresses the data during the insertion and can decompress it using
the Zlib library.
The archive tables only support INSERT and SELECT queries. It does not support most of the data types, such as
index data type, without which we need to scan a full table for reading rows.
Since it stored the information in a compressed format and if we want to read the table, we first need to decompress
the information. This process will take time to perform complex searches and retrievals. Therefore, if we have to
perform a large number of queries in these tables, it is beneficial to use another table such as MyISAM.
BDB Table
BDB stands for the Berkeley DB engine, which is developed by SleepyCat software. It is similar to InnoDB in the
transaction-safe. It is based on the hash storage mechanism that makes the recovery of information very quickly. It
supports page-level locking, but the data file is not portable.
o ISAM
o MyISAM
o MERGE
o InnoDB
o MEMORY (HEAP)
o ARCHIVE
o BDB
o CSV
o FEDERATED
We can use the below query that determines which table types/storage engines our MySQL server supports.
The Support column value indicates whether an engine can be used or not in the current server. A value
of YES, NO, or DEFAULT shows that table type is available, not available, and available & currently set as the
default table type/storage engine.
It is essential to understand each table type's features in MySQL that enable us to decide the most
appropriate among them for our table that maximizes the performance of our databases. Each table type in
MySQL has its own advantages and disadvantages. Let us learn each table type/storage engine and its
advantages and disadvantages for choosing which one to use among them.
ISAM Table
It is abbreviated as Indexed Sequential Access Method. This table type/storage engine has been deprecated
and removed from MySQL version 5.x. MyISAM now replaces the functionalities of this table. The size of an
ISAM table is 4 GB, which requires expensive hardware. It is not portable.
MyISAM Table
It is an extension of the ISAM storage engine. The MyISAM table types are optimized for compression and
speed and can be easily portable between system to system. Before version 5.5, if we do not specify the
table type during table creation, it was the default storage engine. From version 5.x, InnoDB is used as the
default table type/storage engine.
The MyISAM table size is dependent on the OS and can be up to 256 TB. It can be compressed
into read-only tables that save spaces in memory. MyISAM table type can store 64 keys per table and
contains 1024 bytes maximum key length. The MyISAM tables work very fast, but they are not
transaction-safe.
Advantages of MyISAM
o If you are new, it will be best to start with MyISAM because it is simple to design and create.
o It is faster than other storage engines in general conditions.
o It provides full-text indexing/searching.
Disadvantages of MyISAM
InnoDB Table
The InnoDB tables in MySQL fully support transaction-safe storage engine with ACID-compliant. It is the first
table type that supports foreign keys. The InnoDB tables also provide optimal performance. Its size can be up
to 64TB. InnoDB tables are also portable between systems to systems similar to MyISAM. InnoDB tables can
also be checked and repairs by MySQL whenever necessary.
Advantages of InnoDB
Disadvantages of InnoDB
MERGE Table
MERGE table is also known as MRG_MyISAM. This table combines multiple MyISAM tables with a similar
structure (identical column and index information with the same order) into a single table. This table uses
indexes of the component tables because it does not have its own indexes. When we join multiple tables, it
can also be used to speed up the database's performance. We can perform only INSERT, SELECT, DELETE,
and UPDATE operations on the MERGE tables. If we use the DROP TABLE query in this storage engine,
MySQL only removed the MERGE specification, and the underlying tables cannot be affected.
Advantages of MERGE
o The main advantage of this table is to remove the size limitation from MyISAM tables.
o It performs more efficient searches and repairs.
o It manages the set of log tables easily.
Disadvantages of MERGE
o MySQL allows us to use only identical (similar structure) MyISAM tables for the MERGE table.
o It cannot support all MyISAM features, such as we cannot create FULLTEXT indexes on MERGE
tables.
o It reads indexes slower.
Memory Table
The memory table type/storage engine creates tables, which will be stored in our memory. It is also known
as HEAP before MySQL version 4.1. This table type is faster than MyISAM because it uses hash indexes that
retrieve results faster. We already know that data stored in memory can be crashed due to power issues or
hardware failure. Therefore, we can only use this table as temporary work areas or read-only caches for
data pulled from other tables. Hence, the memory/heap tables will be lost whenever the MySQL server halts
or restarts. The memory/heap table's data life depends on the uptime of the database server.
Advantages of Memory
The main advantage of this table type is its speed, which is very fast. It is because it uses hash indexing that
retrieves results faster.
Disadvantages of Memory
It is not a good idea to use the MEMORY storage for the long term because the data would be lost easily as
soon as the power failure or hardware crash.
CSV Table
The CSV table type/storage engine stores data in comma-separated values in a file. It provides a convenient
way to migrate data into many different software packages, such as spreadsheet software. This table type is
not as good as a general database engine; however, it enables us to exchange our data most effectively and
easily. In addition, it will scan the whole table during the read operation.
Advantages of CSV
This table type/storage engine is advantageous when we need to export complex data from one application
to a CSV file and then import it into another application.
Disadvantages of CSV
o It is not good to store a large volume of data or larger data types like BLOB, although such types are
supported.
o It makes data retrieval slow because there is no indexing.
FEDERATED Table
The FEDERATED table type/storage engine supports MySQL from version 5.03 that allows access data from
a remote MySQL server without using the cluster/replication technology. The federated storage engine
located in local storage does not store any data. If we will query data from a federated table stored in local
memory, MySQL automatically pulled data from the remote federated tables. It is to note that it's a way for a
server, not for a client, for accessing a remote database. It is an effective way to combine data from more
than one host or copy data from remote databases into local tables without using the data import and export
method.
ARCHIVE Table
This table type/storage engine allows us to store a large volume of data in a compressed format to save disk
space and cannot be modified. Thus, it is the perfect storage engine to store log data that is no longer in
active use, such as the old invoice or sales data. It compresses the data during the insertion and can
decompress it using the Zlib library.
The archive tables only support INSERT and SELECT queries. It does not support most of the data types, such
as index data type, without which we need to scan a full table for reading rows.
Since it stored the information in a compressed format and if we want to read the table, we first need to
decompress the information. This process will take time to perform complex searches and retrievals.
Therefore, if we have to perform a large number of queries in these tables, it is beneficial to use another table
such as MyISAM.
BDB Table
BDB stands for the Berkeley DB engine, which is developed by SleepyCat software. It is similar to InnoDB in
the transaction-safe. It is based on the hash storage mechanism that makes the recovery of information very
quickly. It supports page-level locking, but the data file is not portable.
1. SELECT: This statement used to retrieve the data from the tables and views.
Syntax:SELECT * FROM [TABLE NAME];
2. SELECT DISTINCT: This statement used to retrieve the distinct data from the table and
view.
3. WHERE: This MySQL Query command is used to filter the data for specific value
4. AND: This condition is used to filter the data on the basis of conditions.
Syntax:SELECT [COLUMN NAMES] FROM [TABLE NAME] WHERE [CONDITION] AND [CONDITON];
EMP_COUNTRY=”INDIA”;
5. OR: This MySQL Query Command combines the data from the table for the specific
condition.
6. IN: This operator helps in filtering the data based on a value match.
(‘val1’,’val2’);
Example:SELECT EMP_NAME, EMP_SALARY FROM EMPLOYEE WHERE EMP_COUNTRY IN
(‘INDIA’,‘USA’, ‘NZ’);
7. ORDER BY: It is used to sort the data in a particular order for a particular column in
Syntax:SELECT COLUMN1, COLUMN2, FROM [TABLE NAME] ORDER BY Column1 desc, Column2
asc;
asc;
8. LIKE: This MySQL Query Command is used to retrieve the data from the table for the
specific pattern.
like‘SA%’;
Syntax:SELECT Column1, Column2 FROM EMPLOYEE WHERE Column3 BETWEEN val1 AND
val2;
Example:SELECT EMP_ID, EMP_NAME FROM EMPLOYEE WHERE EMP_SAL BETWEEN 2000 AND
5000;
10. IS NULL: This is used for checking the value or retrieving the data for the particular
column is null.
11. INSERT: This statement allows you to insert one or more rows in the table.
12. UPDATE: This MySQL Query command is used to update the specific table and column
13. DELETE: The Command is used to delete the record from the table for a particular
value.
14. INNER JOIN: It allows you to retrieve the data from two table matches in one and other
tables.
15. LEFT JOIN: It helps you in providing the data from two or more tables and it will
retrieve all the columns from the left table and will provide the data from the right table
that matches.
Syntax:SELECT T1.C1, T2.C2 FROM TABLE T1 LEFT JOIN TABLE T2 ON T1.C1= T2.C1;
D.DEP_ID;
16. RIGHT JOIN: This MySQL Query command helps in retrieving the data from two or
more tables, taking the full records from the right table, and matches the data with the left
Syntax:SELECT T1.C1, T2.C2 FROM TABLE T1 RIGHT JOIN TABLE T2 ON T1.C1= T2.C1;
= D.DEP_ID;
17. CROSS JOIN: It will provide the Cartesian product of rows of joined tables like if 10 rows
Example:SELECT * FROM EMPLOYEE EMP CROSS JOIN DEP WHERE EMP.DEP_ID= DEP.DEP_ID;
18. GROUP BY: This is used to get the data for the particular value in the combined form.
19. UNION and UNION ALL: It’s allows you to retrieve the data of multiple queries.
Syntax:SELECT Col1, Col2 from T1 UNION SELECT Col1, Col2 FROM T2;
20. DROP: This statement is used for dropping the table from the database.
Syntax:DROP TABLE [TABLE NAME] Condition;
1. CTE (common table expression): These Commands are used to retrieve the data from
the tables.
100;
Syntax:SELECT Col1, Col2 FROM TABLE T1 WHERE COL3 in (SELECT Col3 from Table T2
WHERE Condition);
Example:SELECT EMP_ID, EMP_NAME FROM EMPLOYEE WHERE EMP_SAL in (Select SAL FROM
3. TRANSACTION: This is used to start, commit, and rollback the particular query.
5. CREATE DATABASE: This MySQL Query command is used to create the new database.
Syntax:CREATE DATABASE DATABASE_NAME;
Syntax:SHOW DATABASES;
● MySQL
● MySQLi
● PDO
mysql() is now obsolete because of security issues like SQL injection etc, but
the other two are being actively used.
MySQLi
e. Run it
What Is phpMyAdmin?
It can help you run MySQL queries, repair, optimize, check tables, and
execute other database management commands.
Before you start building PHP connection to MySQL database you need to
know what PHPMyAdmin is. It’s a control panel from where you can manage
the database that you’ve created. Open your browser and go to
localhost/PHPMyAdmin or click “Admin” in XAMPP UI.
When you first installed XAMPP, it only created the username for it to be
accessed, you now have to add a password to it by yourself. For this, you
have to go to User account where the user is the same as the one shown in
this picture:
Now click Edit privileges and go to Change Admin password, type your
password there and save it. Remember this password as it will be used to
connect to your Database.
Note: It is not necessary to change the password to access databases on the
localhost. It is a good practice and that is why we have used a password.
Create Database
Now return to the homepage of PHPMyAdmin. Click the New button to create
a new database.
In the new window, name your database as per your need, I am naming it
“ practice ”. Now select Collation as utf8_general_ci, as we are using it for
learning purposes and it will handle all of our queries and data that will be
covered in this tutorial series. Now click on Create and your database will be
created.
The newly created database will be empty now, as there are no tables in it. I
will be covering that in the upcoming series where we will learn how to create
tables and insert data in it. In this tutorial, we are going to connect this
database to localhost using PHP
Now, locate the folder where you installed XAMPP and open the htdocs
folder (usually c:/xampp). Create a new folder inside c:/xampp/htdocs/ and
name it “practice” we will place web files in this folder. Why we have created
a folder in htdocs? XAMPP uses folders in htdocs to execute and run your
PHP sites.
Note: If you are using WAMP, then add your practice folder in c:/wamp/www
folder.
Create a new PHP file and name it db_connnection.php and save it. Why am I
creating a separate database connection file? Because if you have created
multiple files in which you want to insert data or select data from the
databases, you don’t need to write the code for database connection every
time.
1.<?php
2.
3.functionOpenCon()
4.{
5.$dbhost="localhost";
6.$dbuser="root";
7.$dbpass="1234";
8.$db="example";
9.
10.
11. $conn=newmysqli($dbhost, $dbuser, $dbpass,$db)ordie("Connect
failed: %s\n". $conn-> error);
12.
13.
14. return$conn;
15. }
16.
17. functionCloseCon($conn)
18. {
19. $conn->close();
20. }
21.
22. ?>
Here is the explanation of the variable that we have used in our
db_connection file:
1. $dbhost will be the host where your server is running it is usually
localhost.
2. $dbuser will be the username i.e. root and $dbpass will be the
password which is the same that you used to access your
PHPMyAdmin.
3. $dbname will be the name of your database which we have created in
this tutorial.
You just have to include it by using PHP custom function include (include
‘connection.php’) on the top of your code and call its function and use it. It
also helps when you are moving your project location from one PC to another
and you have to change the values on the single file and all the changes will
be applied to all the other files automatically.
Create a new PHP file to connect to your database. Name it index.php and
add this code in this file.
1.<?php
2.include'db_connection.php';
3.
4.$conn=OpenCon();
5.
6.echo"Connected Successfully";
7.
8.CloseCon($conn);
9.
10. ?>
Run it!
MySQL Connection
A connection is a computer science facility that allows the user to connect with the database server
software. A user can connect with the database server, whether on the same machine or remote locations.
Therefore, if we want to work with the database server to send commands and receive answers in the form
of a result set, we need connections. In this article, we are going to learn how we can connect to MySQL
Server in various ways.
1. MySQL
If we find the MySQL program in the PATH, we can use the below command to connect to the MySQL
Server:
Next, we need to type the password for the root user account and press Enter. If everything is correct, it
should give the screen as follows:
This screen indicates that we have successfully connected with the MySQL database server, where we can
send commands and receive answers in the form of a result set.
Suppose we want to display all databases available in the current server; we can use the command as
follows:
Step 1: Launch the MySQL Workbench. We should get the following screen:
Step 2: Navigate to the menu bar, click on the 'Database' and choose Connect to Database option or press
the CTRL+U command. We can also connect with the database server by just clicking the plus (+)
button located next to the MySQL Connections. See the below image:
Step 3: After choosing any of the options, we will get the below screen:
Step 4: Fill the box to create a connection, such as connection name and username, whatever you want. By
default, the username is the root, but we can also change it with a different username in the Username
textbox. After filling all boxes, click the Store in Vault ... button to write the password for the given user
account.
Step 5: We will get a new window to write the password and click the OK button.
Step 6: After entering all the details, click on the Test Connection to test the database connectivity is
successful or not. If the connection is successful, click on the OK button.
Step 7: Again, click on the OK button for saving connection setup. After finishing all the setup, we can see this
connection under MySQL Connections for connecting to the MySQL database server. See the below output
where we have Localhost3 connection name:
Step 8: Now, we can click this newly created connection that displays the current schemas and a pane for
entering queries:
Syntax
The following is the syntax for MySQL connection using PHP Script:
Server: It is the name of a host that runs the database server. By default, its value will be lcalhost:3306.
User: It is the name of a user who accesses the database. If we will not specify this field, it assumes the
default value that will be the name of a user that owns the server process.
Password: It is the password of a user whose database you are going to access. If we will not specify this
field, it assumes the default value that will be an empty password.
New_link: If we make a second call with the same arguments in the mysql_connect() function, MySQL does
not establish a new connection. Instead, we will get the identifier of the already opened database
connection.
If we want to disconnect from the MySQL database server, we can use another PHP function
named mysql_close(). It accepts only a single parameter that will be a connection returned by the
mysql_connect() function. Its syntax is given below:
Example
The following example explain how to connect to a MySQL server using PHP Script:
1. <html>
2. <head>
3. <title>MySQL Server Connection</title>
4. </head>
5. <body>
6. <?php
7. $servername = 'localhost:3306';
8. $username = 'javatpoint';
9. $dbpass = 'jtp123';
10. $conn = mysql_connect($servername, $username, $password);
11. if(! $conn ) {
12. die('Connection failed: ' . mysql_error());
13. }
14. echo 'Connection is successful';
15. mysql_close($conn);
16. ?>
17. </body>
18. </html>
● The most commonly used statement of MySQL Select statement. MySQL SELECT
statement used to fetch data/records from database table.
● In the INSERT INTO statement of MySQL, you can insert single or multiple rows into
the database table.
● MySQL UPDATE statement, you can update the single row using the UPDATE &
WHERE clause statement at a time.
● The DELETE statement is used to remove/delete a specific row or multiple rows
using the MySQL DELETE & WHERE clause.
Table Of Content
● SELECT Statement MySQL
● INSERT Statement MySQL
● UPDATE Statement MySQL
● DELETE Statement MySQL
Syntax
In MySQL SELECT statement syntax is :-
Params
● Fields :- It’s a database column name. You can fetch one or more fields using
SELECT statement.
● Table :- It’s a database table name.
● WHERE :- It’s a option. If You want to filter some data that time specify any
condition using the WHERE clause.
Syntax
In MySQL INSERT INTO statement syntax is :-
Params
● Fields :- It’s a database column name. You can insert value into in it.
● table_name :- It’s a database table name.
● Value :- It’s your column values, that you want to insert into database
Here, we will insert multiple rows into database table name users.
Syntax
In MySQL UPDATE statement syntax is :-
[WHERE Clause]
Params
UPDATE users
SET first_name = 'Michel'
WHERE id = 501;
DELETE Statement MySQL
In MySQL, You can use the DELETE statement to delete/remove all the data from database
table.
You can delete/remove records single data from the databse table on the basis of
conditions.
Syntax
In MySQL DELETE statement syntax is :-
OR
Params
● http://your-site-address.com:2082
● https://your-site-address.com:2083
Access the URL, and it should take you to the cPanel login page.
Type in your cPanel username and password and click on the Log in button. Once you’re
inside the control dashboard, you should see various modules and features.
We will explain each section along with the main functions.
Files
The Files section contains features for direct file management. From this section, you can
upload files without an FTP client, specify privacy levels, and set up a backup wizard.
Using File Manager
cPanel includes an easy-to-use file manager that works like desktop file explorer
applications – users can easily edit, remove, or add files and folders.
Clicking on the File Manager button will take you to the page that lists all of your website
files.
From there, you can start performing various actions by clicking on the respective menu
button. For example, if you want to move a file, select one and click on the Move button.
This will trigger a prompt to appear – simply fill out the new path and click on the Move
File(s) button.
Keep in mind that every file in cPanel can affect your server, so be careful when modifying
them to prevent any site errors.
Preferences
This is where you can configure the various aspects of the cPanel dashboard, such as
language, theme, and user information. The module also includes features to add a new
cPanel account and manage the password.
Storing Basic Information
One of the most important features of this section is adding your contact information. Use
the Contact Information application to store your contact information and preferences.
Start by including your email address in the provided field – the system will notify you of any
activity in your account, like reaching your mailbox and bandwidth quota.
Then navigate to the bottom of the page and check the boxes based on your contact
preferences. For example, if you want to get notified when your email address is changed,
click on the respective box.
Simply check the boxes to receive notifications about specific circumstances. Be sure to
click Save when you’re done.
Databases
If your website uses a content management system (CMS) like WordPress, it will need a
database to store all the important data and information, such as usernames, passwords,
and posts.
This section is all about managing the databases – its tasks are mostly done via MySQL
Databases and phpMyAdmin.
Create Databases and Manage MySQL Users
Click on MySQL Databases. On the New Database section, enter your desired database
username and click Create Database.
To add a new MySQL user, scroll down to the MySQL Users section. There, enter the
username and password and click Create User.
When you’re done, navigate to the Add User To Database section. Click on the dropdown
menu and choose the new user and database – this will grant the user access to the
selected database.
Once you’re done, you can start importing your files into this new database
via phpMyAdmin. To ensure reliable site performance, try optimizing your database.
Clicking on the Install Now button will prompt you to configure the software setup and site
settings.
Simply choose the installation URL and specify the options for your WordPress website.
Additionally, you can create an admin account, select a free WordPress theme, and
pre-install multiple plugins.
Once you’re done, click Install to start the WordPress installation process.
Software
Generally, your web hosting will set up a dedicated section for
the Softaculous Apps Installer. However, you can also find the auto-installer
under the Software section, along with other scripts and applications.
Since this section mainly deals with programming languages, such as PHP,
Ruby, and Perl, it is mostly used by advanced users.
That said, if you have a Cloudflare account, you can sign up and enable it
here.
Setting Up Cloudflare
To set up Cloudflare for your website, simply click on the Cloudflare icon
and enter your credentials to sign in.
Once you’re logged in, click on the Domains option, and you will see a list of
active websites with their DNS details.
The easiest way to set up Cloudflare is by adding the CNAME records. This
will point a subdomain to your main domain. Go ahead and click
the Provision Domain with CNAME Setup button.
This will change the status to active, enabling Cloudflare on your website.
Domains
It’s common for webmasters to use one hosting account for multiple sites.
Hence, the Domains section provides the features for all domain-related
tasks, such as managing domain names, setting up addon domains for
newly purchased domain names, and creating parked domains.
Using the DNS Zone Editor
cPanel contains Zone Editor to create, edit, and manage DNS (Domain
Name System) records. This feature performs multiple actions, from adding
an MX record to managing domain configurations.
To create an MX record, simply click on the respective button. This will open a
new page – enter the priority value in the Priority box and a fully qualified
domain name in the Destination field.
Finally, click Add An MX Record to save the changes.
Metrics
The features listed in this module work to track website statistics, from
counting the number of visitors to monitoring resource usage. These metrics
give you access to insights that can help you make decisions to improve your
website.
Viewing Traffic Statistics
Every tool provides extensive reports of your site. For instance,
the Awstats feature is integrated with the AWStats software. It reports traffic
statistics hourly, daily, or monthly.
Moreover, the software records visitors’ data, including their browser and
location information. This helps analyze their behavior and make changes to
improve performance.
To view specific information, simply find the options on the left sidebar. To
view reports on a specific timeframe, change the Reported period.
Security
Security is a big concern for every site owner, especially if you store sensitive
information such as credit card details. This module helps you keep an eye on
key security settings on your hosting account.
There are multiple features to optimize your site security, including SSH
Access to connect to your server via SSH and SSL/TLS to enable SSL
certificates for extra security. Another essential feature is the Hotlink
Protection which prevents other sites from stealing your site resources.
Configure Hotlink Protection
Hotlink protection prevents your files from being used on other sites –
preserving your bandwidth amount. To do so, open the Hotlink
Protection feature and configure the settings.
How Do I Use FileZilla And cPanel?
Create Your FTP Account
To create your FTP account in cPanel, please refer to the following article.
Creating An FTP Account In cPanel
How Do I Connect Filezilla To cPanel?
You can connect your account and access your site via FTP with Filezilla,
CoreFTP, or Cyberduck. Of course, you can use any FTP program you want,
but these are the three that cPanel provides auto-magic instant setup files for.
If you use another program, you'll need to set it up manually.
Configure Filezilla Manually
1. Under FTP Accounts, click Configure FTP Client for the chosen FTP
account.
1. Click the blue FTP Configuration File button under Filezilla to download
the file.
1. Run the Configuration File that you just downloaded on your local PC