A Brief Mysql Tutorial: Cse 134A: Web Service Design and Programming Fall 2001 9/28/2001
A Brief Mysql Tutorial: Cse 134A: Web Service Design and Programming Fall 2001 9/28/2001
CSE 134A: Web Service Design and Programming Fall 2001 9/28/2001
1) Creating a database
mysql> CREATE database 134a; Query OK, 1 row affected (0.00 sec)
2) Deleting a database
mysql> DROP database 134a; Query OK, 0 rows affected (0.00 sec)
Creating a Table
3) After we have created the database we use the USE statement to change the current database; mysql> USE 134a; Database changed
4) Creating a table in the database is achieved with the CREATE table statement mysql> CREATE TABLE president ( -> -> -> -> -> -> -> ); Query OK, 0 rows affected (0.00 sec) last_name varchar(15) not null, first_name varchar(15) not null, state varchar(2) not null, city varchar(20) not null, birth date not null default '0000-00-00',
mysql> DESCRIBE president; +------------+-------------+------+-----+------------+-------+---------------------------------+ | Field | Type | Null | Key | Default | Extra | Privileges |
| 0000-00-00 | | NULL |
mysql> INSERT INTO president values ('Washington', 'George', 'VA', 'Westmoreland County', '17320212', '17991214'); Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM president; +------------+------------+-------+---------------------+------------+------------+ | last_name | first_name | state | city | birth | death |
11) Deleting selected rows from a table using the DELETE command
mysql> DELETE FROM president WHERE first_name="George"; Query OK, 1 row affected (0.00 sec)
12) To modify or update entries in the table use the UPDATE command
mysql> UPDATE president SET state="CA" WHERE first_name="George"; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Assuming we have a file named "president_db" in the current directory, with multiple INSERT commands in it, we can use the LOAD DATA command to insert the data into the table president.
mysql> LOAD DATA LOCAL INFILE 'president_db' INTO TABLE president; Query OK, 45 rows affected (0.01 sec) Records: 45 Deleted: 0 Skipped: 0 Warnings: 0
Note, that any ascii file that contains a valid sequence of MySql commands on separate lines can be read in from the command line as:
More on SELECT
SELECT what to select FROM table(s) WHERE condition that the data must satisfy;
Comparison operators are: < ; <= ; = ; != or <> ; >= ; > Logical operators are: AND ; OR ; NOT Comparison operator for special value NULL: IS
mysql> SELECT * FROM president WHERE state="NY"; +-----------+-------------+-------+---------------+------------+------------+ | last_name | first_name | state | city | birth | death |
| Cayuga County | 1800-01-07 | 1874-03-08 | | New York | Hyde Park | 1858-10-27 | 1919-01-06 | | 1882-01-30 | 1945-04-12 |
| Roosevelt | Theodore
| Roosevelt | Franklin D. | NY
mysql> SELECT last_name, first_name FROM president WHERE state="NY"; +-----------+-------------+ | last_name | first_name |
| Roosevelt | Theodore
mysql> SELECT * FROM president WHERE death = NULL; Empty set (0.00 sec)
mysql> SELECT last_name, birth FROM president WHERE death is NULL; +-----------+------------+ | last_name | birth |
+-----------+------------+ | Ford | Carter | Reagan | Bush | Clinton | Bush | 1913-07-14 | | 1924-10-01 | | 1911-02-06 | | 1924-06-12 | | 1946-08-19 | | 1946-07-06 |
mysql> SELECT last_name, birth FROM president WHERE birth<"1800-01-01"; +------------+------------+ | last_name | birth |
+------------+------------+ | Washington | 1732-02-12 | | Adams | Jefferson | Madison | Monroe | Adams | Jackson | Van Buren | Harrison | Tyler | Polk | Taylor | Buchanan | 1735-10-30 | | 1735-04-13 | | 1751-03-16 | | 1758-04-28 | | 1767-07-11 | | 1767-03-15 | | 1782-12-05 | | 1773-02-09 | | 1790-03-29 | | 1795-11-02 | | 1784-11-24 | | 1791-04-23 |
18) The following command will select the president who was born first
mysql> SELECT last_name, birth from president ORDER BY birth ASC LIMIT 1; +------------+------------+ | last_name | birth |
mysql> SELECT state, count(*) AS times FROM president GROUP BY state -> ORDER BY times DESC LIMIT 5; +-------+-------+ | state | times | +-------+-------+ | VA | OH | MA | NY | NC | | | | | 8 | 7 | 4 | 4 | 2 |
20) The following query will select presidents who have been born
mysql> SELECT last_name, birth, death, FLOOR((TO_DAYS(death) - TO_DAYS(birth))/365) AS age -> FROM president -> WHERE death is not NULL ORDER BY age DESC LIMIT 10; +------------+------------+------------+------+ | last_name | birth | death | age |
+------------+------------+------------+------+ | Jefferson | Adams | Hoover | Truman | Madison | Nixon | Adams | Van Buren | Jackson | 1735-04-13 | 1826-07-04 | | 1735-10-30 | 1826-07-04 | | 1874-08-10 | 1964-10-20 | | 1884-05-08 | 1972-12-26 | | 1751-03-16 | 1836-06-28 | | 1913-01-09 | 1994-04-22 | | 1767-07-11 | 1848-02-23 | | 1782-12-05 | 1862-07-24 | | 1767-03-15 | 1845-06-08 | 91 | 90 | 90 | 88 | 85 | 81 | 80 | 79 | 78 | 78 |
+------------+------------+------------+------+
22) Often it is useful to separate data in conceptually distinct groups and store them in separate tables. Assuming we have a table that contains students' personal information, and we have another table that contains test scores of students. We can create a common field in each table, say "ssn" and work with the two tables together as follows:
SELECT last_name, address, test_date, score FROM test, student WHERE test.ssn = student.ssn;
http://www.mysql.com/documentation/index.html http://www.mysql.com/documentation/mysql/bychapter/manual_Introduction.html#General-SQL