Basic
Walkthrough MySQL Part-5
In this part we will study some more useful database objects. For example,
VIEWS, TRIGGERS, PROCEDURES and few other.
As we had an introduction to trigger creation in part 2, we will
discuss how we run scripts first. MySQL supports different kind of scripts. You
can run system shell scripts (.bat, .cmd, .js, .vbs,.sh and many more). Let us try to understand how we can run
scripts. Simple SQL script looks like
|
C:\Documents
and Settings\user1>mysql
-uuser1 -ppassword1 < script.sql |
|
use sample; select * from
trans; select count(*)
from trans; \q |
|
C:\Documents and Settings\user1>mysql -uuser1
-ppassword1 < script.sql Warning: Using a password on the command line
interface can be insecure. ID
AMOUNT CODE 1 123.45 in 2
34.40 out 3
100.00 in 4
15.00 out 5
0.75 in count(*) 5 |
This was a simple SQL script. This is to
be run in db2 command window. The syntax has been indicated in blue. Being an
example script, I just listed few commands. In
actual scripts these lines are usually over hundreds.
Similarly, we can use system scripts
too for accomplishing same mission. Have a look at following script. This one
is equivalent to the previous one.
|
C:\Documents
and Settings\user1>Script.bat |
|
mysql -uuser1
-ppassword1 -e “select id,name,dept,job from staff” mysql -uuser1
-ppassword1 -e “select count(*) from staff” |
This is very straightforward that we use mysql.
The scripts are usually written for various tasks including generating daily
reports. As a rule of thumb, add all the commands to script file that are to be
repeated in future and invoke the script using one of above syntaxes.
Let
us create another trigger on TRANS table (created in part 2) to ensure that
user uses in or out keywords only in code column. Using anything else will be
waste of data. So we will try to reject entry if the code is not set properly.
|
C:\Documents
and Settings\user1>mysql -uuser1 -ppassword1
sample < TriggerCheckCode.sql |
|
DELIMITER // CREATE TRIGGER
Check_Code BEFORE INSERT ON TRANS FOR EACH ROW IF UPPER(NEW.CODE)<>'IN' AND
UPPER(NEW.CODE)<>'OUT' THEN SIGNAL
SQLSTATE '75000' SET MESSAGE_TEXT='Code must be either "in" or
"out"'; END IF; // DELIMITER; |
|
|
This
trigger will stop us from entering wrong ‘Code’
in TRANS table. This is to ensure that data entered will be usable by the
trigger update_total trigger to update TOTAL tables.
|
select * from total |
|
+---------+--------+---------+---------------------+ | WEEKNUM |
INCOME | EXPENSE | TIMEUPDATE
| +---------+--------+---------+---------------------+ | 49 | 224.20 | 49.40 | 2018-12-09 19:38:50 | +---------+--------+---------+---------------------+ 1 row in set
(0.03 sec) |
|
insert into TRANS values (default,
120.00,'Not') |
|
ERROR 1644 (75000): Code must be either "in" or
"out" |
|
insert into TRANS values (default,
120.00,'In') |
|
Query OK, 1 row affected (0.01 sec) |
|
select * from total |
|
+---------+--------+---------+---------------------+ | WEEKNUM | INCOME | EXPENSE | TIMEUPDATE | +---------+--------+---------+---------------------+ | 49 | 344.20 | 49.40 | 2018-12-10 07:57:27 | +---------+--------+---------+---------------------+ 1 row in set (0.01 sec) |
Cool,
the transaction was applied to TOTAL table when we entered the correct code.
Remember we had used ‘case free’ iN and OuT key words. It will work with any case.
Let
us try to have some flavor of VIEWS. As
explained earlier the VIEW is combination of columns from different tables. We
will create another table called PERSONS with names of all family members. Then
we will create a view joining TRANS and PERSONS tables to save info about who
had carried out the transaction. For this purpose, we will add a column PID to
TRANS table.
|
alter table TRANS add column PID CHAR(2); |
|
Query OK, 0
rows affected (0.38 sec) Records: 0 Duplicates: 0 Warnings: 0 |
Now create table PERSONS
as below.
|
create table PERSONS (PID CHAR(2) not
null primary key, FNAME CHAR(16), LNAME CHAR(12)); |
|
Query OK, 0
rows affected (0.16 sec) |
Add some data to the table.
|
insert into PERSONS values
(‘00’,’David’,’Boon’); Query OK, 1 row affected (0.00 sec) insert into PERSONS values
(‘01’,’Marry’,’Boon’); Query OK, 1 row affected (0.00 sec) insert into PERSONS values
(‘02’,’John’,’Boon’); Query OK, 1 row affected (0.00 sec) select * from persons; +-----+-------+-------+ | PID | FNAME |
LNAME | +-----+-------+-------+ | 00 | David | Boon | | 01 | Marry | Boon | | 02 | John
| Boon | +-----+-------+-------+ 3 rows in set
(0.00 sec) |
Now we need to enter these PID in TRANS
table to indicate who carried the transactions out. Let’s do that.
|
select
* from TRANS; +----+--------+------+------+ | ID | AMOUNT | CODE | PID | +----+--------+------+------+ | 1 | 123.45 | in | NULL | | 2 | 34.40 | out
| NULL | | 3 | 100.00 | in | NULL | | 4 | 15.00 | out
| NULL | | 5 | 0.75 | in
| NULL | | 6 | 120.00 | In | NULL | +----+--------+------+------+ 6 rows in set (0.00 sec) update
TRANS set PID='00' where ID=0; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 update
TRANS set PID='01' where ID=1 Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 update
TRANS set PID='00' where ID=2 Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 update
TRANS set PID='02' where ID=3 Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 update
TRANS set PID='02' where ID=4 Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 update
TRANS set PID='00' where ID=6 Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 select
* from TRANS; +----+--------+------+------+ | ID | AMOUNT | CODE | PID | +----+--------+------+------+ | 1 | 123.45 | in | 01
| | 2 | 34.40 | out
| 00 | | 3 | 100.00 | in | 02
| | 4 | 15.00 | out
| 02 | | 5 | 0.75 | in
| NULL | | 6 | 120.00 | In | 00
| +----+--------+------+------+ 6 rows in set (0.00 sec) |
Now
we can create a view name trans_view to reflect name of the person instead of
PID. PID is usually handy for use with managing multiple tables within
database. Views are very handy and useful for creating end-user reports.
|
create view trans_view as SELECT t.AMOUNT,t.CODE,p.FNAME,p.LNAME
from TRANS t, PERSONS p where p.PID=t.PID; Query OK, 0
rows affected (0.03 sec) select * from trans_view; +--------+------+-------+-------+ | AMOUNT | CODE
| FNAME | LNAME | +--------+------+-------+-------+ | 123.45 |
in | Marry | Boon | | 34.40 | out
| David | Boon | | 100.00 |
in | John | Boon
| | 15.00 | out
| John | Boon | | 120.00 |
In | David | Boon | +--------+------+-------+-------+ 5 rows in set
(0.00 sec) |
So,
we had an idea how Views work. In actual
practice, the views are rarely so simple. But the logic behind a view is always
same. JOIN tables and pretend to be one table. Please note that for simplicity
of examples, we did not use TIMESTAMP column in TRANS table, which is mostly used
to keep track of transaction along with some other more useful info.
For simplest use of database system,
we have already covered most objects. In actual usage I hardly need anything
other than what we have covered so far. The other objects are for some more
sophisticated actions. For example, PROCEDURES and FUNCTIONS are very useful
but for the scope of this tutorial these will be covered in last parts.
Let’s
discuss some of useful built-in functions.
CHAR
The CHAR function returns a fixed-length character string representation of the
argument.
DATE
The DATE function returns a date derived from a value.
HEX
The HEX function returns a hexadecimal representation of a value.
LOWER
The LOWER function returns a string in which all the characters have been
converted to lowercase characters.
UPPER
The UPPER function returns a string in which all the characters have been
converted to uppercase characters.
LEFT
The LEFT function returns a string that consists of the specified number of
leftmost bytes of the specified string units.
LENGTH
The LENGTH function returns the length of a value.
LOCATE
The LOCATE function returns the position at which the first occurrence of an
argument starts within another argument.
LTRIM
The LTRIM function removes blanks or hexadecimal zeros from the beginning of a
string expression.
MAX
The MAX scalar function returns the maximum value in a set of values.
MIN
The MIN scalar function returns the minimum value in a set of values.
RIGHT
The RIGHT function returns a string that consists of the specified number of
rightmost bytes or specified string unit from a string.
RPAD
The RPAD function returns a string that is padded on the right with blanks or a
specified string a specified number of times.
RTRIM
The RTRIM function removes blanks or hexadecimal zeros from the end of a string
expression.
SUBSTRING
The SUBSTRING function returns a substring of a string.
TIME
The TIME function returns a time derived from a value.
TIMESTAMP
The TIMESTAMP function returns a timestamp derived from its argument or
arguments.





