0% found this document useful (0 votes)
6 views15 pages

1 Introdution To MySQL

hgnjfh jgvbkh hgv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

1 Introdution To MySQL

hgnjfh jgvbkh hgv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MySQL is a relational database management system (RDBMS) that uses Structured Query

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.

SQL Commands Categories

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.

Example of DDL commands: CREATE, DROP, ALTER

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.

Example of DML commands: SELECT, INSERT, UPDATE, DELETE

Data Control Language (DCL) – DCL consists of commands to create user privileges and
control access to data.

Example of DCL commands: GRANT, REVOKE

Understanding Tables, Records, and Fields

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.

Table rows contain the actual data.


MySQL Lesson - Creating Database

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:

mysql> CREATE DATABASE yourLastname;

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;

The result would look like this:


mysql> SHOW DATABASES;
+----------------+
| Database |
+----------------+
| employees |
| mysql |
+----------------+
2 rows in set (0.00 sec)
MySQL Lesson - Creating Tables

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)

To create the employee table, issue the following command:


mysql>CREATE TABLE employee_record
(
id int unsigned not null auto_increment primary key,
f_name varchar(20),
l_name varchar(20),
position varchar(30),
age int,
salary int,
email varchar(60)
);

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:

1. The name of our table is employee_record.


2. Values with quotes are string type (f_name, l_name, position, email) while (age,
salary) are integer type.
3. We have not inserted a record in id because we have defined it as auto_increment
which means that the id field will increase by one every time we are going to insert a
new record.
4. Once you have encoded the command correctly, it will display a success message.
Ex. Query OK, 1 row affected (0.00 sec)
MySQL Lesson - Performing the SQL SELECT command

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

Here is our database table: (employee_record)

id f_name l_name position age salary email


1 Piolo Pascual Programmer 38 25000 [email protected]
2 Sam Milby System Analyst 34 20000 [email protected]
Network 20000 [email protected]
3 John Lloyd Cruz 33
Administrator
4 Rolan Algara Encoder 19 55000 [email protected]
5 Jericho Rosales Encoder 25 55000 [email protected]
6 John Prats Programmer 24 15000 [email protected]

We will extract the first and lastname of employees. Issue the command:

SELECT f_name, l_name from employee_record;

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 (*).

Ex. SELECT * from employee_record;

MySQL Lesson - Insert multiple records in a single query


To insert multiple records in a single query, each record is enclosed in parentheses and is
separated by a comma. Here is the syntax of inserting multiple records in a single query.
INSERT INTO table_name
(column1, column2, column3, etc…)
VALUES
(value1, value2, value3, etc…),
(value1, value2, value3, etc…),
(value1, value3, value3, etc…);

We have here an example of inserting multiple records in a single query:

INSERT INTO employee_record


(f_name, l_name, position, age, salary, email)
VALUES
("Sam", "Milby", "System Analyst", 34, 20000, "[email protected]"),
("John Lloyd", "Cruz", "Network Administrator", 33,
20000,"[email protected]");

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.

MySQL Lesson - Updating records


This lesson will teach us how to update our records in the database. The UPDATE command
is used to update records in the table. Here is the general form of UPDATE command:
UPDATE table_name SET
column_name1 = value1,
column_name2 = value2,
column_name3 = value3 ...
[WHERE conditions];

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.

Let’s see some example of UPDATE command:

Here is our database table: (employee_record)

id f_name l_name position age salary email


1 Piolo Pascual Programmer 38 25000 [email protected]
2 Sam Milby System Analyst 34 20000 [email protected]
Network 20000 [email protected]
3 John Lloyd Cruz 33
Administrator
4 Rolan Algara Encoder 19 55000 [email protected]
5 Jericho Rosales Encoder 25 55000 [email protected]
6 John Prats Programmer 24 15000 [email protected]

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.

MySQL Lesson - Deleting records


