LAMP_TRAINING_SESSION_6

729 views
698 views

Published on

Presentaions of LAMP Training Conducted by T.Umapathi Anand and R.Kasturi from Southern Telecom Region, BSNL at RTTC Trivandrum.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
729
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

LAMP_TRAINING_SESSION_6

  1. 1. ! quot;quot; # $ % & ( % % ( % & '
  2. 2. It is a RDBMS Developed, Distributed and Supported by MySQL AB It is Open Source It is Fast, Reliable and Easy to Use It can work as Client/Server or as Embedded System. )
  3. 3. Written in C and C++ Works on Many Platforms It can Handle Multiple CPUs effectively if available Provides Transactional and Non Transactional Storage Engines Very fast Thread based Memory Allocation System Supports In Memory Hash Tables Tested with Purify and Valgrind for best performance Handles Huge Databases Portable Code *
  4. 4. +
  5. 5. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 6 Server version: 6.0.3-alpha-community MySQL Community Server (GPL) Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
  6. 6. +--------------------+ | Database | +--------------------+ | information_schema | | dbone | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) Database changed ,
  7. 7. +----+---------+--------+ | id | name | color | +----+---------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 7 | grape | black | | 8 | mango | yellow | | 6 | goa | green | | 9 | suppota | brown | | 10 | jack | green | +----+---------+--------+ 8 rows in set (0.00 sec) -
  8. 8. +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(5) | NO | PRI | NULL | auto_increment | | name | varchar(25) | YES | | NULL | | | color | varchar(15) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) .
  9. 9. +---------+--------+ | name | color | +---------+--------+ | apple | red | | banana | yellow | | cherry | red | | goa | green | | grape | black | | jack | green | | mango | yellow | | suppota | brown | +---------+--------+ 8 rows in set (0.00 sec) /
  10. 10. +----+--------+-------+ | id | name | color | +----+--------+-------+ | 1 | apple | red | | 2 | cherry | red | +----+--------+-------+ 2 rows in set (0.00 sec) +----+--------+--------+ | id | name | color | +----+--------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 8 | mango | yellow | +----+--------+--------+ 4 rows in set (0.00 sec)
  11. 11. '
  12. 12. 0$ ! ! !! ! ! !! ! ! ! quot; quot; quot; )
  13. 13. 0$ ! # $ # % & ' ( ( $& ) ( ( * ( $& ) + , - ' ( # $ # % & ' ( ( $& ) ( ( * ( $& ) + , ' - & (. /0 ( # $ # % & ' ( ( $& ) + , - ' ( ( * ( $& ) + , - ' & ( 01 ' */ *
  14. 14. 0$ 2222 ' ' ( ( quot;quot;quot;quot; 2222 ' ' ( ( 3 3 4 ' 4 ' 4 4 quot;quot;quot;quot; 4 quot;4 quot; , & &) 334 ' 4 ' quot; quot; quot; / ' 5 / ' / ' 5 / ' +
  15. 15. 0$ ! ( ( ( ( ! , ( , ( , ( , ( ! 0 & 6' , 7 ! / 5 6' , 7
  16. 16. ,
  17. 17. %& quot;#$ '()*+ ,)#)(quot;+#' ! quot; # quot; $ % '% & ( ) * ) quot; + , -. $ %& - / 0 % / 1 2 % 1 -
  18. 18. )3)4 6- 5 5 7 8 )- )- 6 - 9 5 -. 94 :) 5 9 - )3 ; < 2 %= <# > !3 7 7)# ?@ A < : )# ;B . - . C # 8 B $ ) 8 $ quot; ))) : 3 $ )3 ) ) .
  19. 19. -. . +%quot;' D * E E D F= 2 66 * E E 66 F= 2 GH HG 0 quot; ( GH 2HG F= 2 '/
  20. 20. '
  21. 21. quot;,. ( #$ +quot;$ *+#)quot;-#' .) ( $ ) / * .) 0 $ .) 1 ' ) $ .) 2 (7 .) 3 ' .) ''
  22. 22. quot;4 '+ $ $ *+#)quot;-#' .) ( $ ) 5 *% ( . ) 6 .8 .) 7 7 8 9. 8 . ) 99 8 .) :: / .) ')
  23. 23. -&$ (); *+#)quot;-#' .) ( $ ) 55 *% ( . ) 66 .8 .) < 0: .) 9 ; : / 9< ; 0: :< / 0: =< :9 % 0: .) :<9 % // .) 9. 8 . ) '*
  24. 24. quot;,+# *+#)quot;-#' .) ( $ ) > ! ! 1 $< 7 1 1 .) (7 .) % % // 5 7 % // 5 5 ? &) # ' $ > ! 1 $< 7 8 '+
  25. 25. quot;,+# *+#)quot;-#' .) ( $ ) ? % &) # ' $ % 8 0,) = % 7 5 # & $ 8 0,) ? 80; 09# ? 1 &) '
  26. 26. ',
  27. 27. ABS(x). ABS(x). & := ! 1'23 = & := ! 1 +'23 = CEIL(x) & := ! ! 1&'+23 = & := ! ! 1 &'+23 = CONV(N,from_base,to_base) CONV(N,from_base,to_base) & := ! !4 5 617 8# #'23 => ? & := ! !4 5 617 8# -#-23 => ? '-
  28. 28. MOD(N,M). MOD(N,M). & := ! 4 1)'#,23 = & := ! 4 1)/& *23 '# = 6 FLOOR(x) & := ! 9 44 1 &'+23 = & := ! 9 44 1 &'+23 = POW(X,Y) & := ! $4 : 1'#'23 = & := ! $4 : 1'# '23 = 6 '.
  29. 29. ROUND(N,M) & := ! 4;5 1 &)'*#'23 = 6 & := ! 4;5 1 &)'*#+23 = 6 SQRT(x) & := ! 1*23 = & := ! 1'/23 = 6 quot; quot;quot;quot; TRUNCATE(X,D) & := ! ;5! 1&)'*#'23 = 6 & := ! ;5! 1 ''# '23 = )/
  30. 30. ASCII(str). ASCII(str) & := ! ! 1< 23 '< = & := ! ! 1'23 = & := ! ! 1< =< 23 = BIN(N) & := ! 5 1 '23 =@ ? BIT_LENGTH(str) BIT_LENGTH(str) & := ! 5 18 '823 = )
  31. 31. CHAR(N,... [USING charset_name]) charset_name]) & := ! !> 1,,# ' # - #, < -)# < 23 = @' A /@ & := ! !> 1,,# )#,,& 23 ,,& < )< = @' ' ' @ CONCAT() & := ! !4 5 ! 1< <<<< < # # 23 = @' A /@ & := ! !4 5 ! 1< < 5; # #< < 23 = % // CONCAT_WS(separator,str1,str2,...) & := ! !4 5 ! ?: 1<<< ##9 # << << # 5 < 23 =@ & B $ & B / % & @ & := ! !4 5 ! ?: 1<<< ##9 # <5 ; #< 5 < 23 =@ & B / % & @ )'
  32. 32. ELT(N,str1,str2,str3,...) & := ! < > # 1 #< @#< @ < < @#< < < 23 =@ C @ & := ! < > # 1*#< @#< @ < < @#< < < 23 =@ @ FIELD(str,str1,str2,str3,...) & := ! 9 < > < < > # 1< @#< @#< @#< @ < < @#< < < 23 = & := ! 9 #> < < > # 1< < < @#< @#< @ < < @#< < < 23 = FIND_IN_SET(str,strlist) FIND_IN_SET(str,strlist) & := ! 9 5 ? 5? 1< # # # < %<< %# 23 = & := ! 9 5 ? 5? 17 # *+# ,# '//<7 ,# '//#)//< 23 = ))
  33. 33. FORMAT(X,D) & := ! 94 1 '))'& ')*+ #*23 =@ B 6 @ & := ! 94 1 '))'& #*23 =@ B 6 ? HEX(N_or_S) HEX(N_or_S) & := ! > A1'++23 =@ > & := ! > A1< % < 23 = INSERT(str,pos,len,newstr) INSERT(str,pos,len,newstr) & := ! 5 1< # < )#*#< : < 23 = @A D $> & := ! 5 1< # < #*#< : < 23 = @A $@ )*
  34. 34. INSTR(str,substr) INSTR(str,substr) & := ! 5 1< % % << < # % 23 = & := ! 5 =% < < 1< # % < 23 = LCASE(str) LCASE(str) & := ! ! 17 $ 823 => & ) > & := ! ! 18) B 5; 0 '//-< 23 =? C !? INSERT(str,pos,len,newstr) INSERT(str,pos,len,newstr) & := ! 5 1< # < )#*#< : < 23 = @A D $> & := ! 5 1< # < #*#< : < 23 = @A $@ )+
  35. 35. LEFT(str,len) LEFT(str,len) & := ! 9 17 < +23 # => ? & := ! 9 1 ')*+ #)23 => ? LENGTH(str) LENGTH(str) & := ! 5 C > 17 $ 823 = & := ! 5 C > 1 ')*+ 23 = LPAD(str,pos,len,padstr) LPAD(str,pos,len,padstr) & := ! $ 17 $< ,# 23 # 8D< = >EEE/*' #> & := ! $ 17 $< '# # 8D823 = >/*@ )
  36. 36. LTRIM(str) LTRIM(str) & := ! 17 823 => ? & := ! 17 $ 723 =7 $ 7 MID(str,pos,len) MID(str,pos,len) & := ! 17 $ 8# #+23 => ? & := ! 1 ')*+ # '23 )# =? ? REPEAT(str,count) REPEAT(str,count) & := ! $ 17 < )23 > # = >3 3 3 > & := ! $ 1 /#'23 => ? ),
  37. 37. REVERSE(str) REVERSE(str) & := ! 6 17: 823 = > *D ? & := ! 6 1//,23 = >,8 RIGHT(str,len) RIGHT(str,len) & := ! C > 17 $ 8#)23 => ? & := ! C > 1 '///#)23 => ? RPAD(str,len,padstr) RPAD(str,len,padstr) & := ! $ 17 $ < -#7 # D823 = >/*' #EEEE> & := ! $ 1/+/#*#+/23 => ? )-
  38. 38. SPACE(10) & := ! $ ! 1 /23 => ? & := ! $ ! 1 -23 = >8 TRIM(str) TRIM(str) & := ! 17 $ 823 = >/*' # ? & := ! 1 ') 23 => ? UNHEX(str) UNHEX(str) & := ! ; 5 > A17*!* * +/823 = >/*' #> & := ! ; 5 > A17*!* * +C 823 = % // ).
  39. 39. ADDDATE(date,INTERVAL expr unit) ADDDATE(date,INTERVAL & := ! 17'//- / ) 8# E ' 23 => ! ? & := ! 17'//- / ) 8# E + 23 => ! ? CURDATE() & := ! !; 123 => ! ? & := ! !; 12F .3 = ! DATEDIFF(exp1, exp2) & := ! 991 ( 12# .,+ /' /+823 8 = ! & := ! 991 ( 12# .*, /- +823 8 = ! */
  40. 40. MAKETIME(hr,min,sec) MAKETIME(hr,min,sec) & := ! G 1 '# +'23 */# =? 4 4 ? & := ! G 1 '# +'23 */# = % // CURTIME() & := ! !; 123 = ? 4 4 !? & := ! !; 12F)/3 = !6 TIME_TO_SEC(x) & := ! ? 4 ? !18 /H /)823 '/H = & := ! ? 4 ? !18)/H /)823 '/H = quot; *
  41. 41. *'
  42. 42. Single line Query mysql> select VERSION(), CURDATE() ; +-----------------------+------------+ | VERSION() | CURDATE() | +-----------------------+------------+ | 6.0.3-alpha-community | 2008-01-21 | +-----------------------+------------+ 1 row in set (0.08 sec) mysql> *)
  43. 43. Multiple line Query mysql> select VERSION(); select curdate(); +-----------------------+ | VERSION() | +-----------------------+ | 6.0.3-alpha-community | +-----------------------+ 1 row in set (0.00 sec) +------------+ | curdate() | +------------+ | 2008-01-21 | +------------+ 1 row in set (0.00 sec) mysql> **
  44. 44. The Delimiter (;) mysql> select USER() -> , -> CURDATE(), -> curtime(); +----------------+------------+-----------+ | USER() | CURDATE() | curtime() | +----------------+------------+-----------+ | root@localhost | 2008-01-21 | 12:18:56 | +----------------+------------+-----------+ 1 row in set (0.05 sec) mysql> Query Termination mysql> select -> USER() -> c mysql> *+
  45. 45. #-. *quot;' )%@ +)%$ %& #8. ' # ' . A'B; 9 8 F $ && 6 09 D , & ) $ && 6 C 9 D , BF $ &) F : + 6 G@H- D9 D , BF $ &) F : + 6 GIH- E9 D , BF $ &) F $< $< + H- GJ 6 219 D , BF $ &) $ && F KE6 *
  46. 46. Creating and Using Databases mysql> create database LAMP; Query OK, 1 row affected (0.00 sec) mysql> mysql> use LAMP; Database changed mysql> *,
  47. 47. Creating Tables mysql> show tables; Empty set (0.00 sec) mysql> create table fruits( -> id int(5) auto_increment primary key, -> name varchar(25), -> color varchar(15)) engine=myisam; Query OK, 0 rows affected (0.16 sec) mysql> show tables; +----------------+ | Tables_in_lamp | +----------------+ | fruits | +----------------+ 1 row in set (0.00 sec) mysql> *-
  48. 48. Structure of the Table mysql> desc fruits; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(5) | NO | PRI | NULL | auto_increment | | name | varchar(25) | YES | | NULL | | | color | varchar(15) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 3 rows in set (0.05 sec) mysql> *.
  49. 49. Working with Tables mysql> select * from fruits; Empty set (0.00 sec) mysql> insert into fruits(name,color) value ('apple','red'); Query OK, 1 row affected (0.00 sec) mysql> select count(*) from fruits; +----------+ | count(*) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec) +/
  50. 50. Working with Tables mysql> select * from fruits; +----+-------+-------+ | id | name | color | +----+-------+-------+ | 1 | apple | red | +----+-------+-------+ 1 row in set (0.00 sec) mysql> insert into fruits values ('1','orange','orange'); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> +
  51. 51. The effects of auto increment mysql> insert into fruits(id,name,color) values (‘2’,‘cherry’,’red’); Query Ok, 1 row affected (0.00 sec) mysql> insert into fruits(name,color) values (‘banana’,’yellow’); Query Ok, 1 row affected (0.00 sec) mysql> insert into fruits(id,name,color) values (‘7’,‘grape’,’black’); Query Ok, 1 row affected (0.00 sec) mysql> insert into fruits(name,color) values (‘mango’,’yellow’); Query Ok, 1 row affected (0.00 sec) mysql> insert into fruits(id,name,color) values (‘6’,‘goa’,’green’); Query Ok, 1 row affected (0.00 sec) mysql> insert into fruits values (null,‘suppota’,’brown’); Query Ok, 1 row affected (0.00 sec) mysql> insert into fruits(name,color) values (‘jack’,’green’); Query Ok, 1 row affected (0.00 sec) +'
  52. 52. The effects of auto increment (Look at the id value) mysql> select * from fruits; +----+---------+--------+ | id | name | color | +----+---------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 7 | grape | black | | 8 | mango | yellow | | 6 | goa | green | | 9 | suppota | brown | | 10 | jack | green | +----+---------+--------+ 8 rows in set (0.00 sec) mysql> +)
  53. 53. Sorting the records mysql> select name,color from fruits order by color; +---------+--------+ | name | color | +---------+--------+ | grape | black | | suppota | brown | | goa | green | | jack | green | | apple | red | | cherry | red | | banana | yellow | | mango | yellow | +---------+--------+ 8 rows in set (0.00 sec) mysql> +*
  54. 54. Selection with conditions mysql> select * from fruits where color='red'; +----+--------+-------+ | id | name | color | +----+--------+-------+ | 1 | apple | red | | 2 | cherry | red | +----+--------+-------+ 2 rows in set (0.00 sec) mysql> select * from fruits where color='red' or color='yellow'; +----+--------+--------+ | id | name | color | +----+--------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 8 | mango | yellow | +----+--------+--------+ 4 rows in set (0.00 sec) ++
  55. 55. Selection with conditions mysql> select * from fruits -> where (color='red' or color='yellow') and length(name) > 5; +----+--------+--------+ | id | name | color | +----+--------+--------+ | 2 | cherry | red | | 3 | banana | yellow | +----+--------+--------+ 2 rows in set (0.00 sec) mysql> select * from fruits where name like '%o%'; +----+---------+--------+ | id | name | color | +----+---------+--------+ | 8 | mango | yellow | | 6 | goa | green | | 9 | suppota | brown | +----+---------+--------+ 3 rows in set (0.00 sec) +
  56. 56. Selection with conditions mysql> select * from fruits where name like '%a'; +----+---------+--------+ | id | name | color | +----+---------+--------+ | 3 | banana | yellow | | 6 | goa | green | | 9 | suppota | brown | +----+---------+--------+ 3 rows in set (0.00 sec) mysql> select * from fruits where name like '_a%'; +----+--------+--------+ | id | name | color | +----+--------+--------+ | 3 | banana | yellow | | 8 | mango | yellow | | 10 | jack | green | +----+--------+--------+ 3 rows in set (0.00 sec) +,
  57. 57. Copy a table with structure mysql> create table fruits1 like fruits; Query OK, 0 rows affected (0.00 sec) mysql> create table fruits2 select * from fruits; Query OK, 8 rows affected (0.33 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> desc fruits2; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(5) | NO | | 0 | | | name | varchar(25) | YES | | NULL | | | color | varchar(15) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) +-
  58. 58. Copy a table with structure mysql> select * from fruits2; +----+---------+--------+ | id | name | color | +----+---------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 7 | grape | black | | 8 | mango | yellow | | 6 | goa | green | | 9 | suppota | brown | | 10 | jack | green | +----+---------+--------+ 8 rows in set (0.00 sec) mysql> +.
  59. 59. Grouping Function mysql> select color,count(*) from fruits group by color; +--------+----------+ | color | count(*) | +--------+----------+ | black | 1 | | brown | 1 | | green | 2 | | red | 2 | | yellow | 2 | +--------+----------+ 5 rows in set (0.08 sec) /
  60. 60. Grouping Function mysql> select color,count(*) as cnt from fruits group by color order by cnt; +--------+-----+ | color | cnt | +--------+-----+ | black | 1 | | brown | 1 | | red | 2 | | yellow | 2 | | green | 2 | +--------+-----+ 5 rows in set (0.00 sec)
  61. 61. Grouping Function mysql> select color,count(*) as cnt from fruits group by color order by cnt desc; +--------+-----+ | color | cnt | +--------+-----+ | red | 2 | | yellow | 2 | | green | 2 | | black | 1 | | brown | 1 | +--------+-----+ 5 rows in set (0.00 sec) mysql> '
  62. 62. Altering Table mysql> alter table fruits add rate float(3,2) default 0.0; Query OK, 8 rows affected (0.28 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> select * from fruits; +----+---------+--------+------+ | id | name | color | rate | +----+---------+--------+------+ | 1 | apple | red | 0.00 | | 2 | cherry | red | 0.00 | | 3 | banana | yellow | 0.00 | | 7 | grape | black | 0.00 | | 8 | mango | yellow | 0.00 | | 6 | goa | green | 0.00 | | 9 | suppota | brown | 0.00 | | 10 | jack | green | 0.00 | +----+---------+--------+------+ 8 rows in set (0.00 sec) Mysql> )
  63. 63. Updating the Records mysql> update fruits set rate=5 where name='apple' or name='mango'; Query OK, 2 rows affected (0.05 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> update fruits set rate=0.5 where name in ('cherry','grape'); Query OK, 2 rows affected (0.06 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> update fruits set rate=2.25 where rate=0; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> *
  64. 64. Updating the Records mysql> select * from fruits; +----+---------+--------+------+ | id | name | color | rate | +----+---------+--------+------+ | 1 | apple | red | 5.00 | | 2 | cherry | red | 0.50 | | 3 | banana | yellow | 2.25 | | 7 | grape | black | 0.50 | | 8 | mango | yellow | 5.00 | | 6 | goa | green | 2.25 | | 9 | suppota | brown | 2.25 | | 10 | jack | green | 2.25 | +----+---------+--------+------+ 8 rows in set (0.00 sec) mysql> +
  65. 65. More than one table mysql> create table purchase( -> fruit varchar(25), qty int(3) default 0)engine=myisam; Query OK, 0 rows affected (0.11 sec) mysql> load data local infile 'c:/buy.txt' into table purchase fields terminated by ',' lines terminated by 'rn'; Query OK, 4 rows affected (0.05 sec) Records: 4 Deleted: 0 Skipped: 0 Warnings: 0 mysql> select * from purchase; +--------+------+ | fruit | qty | +--------+------+ | apple | 5 | | grape | 30 | | goa | 3 | | banana | 12 | +--------+------+ 4 rows in set (0.00 sec) mysql>
  66. 66. Combined Query mysql> select a.name,a.rate,b.qty,b.qty*a.rate as amount from fruits a, purchase b where a.name=b.fruit; +--------+------+------+--------+ | name | rate | qty | amount | +--------+------+------+--------+ | apple | 5.00 | 5 | 25.00 | | banana | 2.25 | 12 | 27.00 | | grape | 0.50 | 30 | 15.00 | | goa | 2.25 | 3 | 6.75 | +--------+------+------+--------+ 4 rows in set (0.00 sec) mysql> select sum(b.qty*a.rate) as amount from fruits a, purchase b where a.name=b.fruit; +--------+ | amount | +--------+ | 73.75 | +--------+ 1 row in set (0.05 sec) mysql> ,
  67. 67. Indexing mysql> create index index1 on fruits(rate,color); Query OK, 8 rows affected (0.14 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> show keys from fruits; +--------+------------+----------+--------------+-------------+----------- +----- --------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Card inality | Sub_part | Packed | Null | Index_type | Comment | Index_Comment | +--------+------------+----------+--------------+-------------+----------- +----- --------+----------+--------+------+------------+---------+---------------+ | fruits | 0 | PRIMARY | 1 | id | A | 8 | NULL | NULL | | BTREE | | | | fruits | 1 | index1 | 1 | rate | A | NULL | NULL | NULL | YES | BTREE | | | | fruits | 1 | index1 | 2 | color | A | NULL | NULL | NULL | YES | BTREE | | | +--------+------------+----------+--------------+-------------+----------- +----- --------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec) mysql> -
  68. 68. Stored - Functions mysql>select rate from lamp.fruits where fruit=’apple’; +------+ | rate | +------+ | 5.00 | +------+ 1 row in set (0.00 sec) mysql> delimiter |; mysql> create function GetFruitRate(item varchar(20)) -> returns float -> deterministic -> begin -> declare rt float; -> select rate into rt from lamp.fruits where name=item; -> return rt; -> end| Query OK, 0 rows affected (0.14 sec) mysql> select GetFruitRate('banana'); +------------------------+ | GetFruitRate('banana') | +------------------------+ | 2.25 | +------------------------+ 1 row in set (0.00 sec) .
  69. 69. Listing all the available stored functions mysql> show function statusG *************************** 1. row *************************** Db: lamp Name: GetFruitRate Type: FUNCTION Definer: root@localhost Modified: 2008-01-25 12:27:27 Created: 2008-01-25 12:27:27 Security_type: DEFINER Comment: character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: latin1_swedish_ci 1 row in set (0.00 sec) mysql> ,/
  70. 70. To view the code mysql> show create function GetFruitRateG *************************** 1. row *************************** Function: GetFruitRate sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `GetFruitRate`( item varchar(20)) RETURNS float DETERMINISTIC begin declare rt float; select rate into rt from lamp.fruits where name=item; return rt; end character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: latin1_swedish_ci 1 row in set (0.00 sec) mysql> ,
  71. 71. Procedures mysql> delimiter | mysql> create procedure ChangeFruitRate(item varchar(20), newrate float) -> begin -> declare cnt int; -> select count(*) into cnt from lamp.fruits where name=item; -> if cnt>0 then -> update lamp.fruits set rate=newrate where name=item; -> select name,rate from lamp.fruits where name=item; -> else -> select concat(item, ' not available in the fruits table') as Error; -> end if; -> end| Query OK, 0 rows affected (0.09 sec) ,'
  72. 72. Procedures mysql> delimiter ; mysql> call ChangeFruitRate('apple',6.50); +-------+------+ | name | rate | +-------+------+ | apple | 6.50 | +-------+------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.02 sec) mysql> select * from fruits; +----+---------+--------+------+ | id | name | color | rate | +----+---------+--------+------+ | 1 | apple | red | 6.50 | | 2 | cherry | red | 0.50 | | 3 | banana | yellow | 2.25 | | 7 | grape | black | 0.50 | | 8 | mango | yellow | 5.00 | | 6 | goa | green | 2.25 | | 9 | suppota | brown | 2.25 | | 10 | jack | green | 2.25 | +----+---------+--------+------+ 8 rows in set (0.00 sec) mysql> ,)
  73. 73. Procedures mysql> call ChangeFruitRate('orange',6.50); +------------------------------------------+ | Error | +------------------------------------------+ | orange not available in the fruits table | +------------------------------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.02 sec) mysql> ,*
  74. 74. Trigger mysql> create table log(time datetime, task varchar(200)engine=myisam; Query OK, 0 rows affected (0.02 sec) mysql> delimiter | mysql> create trigger LogRateChange after update on fruits -> for each row -> begin -> insert into log values(now(),concat(new.name,' rate changed from ', old.rate, ' to ', new.rate)); -> end; -> | Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> update fruits set rate='7.5' where name='apple'; Query OK, 1 row affected (0.00 sec) ,+
  75. 75. Trigger mysql> select * from fruits; +----+---------+--------+------+ | id | name | color | rate | +----+---------+--------+------+ | 1 | apple | red | 7.50 | | 2 | cherry | red | 0.50 | | 3 | banana | yellow | 2.25 | | 7 | grape | black | 0.50 | | 8 | mango | yellow | 5.00 | | 6 | goa | green | 4.50 | | 9 | suppota | brown | 2.25 | | 10 | jack | green | 2.25 | +----+---------+--------+------+ 8 rows in set (0.00 sec) mysql> select * from log; +---------------------+--------------------------------------+ | time | task | +---------------------+--------------------------------------+ | 2008-01-25 14:21:38 | apple rate changed from 6.50 to 7.50 | +---------------------+--------------------------------------+ 1 row in set (0.00 sec) mysql> ,
  76. 76. Views mysql> create view myfruits as select name, rate from fruits; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +----------------+ | Tables_in_lamp | +----------------+ | fruits | | fruits1 | | log | | myfruits | | purchase | +----------------+ 5 rows in set (0.00 sec) ,,
  77. 77. Views mysql> select * from myfruits; +---------+------+ | name | rate | +---------+------+ | apple | 7.50 | | cherry | 0.50 | | banana | 2.25 | | grape | 0.50 | | mango | 5.00 | | goa | 4.50 | | suppota | 2.25 | | jack | 2.25 | +---------+------+ 8 rows in set (0.00 sec) mysql> ,-
  78. 78. User Management Create a new user mysql> grant all privileges on lamp.* to 'myfriend'@'localhost' identified by ‘myfriend'; Query OK, 0 rows affected (0.09 sec) Login from another terminal as myfriend C:>mysql -u root -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 6.0.3-alpha-community MySQL Community Server (GPL) Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> use lamp Database changed mysql> update fruits set rate=5.0 where name='apple'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 Mysql> ,.
  79. 79. Revoke the update privilege from myfriend mysql> revoke update on lamp.* from 'myfriend'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> If myfriend try to update again mysql> update fruits set rate=5.0 where name='apple'; ERROR 1142 (42000): UPDATE command denied to user 'myfriend'@'localhost' for table 'fruits' mysql> -/
  80. 80. Deleting a record mysql> select * from fruits2; +----+---------+--------+ | id | name | color | +----+---------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 7 | grape | black | | 8 | mango | yellow | | 6 | goa | green | | 9 | suppota | brown | | 10 | jack | green | +----+---------+--------+ 8 rows in set (0.00 sec) mysql> delete from fruits2 where color='green'; Query OK, 2 rows affected (0.24 sec) -
  81. 81. Deleting a record mysql> select * from fruits2; +----+---------+--------+ | id | name | color | +----+---------+--------+ | 1 | apple | red | | 2 | cherry | red | | 3 | banana | yellow | | 7 | grape | black | | 8 | mango | yellow | | 9 | suppota | brown | +----+---------+--------+ 6 rows in set (0.00 sec) mysql> -'
  82. 82. Dropping a table mysql> truncate table fruits2; Query OK, 6 rows affected (0.28 sec) mysql> select * from fruits2; Empty set (0.00 sec) mysql> drop table fruits2; Query OK, 0 rows affected (0.16 sec) mysql> show tables; +----------------+ | Tables_in_lamp | +----------------+ | fruits | | fruits1 | | purchase | +----------------+ 3 rows in set (0.06 sec) -)
  83. 83. Dropping a database mysql> drop database lamp; Query OK, 0 rows affected (0.00 sec) mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> -*
  84. 84. Quit from mysql prompt mysql> exit Bye C:> -+
  85. 85. -

×