3. 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.
)
4. 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
*
6. 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.
7. +--------------------+
| Database |
+--------------------+
| information_schema |
| dbone |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
Database changed
,
8. +----+---------+--------+
| 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)
-
9. +-------+-------------+------+-----+---------+----------------+
| 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)
.
10. +---------+--------+
| 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)
/
11. +----+--------+-------+
| 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)
46. #-. *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
*
47. Creating and Using Databases
mysql> create database LAMP;
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> use LAMP;
Database changed
mysql>
*,
48. 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>
*-
49. 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>
*.
50. 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)
+/
51. 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>
+
52. 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)
+'
53. 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>
+)
54. 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>
+*
55. 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)
++
56. 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)
+
57. 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)
+,
58. 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)
+-
59. 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>
+.
60. 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)
/
61. 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)
62. 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>
'
63. 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>
)
64. 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>
*
65. 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>
+
66. 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>
67. 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>
,
69. 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)
.
70. 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>
,/
71. 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>
,
72. 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)
,'
73. 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>
,)
79. 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>
,.
80. 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>
-/
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 |
| 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)
-
82. 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>
-'
83. 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)
-)
84. 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>
-*