MySQL DBMS
From Beginner to Master
▪1
What is Database?
These days, organizations are considering data as one important
resource like finance, human resource and time.
The management of these resources needs special attention and
development strategy.
large amounts of data must be stored for efficient update and
retrieval.
Due to this reason it is considered that databases are essential to every
business.
Databases are likewise found at the core of many modern
organization’s information systems.
Why Database?…
Database system is about
Organization of data
Efficient retrieval of data
Reliable storage of data
Maintaining consistent data
Making useful information for decision making
What will be learned?
This topic aims to equip students with practical skill in
understanding,
designing,
developing,
administering and managing a database system in an
organization.
Using MySQL DBMS
What is Database?…
Database is a collection of information, usually,
kept in a repository, on a particular subject
A database is a storage space for content or
information (data)
Database (DB) is used
1. To store data
2. To manipulate information so that it can be sorted
and/or searched.
3. To make record keeping and tracking fast and
efficient.
Ultimate purpose of a DB system?
▪is to transform and manage
▪Data ▪Information ▪Knowledge ▪Action
▪Data driven decision making
What does “managing data” mean?
Making information work for us
Making information useful
Avoiding "accidental disorganisation”
Making information easily accessible and integrated with the
rest of our work
Basic Definitions
Database:
A collection of related data.
Data:
Known facts that can be recorded and have
an implicit meaning.
Mini-world:
Some part of the real world about which
data is stored in a database.
Database Management System
(DBMS):
A software package/ system to facilitate
the creation and maintenance of a
computerized database.
Database System:
The DBMS software together with the data itself. Sometimes, the applications are also
included.
Database and DBMS
Database is simply a collection of data. In relational
database, data is organized into tables.
Database Management System (DBMS) is software to
create, use, maintain and manage a collection of data
required by an organization or for a specific purpose.
eg: Oracle, MSSQL, DB4, MySQL, etc)
Student_ID Name Major CGPA
S-101 Belay Biology 2.01
S-102 Mike Inf Sc 3.56
S-103 Tye Acct 2.73
… … …
MySQL Introduction
MySQL is an open source database management
system
SQL stands for the Structured Query Language. It
defines how to create, insert, retrieve, modify
and delete data
Can be downloaded for FREE from
www.mysql.com
Reference sites
NASA, Yahoo!, Compaq, Motorola
Basic MySQL Operations
Create Database
Create table
Insert records
Load data
Retrieve records
Update records
Delete records
Modify table
Join table
Drop table
Optimize table
Count, Like, Order by, Group by
More advanced ones (sub-queries, stored procedures, triggers, views …)
How MySQL Stores data
A MySQL server can store several databases
Databases are stored as directories
Default is at /usr/local/mysql/var/
Tables are stored as files inside each database (directory)
For each table, it has three files:
table.FRM file containing information about the table structure
table.MYD file containing the row data (actual data)
table.MYI containing any indexes created for the tables, as well
as some statistics about the tables.
Login to MySQL
mysql –h hostname –u username –p [password]
Example
% mysql -u usrname -p
Enter password: passowrd
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23 to server version: 3.23.41.
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Create User
Syntax
mysql>use mysql;
Use database mysql, used by the system
Example
mysql>insert into user (Host, User, Password)
values (‘localhost’, ‘aauuser’, ‘MyPass@123’);
Create a new database user aauuser
An alternative
GRANT USAGE ON *.* TO ‘aauuser’@’localhost‘ IDENTIFIED BY
‘MyPass@123’;
Create User
mysql>insert into db (Host, Db, User, Select_priv,
Insert_priv, Update_priv, Delete_priv, Create_priv,
Drop_priv) values (‘localhost’, ‘is_workshopbd’, ‘aauuser‘,
‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’, ‘Y’);
Create a new database is_workshopbd for user aauuser
mysql>flush privileges
Reloads the privileges from the grant tables in the database
mysql
An alternative
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE,
DROP ON is_workshopbd.* TO ‘aauuser’@’localhost’
IDENTIFIED BY ‘MyPass@123’;
Create Database
What are the current databases at the server?
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| mysql | mysql is a database (stores users’ password ) used by system.
| is_workshopbd |
+----------------------+
Create a database (make a directory) whose name is MyDB
mysql> create database MyDB;
Select database to use
mysql> use MyDB;
Database changed
What tables are currently stored in the MyDB database?
mysql> show tables;
Empty set (0.00 sec)
Create Table
CREATE TABLE Table_Name (column_specifications)
Example
mysql> CREATE TABLE student
-> (
-> student_ID INT UNSIGNED NOT NULL,
-> name VARCHAR(20) NOT NULL,
-> major VARCHAR(50),
-> cgpa float
-> );
Query OK, 0 rows affected (0.00 sec)
Student_ID Name Major CGPA
Display Table Structure
mysql> show tables;
+--------------------+
| Tables_in_MyDB |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)
mysql> describe student;
+---------------+----------------------+------+------+----------+--------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------+-------+-----+-----------+-------+
| student_ID | int(10) unsigned | | |0 | |
| name | varchar(20) | | | | |
| major | varchar(50) | YES | | NULL | |
| cgpa | varchar(5) | YES | | NULL | |
+---------------+----------------------+-------+------+----------+-------+
4 rows in set (0.00 sec)
Modify Table Structure
ALTER TABLE table_name Operations
mysql> alter table student add primary key (student_ID);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings:
0 mysql> describe student;
+---------------+--------------------- +-------+------+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------+-------+------+----------+-------+
| student_ID | int(10) unsigned | | PRI | 0 | |
| name | varchar(20) | | | | |
| major | varchar(10) | YES | | NULL | |
| cgpa | varchar(5) | YES | | NULL | |
+---------------+----------------------+-------+------+-----------+-------+
4 rows in set (0.00 sec)
Insert Record
INSERT INTO table_name SET col_name1=value1,
col_name2=value2, col_name3=value3, …
INSERT INTO table_name SET col_name1=value1,
col_name2=value2, col_name3=value3, …
Example
mysql> INSERT INTO student SET student_ID=S-101, name=‘Belay',
major=‘Biology’, cgpa=‘2.01’;
Query OK, 1 row affected (0.00 sec)
Student_ID Name Major CGPA
S-101 Belay Biology 2.01
Retrieve Record
Syntax
SELECT what_columns
FROM table or tables
WHERE condition
Example
mysql> SELECT major, cgpa FROM student
WHERE name=‘Mike';
Student_ID Name Major CGPA
S-101 Belay Biology 2.01
+-------+-------+
| major| cgpa | +-------+-------+ S-102 Mike Inf Sc 3.56
| Inf Sc | 3.56 |
S-103 Tye Acct 2.73
+-------+-------+
1 row in set (0.00 sec) … … …
mysql> SELECT * FROM student;
Update Record
UPDATE table_name
SET which columns to change
WHERE condition
Example
mysql> UPDATE student SET cgpa=3.01WHERE name=‘Belay';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql>
SELECT * FROM studentWHERE name=‘Belay’;
+------------+---------------+--------+--------+
| name | student_ID | major | cgpa |
+------------+---------------+--------+--------+
| Belay | S-101 | Biology | 3.01 |
+------------+---------------+--------+--------+
1 row in set (0.00 sec)
Delete Record
DELETE FROM table_nameWHERE condition
Example
mysql> DELETE FROM studentWHERE name=‘Belay';
Query OK, 1 row affected (0.00 sec)
Mysql> DELETE FROM student;
Will delete ALL student records!
More Table Retrieval
OR
mysql> select name from student where major = 'BCB' OR major = 'CS';
COUNT (Count query results)
mysql> select count(name) from student where major = 'BCB' OR major = 'CS';
ORDER BY (Sort query results)
mysql> select name from student where major = 'BCB' OR major = 'CS‘ ORDER
BY name; mysql> select name from student where major = 'BCB' OR major = 'CS‘
ORDER
BY name DESC; mysql> select * from student where major = 'BCB' OR major
= 'CS‘ ORDER BY student_id ASC, name DESC
LIKE (Pattern matching)
mysql> select name from student where name LIKE "J%";
DISTINCT (Remove duplicates) mysql> select major from student; mysql>
select DISTINCT major from student;
Group By
Cluster query results based on different groups
Example
mysql> select major, count(*) from student GROUP BY major;
+---------+----------+
| major | count(*) |
+---------+----------+
| BBMB | 3|
| BCB | 3|
| Chem | 1|
| CS | 5|
| IG | 2|
| Math | 2|
| MCDB | 3|
| Stat | 2|
+---------+------------+
8 rows in set (0.00 sec)
NULL
No Value
Can not use the usual comparison operators (>, =, != …)
Use IS or IS NOT operators to compare with
Example
mysql> select name from student where project_ID = NULL;
Empty set (0.00 sec)
mysql> select name from student where project_ID IS NULL;
+-------+
| name|
+-------+
| Jerry |
+-------+
1 row in set (0.00 sec)
Drop Table
DROP TABLE table_name
Example
mysql> drop table student; Query
OK, 0 rows affected (0.00 sec)
Logout MySQL
mysq> quit;
Data Loading
Load batch data instead of inserting records one by one
Example
mysql> LOAD DATA LOCAL INFILE “proj.txt" INTO TABLE project;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> LOAD DATA LOCAL INFILE “emp.txt" INTO TABLE employee;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
Loading to a Table from File
MariaDB [today]> load data local infile "C:\\xampp\\mysql\\data\\today\\emp.txt"
into table Employee;
Query OK, 4 rows affected, 7 warnings (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 7
MariaDB [today]> select * from Employee;
+--------+-------------------------+----------+-------+-----------+------------+
| EID | EName | Salary | Age | Gender | Tel |
+--------+-------------------------+----------+-------+-----------+------------+
|E-01 | 'Abebe Belay' | 12 | 34 | M | 121212 |
| E-02 | 'Birtukan Ayalew' | 24 | 29 | F | 131313 |
| E-03 | 'Yohannes Wube' | 10 | 42 | M | 141414 |
| E-04 | 'Mulugeta Gewe' | 29 | 32 | M | 151515 |
+-------+--------------------------+----------+------+--------+----------------+
Writing to a Table
MariaDB [today]> select * from employee into outfile "emp_out.txt";
Query OK, 4 rows affected (0.00 sec)
Joining Tables
Types of Joins
Inner joins
return only matching rows
enable a maximum of 256 tables to be joined at the same
time.
Types of Joins
Outer joins
return all matching rows, plus nonmatching rows
from one or both tables
can be performed on only two tables or views at a
time.
Left Full Right
Cartesian Product
To understand how SQL processes a join, it is important to
understand the concept of the Cartesian product.
A query that lists multiple tables in the FROM clause without
a WHERE clause produces all possible combinations of rows
from all tables. This result is called the Cartesian product.
select * from
one, two;
Cartesian Product
How many rows are returned from this query?
select * from three,
four;
Table Three Table Four Partial Results Set
X A X B
1 a1 2 x1
X A X B 1 a1 2 x2
1 a1 2 x1 1 a1 3 y
1 a2 2 x2 1 a1 5 v
2 b1 3 y 1 a2 2 x1
2 b2 5 v 1 a2 2 x2
4 d
1 a2 3 y
5*4=20
1 a2 5 v
2 b1 2 x1
Creating a Table with PK 2 b1 2 x2
and FK
Joining tables requires tables be related with one another using
primary key foreign key relationship.
One has to create the related tables before joining/combining tables.
MariaDB [today]> create table employee
-> (EID char(5) primary key,
-> Name char(25) not null,
-> Age int unsigned,
-> Salary float
-> );
Query OK, 0 rows affected (0.29 sec)
MariaDB [today]> create table dept
-> (DID char(3) primary key,
-> DName char(50) not null,
-> Tel char(15),
-> MangID char(5),
-> constraint foreign key fk_dept_emp(MangID) references employee(EID)
-> );
Query OK, 0 rows affected (0.21 sec)
Insert values to a Table
MariaDB [today]> insert into employee values ('E-01', 'Abebe', 29, 14650);
Query OK, 1 row affected (0.01 sec)
MariaDB [today]> insert into employee values ('E-02', 'Belay', 41, 24000);
Query OK, 1 row affected (0.01 sec)
MariaDB [today]> insert into employee values ('E-03', 'Taye', 52, 4000);
Query OK, 1 row affected (0.02 sec)
MariaDB [today]> insert into employee values ('E-04', 'Hirut', 32, 14000);
Query OK, 1 row affected (0.09 sec)
MariaDB [today]> select * from employee;
+-------+----------+--------+----------+
| EID | Name | Age | Salary |
+-------+----------+--------+----------+
| E-01 | Abebe | 29 | 14650 |
| E-02 | Belay | 41 | 24000 |
| E-03 | Taye | 52 | 4000 |
| E-04 | Hirut | 32 | 14000 |
+-------+----------+-------+-----------+
4 rows in set (0.00 sec)
Insert values to a Table with FK
Inserting a record in Department table where the attribute MangID
is a foreign key referring to values in Employee table on the attribute
EID.
MariaDB [today]> insert into dept values ('D01', 'Finance', '343434', 'E-02');
Query OK, 1 row affected (0.02 sec)
MariaDB [today]> insert into dept values ('D02', 'Human Resource', '555555', 'E-03');
Query OK, 1 row affected (0.03 sec)
MariaDB [today]> select * from dept;
+-------+------------------------+-----------+-------------+
| DID | DName | Tel | MangID |
+-------+------------------------+-----------+-------------+
| D01 | Finance | 343434 | E-02 | | D02 |
Human Resource | 555555 | E-03 |
+-------+------------------------+-----------+-------------+
2 rows in set (0.00 sec)
Combining Tables
Combining tables using Cartesian will result in a table where each
record of the first table is combined with each table of the second
table in a combinatory manner. This will note extract related tuple.
MariaDB [today]> select * from employee, dept;
+--------+----------+-------+----------+-------+------------------------+-----------+-------------+
| EID | Name | Age | Salary | DID | DName | Tel | MangID |
+-------+-----------+-------+----------+-------+------------------------+-----------+-------------+
| E-01 | Abebe | 29 | 14650 | D01 | Finance | 343434 | E-02 |
| E-01 | Abebe | 29 | 14650 | D02 | Human Resource| 555555 | E-03 |
| E-02 | Belay | 41 | 24000 | D01 | Finance | 343434 | E-02 |
| E-02 | Belay | 41 | 24000 | D02 | Human Resource | 555555 | E-03 |
| E-03 | Taye | 52 | 4000 | D01 | Finance | 343434 | E-02 |
| E-03 | Taye | 52 | 4000 | D02 | Human Resource | 555555 | E-03 |
| E-04 | Hirut | 32 | 14000 | D01 | Finance | 343434 | E-02 |
| E-04 | Hirut | 32 | 14000 | D02 | Human Resource | 555555 | E-03 |
+-------+----------+------+------------+--- ---+------------------------+------------+-------------+
8 rows in set (0.00 sec)
Null Values and Outer joins
Explicit joins in SQL:
Product(name, category)
Purchase(prodName, store)
SELECTProduct.name, Purchase.store
FROM Product JOINPurchase ON
Product.name = Purchase.prodName
Same as:
SELECTProduct.name, Purchase.store
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
But Products that never sold will be lost !
Inner Joining Related Table
Inner Join will extract only record that are related based on Foreign
Key Primary Key equality.
MariaDB [today]> select * from employee inner join dept on eid=mangid;
+--------+----------+-------+----------+-------+-------------------------+-----------+-------------+
| EID | Name | Age | Salary | DID | DName | Tel | MangID |
+-------+-----------+-------+----------+-------+-------------------------+-----------+-------------+
| E-02 | Belay | 41 | 24000 | D01 | Finance | 343434 | E-02 |
| E-03 | Taye | 52 | 4000 | D02 | Human Resource | 555555 | E-03 |
+-------+-----------+-------+----------+-------+-------------------------+-----------+-------------+
2 rows in set (0.00 sec)
Outer Joining Related Table
MariaDB [today]> insert into dept values (‘D03', 'Procurement', '777777’, NULL);
Query OK, 1 row affected (0.03 sec)
MariaDB [today]> select * from dept;
+-------+------------------------+-----------+-------------+
| DID | DName | Tel | MangID |
+-------+------------------------+-----------+-------------+
| D01 | Finance| 343434 | E-02 | | D02 |
Human Resource | 555555 | E-03 |
| D03 | Procurement | 777777 | NULL |
+-------+------------------------+-----------+-------------+
2 rows in set (0.00 sec)
MariaDB [today]> select * from employee;
+-------+----------+--------+----------+ In outer join, record either from the
| EID | Name | Age | Salary | left or right or both sides which are
+-------+----------+--------+----------+ not related will be included in the
| E-01 | Abebe | 29 | 14650 | result.
| E-02 | Belay | 41 | 24000 |
| E-03 | Taye | 52 | 4000 |
| E-04 | Hirut | 32 | 14000 |
+-------+----------+-------+-----------+
4 rows in set (0.00 sec)
Null Values and Outer joins
Left outer joins in SQL:
Product(name, category)
Purchase(prodName, store)
SELECTProduct.name, Purchase.store
FROM Product LEFTOUTERJOINPurchase ON
Product.name = Purchase.prodName
Right outer joins in SQL:
Product(name, category)
Purchase(prodName, store)
SELECTProduct.name, Purchase.store
FROM Product RightOUTERJOINPurchase ON
Product.name = Purchase.prodName
Left and Right Outer Join
MariaDB [today]> select * from employee left outer join dept
-> on employee.eid=dept.mangid;
+--------+---------+--------+----------+----------+------------------------+-----------+-------------+
| EID | Name | Age | Salary | DID | DName | Tel | MangID |
+--------+---------+--------+----------+----------+------------------------+-----------+-------------+
| E-02 | Belay | 41 | 24000 | D01 | Finance | 343434 | E-02 |
| E-03 | Taye | 52 | 4000 | D02 | Human Resource | 555555 | E-03 |
| E-01 | Abebe | 29 | 14650 | NULL | NULL | NULL | NULL |
| E-04 | Hirut | 32 | 14000 | NULL | NULL | NULL | NULL |
+--------+---------+--------+----------+----------+------------------------+-----------+------------+
4 rows in set (0.01 sec)
MariaDB [today]> select * from employee right outer join dept
-> on employee.eid=dept.mangid;
+----------+-----------+---------+------------+-------+------------------------+----------+-------------+
| EID | Name | Age | Salary | DID | DName | Tel | MangID |
+----------+----------+----------+------------+-------+------------------------+----------+-------------+
| E-02 | Belay | 41 | 24000 | D01 | Finance | 343434 | E-02 | | E-03 | Taye | 52 |
4000 | D02 | Human Resource | 555555 | E-03 |
| NULL | NULL | NULL | NULL | D03 | Procurement | 777777 | NULL |
+----------+----------+----------+------------+-------+------------------------+----------+-------------+
3 rows in set (0.00 sec)
Backup Database
mysqldump
Writes the contents of database tables into text files
Example
>mysqldump –p bcb –T ./
Select … INTO OUTFILE ‘/path/outputfilename’;
Example
>SELECT * FROM student INTO OUTFILE ‘/dump/student.txt’;
mysql –u username –p password –h host database > /path/to/file
mysql –u bcb –p tuckseed0 bcb > test
MySQL Indexing
Index
Index columns that you search for
Example
MariaDB [today]> alter table employee add index(name);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [today]> describe employee;
+--------+--------------------+------+-------+----------+--------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-------+-----------+-------+
| EID | char(5) | NO | PRI | NULL | |
| Name | char(25) | NO | MUL | NULL | |
| Age | int(10) unsigned | YES | | NULL | |
| Salary | float | YES | | NULL | |
+--------+--------------------+------+--------+----------+-------+
4 rows in set (0.00 sec)
MySQL Optimization (cont.)
EXPLAIN
Find what is going on a slow query
Example
mysql> EXPLAIN select * from student s, project p
where s.project_ID = p.project_ID order by p.level;
Stored Procedures
Stored Procedures
Like procedures in other languages
Allow for quick reuse of language constructs
Reduce the amount of code that must be written
Allow parameter-driven code
Must change the default command delimiter “;” to something else
General format in MySQL
DELIMITER $$
CREATE PROCEDURE procName(optionalParamDecls)
BEGIN
SQL_Statements
END$$
DELIMITER ;
Stored Procedures
Example:
MariaDB [today]> delimiter @@
MariaDB [today]> create procedure myview()
-> begin
-> select * from employee inner join dept
-> where employee.eid=dept.mangid;
-> end; @@
Query OK, 0 rows affected (0.01 sec)
MariaDB [today]> delimiter ;
Executing Stored Procedures
call procName(); call
procName(arg1, arg2,…);
Examples:
call advList();
call p4(12345);
Executing Stored Procedures
Example:
MariaDB [today]> call myview();
+------+-------+------+--------+-----+----------------+--------+--------+
| EID | Name | Age | Salary | DID | DName | Tel | MangID |
+------+-------+------+--------+-----+----------------+--------+--------+
| E-02 | Belay | 41 | 24000 | D01 | Finance | 343434 | E-02 |
| E-03 | Taye | 52 | 4000 | D02 | Human Resource | 555555 | E-03 |
+------+-------+------+--------+-----+----------------+--------+--------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
MariaDB [today]> ->
Modifying and Deleting Stored
Procedures
To drop a stored procedure in MySQL
DROP PROCEDURE procName;
or
DROP PROCEDURE IF EXISTS procName;
To modify a procedure, DROP it then CREATE it again
SQL Triggers
To monitor a database and take a corrective action when a
condition occurs
Examples:
Charge $10 overdraft fee if the balance of an account after a withdrawal
transaction is less than $500
Limit the salary increase of an employee to no more than 5% raise
CREATE TRIGGER trigger-name
trigger-time trigger-event
ON table-name
FOR EACH ROW trigger-
action;
trigger-time {BEFORE, AFTER} trigger-
event {INSERT,DELETE,UPDATE}
Triggers
MariaDB [today]> create trigger default_trig
-> before insert on employee
-> for each row
-> begin
-> if(new.salary is NULL) then
-> set new.salary=5000;
-> end if;
-> end @@
Query OK, 0 rows affected (0.03 sec)
MariaDB [today]>delimiter ;
Example: Triggers MariaDB [today]> select * from employee;
+------+-------+------+--------+
| EID | Name | Age | Salary |
+------+-------+------+--------+
| E-01 | Abebe | 29 | 14650 |
| E-02 | Belay | 41 | 24000 |
| E-03 | Taye | 52 | 4000 |
| E-04 | Hirut | 32 | 14000 |
+------+-------+------+--------+
MariaDB [today]> insert into employee values ('E-05','Hareg',29,NULL);
Query OK, 1 row affected (0.03 sec)
MariaDB [today]> select * from employee;
+------+-------+------+--------+
| EID | Name | Age | Salary |
+------+-------+------+--------+
| E-01 | Abebe | 29 | 14650 |
| E-02 | Belay | 41 | 24000 |
| E-03 | Taye | 52 | 4000 |
| E-04 | Hirut | 32 | 14000 |
| E-05 | Hareg | 29 | 5000 |
+------+-------+------+--------+
5 rows in set (0.00 sec)
MariaDB [today]>
Remarks about Trigger
If you have the same or similar processing that has to go on
during insert and delete, then it’s best to have that in a
procedure or function and then call it from the trigger.
A given trigger can only have one event.
A good naming standard for a trigger is
<table_name>_event if you have the room for that in the
name.
Just like a function or a procedure, the trigger body will
need a begin … end unless it is a single statement trigger.
The Special Powers of a Trigger
While in the body of a trigger, there are potentially two sets of column
values available to you, with special syntax for denoting them.
old.<column name> will give you the value of the column before the DML
statement executed.
new.<column name> will give you the value of that column after the DML
statement executed.
Insert triggers have no old values available, and delete triggers have no
new values available for obvious reasons.
Update triggers have both the old and the new values available.
Only triggers can access these values this way.
These tables are created when the transaction start and dropped when
the transaction end.
These tables (old and new) will be used for logging as well
End of Topic