The DELETE command is used to delete records in a table. The syntax of DELETE
command:
DELETE from table_name WHERE [conditions];

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.

Example of DELETE command:


DELETE from employee_record WHERE id = 4;

It will delete the record of employee with the id of 4.

Once you have encoded the command correctly, it will display a success message:

Ex. Query OK, 1 row affected (0.00 sec)

Note: If we excluded the where clause it will delete all the records of the employee.

MySQL Lesson - Performing SQL SELECT command using conditions


In this lesson we will learn how to retrieve data using the WHERE clause. The WHERE
clause is used to specify the record you want to retrieve. Here is the syntax:
SELECT f_name, l_name from employee_record WHERE id=2;

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)

MySQL Lesson - Using the SELECT DISTINCT command


SELECT DISTINCT command is used to retrieve unique fields in the table.

SELECT DISTINCT syntax:


SELECT DISTINCT column_name(s) FROM table_name;

Here is our database table: (employee_record)

id f_name l_name position age salary email


1 Piolo Pascual Programmer 38 25000 [email protected]
2 Sam Milby System Analyst 34 20000 [email protected]
Network 20000 [email protected]
3 John Lloyd Cruz 33
Administrator
4 Rolan Algara Encoder 19 55000 [email protected]
5 Jericho Rosales Encoder 25 55000 [email protected]
6 John Prats Programmer 24 15000 [email protected]

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)

MySQL Lesson - Using the SELECT DISTINCT command

SELECT DISTINCT command is used to retrieve unique fields in the table.


SELECT DISTINCT syntax:
SELECT DISTINCT column_name(s) FROM table_name;

Here is our database table: (employee_record)

id f_name l_name position age salary email


1 Piolo Pascual Programmer 38 25000 [email protected]
2 Sam Milby System Analyst 34 20000 [email protected]
Network 20000 [email protected]
3 John Lloyd Cruz 33
Administrator
4 Rolan Algara Encoder 19 55000 [email protected]
5 Jericho Rosales Encoder 25 55000 [email protected]
6 John Prats Programmer 24 15000 [email protected]

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)

MySQL Lesson - Selecting records using AND & OR Operators

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;

Let’s see some example of AND operator:

Here is our database table: (employee_record)

id f_name l_name Position age salary email


1 Piolo Pascual Programmer 38 25000 [email protected]
2 Sam Milby System Analyst 34 20000 [email protected]
Network 20000 [email protected]
3 John Lloyd Cruz 33
Administrator
4 Rolan Algara Encoder 19 55000 [email protected]
5 Jericho Rosales Encoder 25 55000 [email protected]
6 John Prats Programmer 24 15000 [email protected]

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)

MySQL Lesson - Sorting records using ORDER BY clause

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;

Let’s see some example of ORDER BY clause:

Here is our database table: (employee_record)

id f_name l_name position age salary email


1 Piolo Pascual Programmer 38 25000 [email protected]
2 Sam Milby System Analyst 34 20000 [email protected]
Network 20000 [email protected]
3 John Lloyd Cruz 33
Administrator
4 Rolan Algara Encoder 19 55000 [email protected]
5 Jericho Rosales Encoder 25 55000 [email protected]
6 John Prats Programmer 24 15000 [email protected]

We are going to select the f_name and l_name and sort it according by
l_name. To do that, issue the command:

SELECT f_name, l_name


FROM employee_record
ORDER BY l_name;

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)

MySQL Lesson - The GROUP BY clause

The GROUP BY clause is used to group similar data specified by a certain field in your table.

The GROUP BY clause syntax:


SELECT column_name(s)
FROM table_name
GROUP BY column_name;

Let’s see some example of GROUP BY clause:


SELECT position
FROM employee_record
GROUP BY position;

Result:
+-----------------------+
| position |
+-----------------------+
| Encoder |
| Network Administrator |
| Programmer |
| System Analyst |
+-----------------------+
4 rows in set (0.00 sec)

You might also like