If you know SQL, then MySQL is easy to use. This exercise demonstrates how to easy it is to work with a MySQL database.
mysql>GRANT ALL ON pet.* TO 'mysql_name'@'client_host';
mysql>CREATE DATABASE pet;
mysql>USE pet;
mysql>CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
->species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
mysql>SHOW TABLES;
mysql>DESCRIBE pet;
vi to create a text file called pet.txt containing one record per line, with values separated by tabs.\N to represent a NULL value.| name | owner | species | sex | birth | death |
|---|---|---|---|---|---|
| Fluffy | Harold | cat | f | 1993-02-04 | |
| Claws | Gwen | cat | m | 1997-03-17 | |
| Buffy | Harold | dog | f | 1989-05-13 | |
| Fido | Ben | dog | m | 1990-08-27 | |
| Rover | Dave | dog | m | 1989-08-31 | 2005-07-21 |
| Chirpy | Gwen | bird | f | 1998-09-11 | |
| Whistler | Gwen | bird | 1999-12-19 | ||
| Peter | Ben | rabbit | m | 1997-04-21 |
mysql>LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;
mysql>INSERT INTO pet VALUES
->('Puffball','Bob','hamster','f','2001-03-31',NULL);
mysql>SELECT * FROM pet;
mysql>SELECT * FROM pet WHERE name = 'Rover';
mysql>SELECT * FROM pet WHERE birth <= '1998-1-1';
mysql>SELECT * FROM pet WHERE species = 'cat' OR species = 'dog';
mysql>SELECT name, owner FROM pet WHERE species = 'cat' OR species = 'dog';
mysql>SELECT DISTINCT owner FROM pet;
mysql>SELECT name, birth, CURDATE(),
-> (YEAR(CURDATE())-YEAR(birth)) - (RIGHT(CURDATE(),5)<RIGHT(birth,5))
-> AS age FROM pet ORDER BY name;
IS NOT NULL in listing pet names alphabetically and their ages.mysql>SELECT name, birth, death,
->(YEAR(death)-YEAR(birth)) - (RIGHT(death,5)<RIGHT(birth,5))
->AS age FROM pet WHERE death IS NOT NULL ORDER BY age;
mysql>SELECT * FROM pet WHERE name LIKE 'f%';
mysql>SELECT * FROM pet WHERE name LIKE '%fy';
mysql>SELECT sex, COUNT(*) FROM pet GROUP BY species;
vi to create a text file called event.txt containing one record per line, with values separated by tabs.\N to represent a NULL value.| name | date | type | remark |
|---|---|---|---|
| Fluffy | 1995-05-15 | litter | 4 kittens, 3 female, 1 male |
| Buffy | 1993-06-23 | litter | 5 puppies, 2 female, 3 male |
| Buffy | 1994-06-19 | litter | 3 puppies, 3 female |
| Chirpy | 1999-03-21 | vet | needed beak straightened |
| Fido | 2005-08-03 | vet | broken rib |
| Rover | 1991-10-12 | kennel | |
| Fido | 1991-10-12 | kennel | |
| Fido | 1998-08-27 | birthday | Gave him a new chew toy |
| Claws | 2001-03-17 | birthday | Gave him a new flea collar |
| Whistler | 2000-12-09 | birthday | First birthday |
mysql>CREATE TABLE event (name VARCHAR(20), date DATE,
->type VARCHAR(15), remark VARCHAR(255));
mysql>LOAD DATA LOCAL INFILE '/path/event.txt' INTO TABLE event;
INNER JOININNER JOIN to build a complex query.
mysql>SELECT pet.name,
->(YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5))
->AS age, remark FROM pet INNER JOIN event
->ON pet.name = event.name WHERE event.type = 'litter';
QUIT to exit out of the server
mysql>QUIT
Congratulations! You have successfully created and used a MySQL database.