MySQL
MySQL
Other kinds of data stores can be used, such as files on the file system or large hash
tables in memory but data fetching and writing would not be so fast and easy with those type of
systems.
So now a days we use relational database management systems (RDBMS) to store and
manager huge volume of data. This is called relational database because all the data is stored
into different tables and relations are established using primary keys or other keys known as
foreign keys.
RDBMS Terminology:
Before we proceed to explain MySQL database system, let’s revise few definitions related to
database.
MySQL Database:
MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. MySQL is
developed, marketed, and supported by MySQL AB, which is a Swedish company. MySQL is
becoming so popular because of many good reasons.
• MySQL is released under an open-source license. So you have nothing to pay to use it.
• MySQL is a very powerful program in its own right. It handles a large subset of the
functionality of the most expensive and powerful database packages.
• MySQL uses a standard form of the well-known SQL data language.
• MySQL works on many operating systems and with many languages including PHP, PERL,
C, C++, JAVA etc.
• MySQL works very quickly and works well even with large data sets.
• MySQL is very friendly to PHP, the most appreciated language for web development.
• 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).
• MySQL is customizable. The open source GPL license allows programmers to modify the
MySQL software to fit their own specific environments.
MySQL Data Types
Properly defining the fields in a table is important to the overall optimization of your
database. You should use only the type and size of field you really need to use; don't define a
field as 10 characters wide if you know you're only going to use 2 characters. These types of
fields (or columns) are also referred to as data types, after the type of data you will be storing in
those fields.
MySQL uses many different data types, broken into three categories: numeric, date and
time, and string types.
MySQL uses all the standard ANSI SQL numeric data types, so if you're coming to MySQL
from a different database system, these definitions will look familiar to you. The following list
shows the common numeric data types and their descriptions.
• INT - 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. You can specify a width of up to 11 digits.
• TINYINT - A very small integer that can be signed or unsigned. If signed, the allowable
range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can
specify a width of up to 4 digits.
• SMALLINT - 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. You can
specify a width of up to 5 digits.
• MEDIUMINT - 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. You can specify a width of up to 9 digits.
• BIGINT - 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. You can specify a width of up to 11 digits.
• FLOAT(M,D) - 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.
• DOUBLE(M,D) - 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.
• 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.
You have various options as to the user interface or client to MySQL that you choose to
use. The three most popular user interfaces are the command-line interface mysql (also known
as the MySQL monitor), MySQL Control Center (MySQLCC for short), and phpMyAdmin.
The MySQL monitor comes with your basic installation. It is a command-line interface. This
is always available as an option, it is simple to use, and it works on all platforms.
The MySQL Control Center (MySQLCC) is a graphical user interface. It is written using the
Qt windowing toolkit, which is cross-platform. At the time of writing, MySQLCC was available for
Unix and Windows, and it is planned to be available for OS X in the future.
phpMyAdmin is a Web-based interface for using MySQL. It is very popular with ISPs that
supply MySQL for use in developing Web applications.
If you have MySQL installed, you already have the MySQL monitor. MySQLCC is an official
MySQL product, but depending on which MySQL version you have, it may be a separate
download. You can get it from
mysql -u username -p
After you're logged in, you can see what databases exist on the system by using the
SHOW command:
show databases;
Notice that the command has a semicolon at the end of the line. Most commands you
type in the monitor need to be terminated with a semicolon; otherwise, MySQL will not execute
them.
This allows you to split complex commands over multiple lines for readability. You can
also type \g (backslash g) instead of the semicolon, but most people use the semicolon.
You can select a database from this list and type this:
use databasename;
(Substitute the name of the database you want to use.)
You can get information on a particular table by typing
describe tablename;
Identifiers in MySQL
Generally speaking, identifiers can contain any characters, with these exceptions:
Creating a Database
After design, the first step in creating a database is, logically enough, to tell MySQL that
we want to create a new database. We do this with the CREATE DATABASE SQL
statement, as follows:
You can check to see that this statement worked by executing the command
show databases;
Before we can create any tables or do anything else with the employee database, we
need to tell MySQL that we want to work with our new database. We do this with the use
statement, as follows:
use student;
Creating Tables
To create the tables in the employee database, we use the CREATE TABLE SQL
statement. The usual form of this statement is
We can delete an entire database and all its contents with the following statement
drop database databasename;
You can delete a single table with the DROP TABLE statement, for example,
drop table assignment;
You can delete an index with the DROP INDEX statement, for example,
drop index part_name on employee;
As well as creating and deleting tables, we often need to be able to change the structure of an
existing table. We can do this with the ALTER TABLE statement. ALTER TABLE has many, many
variations we can use to alter table structure.
Altering Existing Table Structures
Using INSERT
The INSERT SQL statement is used to insert rows into a table.
Using DELETE
The DELETE SQL statement allows us to delete rows from a table.
Using TRUNCATE
The TRUNCATE statement allows us to delete all the rows from a table. For example:
We can use the UPDATE SQL statement to change rows already stored in the database.
Using INSERT
The INSERT SQL statement is used to insert rows into a table.
insert into department values (42, 'Finance'), (128, 'Research and Development'), (NULL, 'Human
Resources'), (NULL, 'Marketing');
Viewing Records
We retrieve data from the database using the SELECT statement. We will cover SELECT
fairly exhaustively in the next few chapters. For the moment, we only need to know that typing
Using DELETE
The DELETE SQL statement allows us to delete rows from a table.
delete from table_name;
In this form, the delete statement will delete all the rows from the student table.
We can also limit which rows are deleted using a WHERE clause, for example,
delete from student where Year=‘3’;
Using TRUNCATE
The TRUNCATE statement allows us to delete all the rows from a table. For example:
TRUNCATE TABLE student;
This query would delete all the students from the student table. This is faster than a DELETE
statement because it works by dropping the table and re-creating it empty. One thing to bear in
mind is that TRUNCATE is not transaction safe.
Using UPDATE
We can use the UPDATE SQL statement to change rows already stored in the database.
update student set course=‘BSIT' where id=6651;
Querying MySQL
In this chapter, we will cover the SQL SELECT statement in some detail. This is probably the
most important statement in SQL. It is the statement we use to select rows from one or more
database table(s).
Simple Queries
An example of the simplest form of the SELECT statement is as follows:
Aliases
We have the ability to rename columns or expressions in a SELECT statement, and the
new name will be what is shown in the output. For example, we can use the following query:
select name as studentName from student;
Here, we have renamed the column name as studentName just within the context of this
query.
It is relatively easy to type the previous query by mistake when what you actually meant
was this:
select count(distinct job) from employee;
String Functions
MySQL's string functions fall into two categories: the string processing functions and the
string comparison functions.
String Comparison Functions
In addition to offering the equality operator for comparing two strings, MySQL provides
various comparison functions we can also use:
LIKE: Performs string wildcard matching.
RLIKE: Performs regular expression matching.
STRCMP: String comparison, just like the strcmp() function in C.
MATCH: Performs full-text searching.
The RLIKE function can be used to match on the basis of regular expressions.
A regular expression is a pattern that describes the general shape of a string. There is a
special notation for describing the features we would like to see in matching strings.
Now, let's look at an example of how to use these patterns with RLIKE. Consider the
following query: