1 Introdution To MySQL
1 Introdution To MySQL
Language (SQL) . MySQL RDBMS is an open source application which means it is free to
use under the GNU General Public License.
Structured Query Language (SQL) – the most common language for relational system, used
to insert, update, delete and retrieve content in the database.
Example applications which uses MySQL database are: Joomla, Drupal, Wordpress,phpBB,
Magento, Moodle.
Data Definition Language (DDL) – DDL consists of commands that specify the database
scheme. It is also used to create and destroy databases and database objects.
Data Manipulation Language (DML) – DML is used to add new records, update records,
delete records and retrieve records. It enables you to access and manipulate data in the
database.
Data Control Language (DCL) – DCL consists of commands to create user privileges and
control access to data.
Every database is composed of one or more tables. Tables are the foundation of every
Relational Database Management System and one of those is MySQL.
Table – A set of data arranged in columns and rows. The columns represent characteristics of
stored data and the rows represent actual data entries.
Table columns describe the data types and particular attribute of the data.
To create a database in MySQL, we are going to use the CREATE DATABASE command.
We will name our database as “employees”, to do that issue the following command:
Note: each time you issue a command in MySQL it ends with a semi-colon(;).
If you have done it correctly the MySQL will respond a success message, it means we have
successfully created the database. The message would look like this:
Query OK, 1 row affected (0.00 sec)
Let’s try to check the list of databases in our system. Issue the following command:
mysql>SHOW DATABASES;
After we have created our database the next thing to do is to create a table. To create a table,
use the CREATE TABLE command. We are going to name our table as “employee_record”.
Note: in order to create table, first we are going to choose on which database we are going to
insert the table. Issue the command:
mysql> USE yourLastname;
Database changed
Our table consists of the following field: (id, f_name, l_name, position, age, salary, email)
unsigned - Unsigned type can be used to permit only nonnegative numbers in a column
Note: if you have encoded the command correctly, it will display success message:
Query OK, 0 rows affected (0.11 sec)
Now that we’ve created the table, issue the command to display the list of tables.
mysql> SHOW TABLES;
+--------------------+
| Tables_in_employee |
+--------------------+
| employee_record |
+--------------------+
1 row in set (0.00 sec)
To display the lists all the column names along with their column types of the table. Use the
DESCRIBE command:
mysql> DESCRIBE employee_record;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | | PRI | 0 | auto_increment |
| f_name | varchar(20) | YES | | NULL | |
| l_name | varchar(20) | YES | | NULL | |
|position| varchar(30) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
| email | varchar(60) | YES | | NULL | |
+--------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
MySQL Lesson - Inserting of records
To add new record in the database, use the INSERT INTO command. Here is the syntax of
INSERT INTO command:
INSERT into table_name (column1, column2....)
values (value1, value2...);
Where: table_name is the name of the table where you want to insert new record; column1,
column2 etc. are the name of the columns in the table and value1, value2 etc. are the values
you want to insert in the table.
Note: The number of columns that you specify in INSERT INTO command should match the
number of values you insert; otherwise you will get an error.
We will add a new record in our table. Issue the following command:
INSERT INTO employee_record
(f_name, l_name, position, age, salary, email)
values
("Piolo", "Pascual", "Programmer", 38, 25000,
"[email protected]");
Note:
SELECT command is used to select record in a table. SELECT command is one of the most
useful commands in SQL. The syntax of SELECT command:
SELECT column_names from table_name
We will extract the first and lastname of employees. Issue the command:
Once you have encoded the command correctly, it will display the first and lastname of all
the employee. Result would look like this:
mysql> select f_name,l_name from employee_record;
+------------+---------+
| f_name | l_name |
+------------+---------+
| Piolo | Pascual |
| Sam | Milby |
| John Lloyd | Cruz |
| Rolan | Algara |
| Jericho | Rosales |
| John | Prats |
+------------+---------+
6 rows in set (0.00 sec)
Note: To select all the columns in the table use asterisk (*).
Once you have encoded the command correctly, it will display a success message.
Ex. Query OK, 1 row affected (0.00 sec)
Note: using this technique will make your work faster and it will improve the processing
performance of your database.
Note: The where clause in the update command specifies which records should be updated. If
you exclude the where clause all records will be updated.
We will update the salary of the employees that have a position of programmer. The salary
will increase to 30000. Issue the command:
UPDATE employee_record SET
salary = 30000
WHERE position=”Programmer”;
Once you have encoded the command correctly, it will display a success message.
Ex. Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Note: If we excluded the where clause it will update all the salary of the employee.
Note: The where clause in the delete command specifies which records should be deleted. If
you exclude the where clause all the records in the table will be erased.
Once you have encoded the command correctly, it will display a success message:
Note: If we excluded the where clause it will delete all the records of the employee.
Once you have encoded the command correctly, it will display the first and lastname of the
employee with the id of 4. The result would look like this:
mysql> SELECT f_name, l_name from employee_record WHERE id=2;
+--------+--------+
| f_name | l_name |
+--------+--------+
| Rolan | Algara |
+--------+--------+
1 row in set (0.00 sec)
We will select the distinct values in the field of position. Issue the command:
SELECT DISTINCT position FROM employee_record;
Result:
mysql> SELECT DISTINCT position FROM employee_record;
+-----------------------+
| position |
+-----------------------+
| Programmer |
| System Analyst |
| Network Administrator |
| Encoder |
+-----------------------+
4 rows in set (0.00 sec)
We will select the distinct values in the field of position. Issue the command:
SELECT DISTINCT position FROM employee_record;
Result:
mysql> SELECT DISTINCT position FROM employee_record;
+-----------------------+
| position |
+-----------------------+
| Programmer |
| System Analyst |
| Network Administrator |
| Encoder |
+-----------------------+
4 rows in set (0.00 sec)
AND operator is used to display records if both the first and second condition is TRUE. It can
be used in SQL statement such as SELECT, UPDATE, DELETE.
AND operator syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name=condition1 AND column_name=condition2;
Retrieve the f_name and l_name of employees whose salary is greater than 20000 but not
more than 60000. Issue the following command:
SELECT f_name, l_name from employee_record
where salary > 20000 AND salary < 60000;
Result:
+---------+---------+
| f_name | l_name |
+---------+---------+
| Piolo | Pascual |
| Rolan | Algara |
| Jericho | Rosales |
+---------+---------+
3 rows in set (0.05 sec)
OR operator is used to display records if either the first or second condition is TRUE.
OR operator syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name=condition1 OR column_name=condition2;
Example: Select the f_name and l_name of employees whose position is “Programmer” or
“System Analyst”. Issue the following command:
SELECT f_name, l_name from employee_record
where position=”Programmer” OR position=”System Analyst”;
Result:
+--------+---------+
| f_name | l_name |
+--------+---------+
| Piolo | Pascual |
| Sam | Milby |
| John | Prats |
+--------+---------+
3 rows in set (0.00 sec)
The ORDER BY clause is used to sort the records in the result set specified by a column
name. You can sort the record in ascending (ASC) or descending (DESC) order.
ASC – ascending order (default)
DESC – descending order
ORDER BY syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC;
We are going to select the f_name and l_name and sort it according by
l_name. To do that, issue the command:
Result:
+------------+---------+
| f_name | l_name |
+------------+---------+
| Rolan | Algara |
| John Lloyd | Cruz |
| Sam | Milby |
| Piolo | Pascual |
| John | Prats |
| Jericho | Rosales |
+------------+---------+
6 rows in set (0.11 sec)
The GROUP BY clause is used to group similar data specified by a certain field in your table.
Result:
+-----------------------+
| position |
+-----------------------+
| Encoder |
| Network Administrator |
| Programmer |
| System Analyst |
+-----------------------+
4 rows in set (0.00 sec)