컴퓨터/APM

MySQL : 데이타베이스 실습

하늘치 2008. 1. 14. 14:43
반응형

MySQL

p.290
primary key
- 단일키
- 복합키

===================================================

mysql> create database bookorama;
Query OK, 1 row affected (0.20 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bookorama          |
| company            |
| mysql              |
| sqltest            |
| test               |
+--------------------+
6 rows in set (0.20 sec)

mysql> use bookorama;
Database changed

===================================================

mysql> create table customers
    -> (
    -> customerid int unsigned not null auto_increment primary key,
    -> name char(50) not null,
    -> address char(100) not null,
    -> city char(30) not null
    -> );
Query OK, 0 rows affected (0.91 sec)

mysql> desc customers;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| customerid | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name       | char(50)         | NO   |     |         |                |
| address    | char(100)        | NO   |     |         |                |
| city       | char(30)         | NO   |     |         |                |
+------------+------------------+------+-----+---------+----------------+
4 rows in set (0.13 sec)


===================================================


mysql> create table orders
    -> (
    -> orderid int unsigned not null auto_increment primary key,
    -> customerid int unsigned not null,
    -> amount float(6,2),
    -> date date not null
    -> );
Query OK, 0 rows affected (0.22 sec)

mysql> desc orders;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| orderid    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| customerid | int(10) unsigned | NO   |     |         |                |
| amount     | float(6,2)       | YES  |     | NULL    |                |
| date       | date             | NO   |     |         |                |
+------------+------------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)


===================================================


mysql> create table books
    -> (
    -> isbn char(13) not null primary key,
    -> author char(50),
    -> title char(100),
    -> price float(4,2)
    -> );
Query OK, 0 rows affected (0.22 sec)

mysql> desc books;
+--------+------------+------+-----+---------+-------+
| Field  | Type       | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| isbn   | char(13)   | NO   | PRI |         |       |
| author | char(50)   | YES  |     | NULL    |       |
| title  | char(100)  | YES  |     | NULL    |       |
| price  | float(4,2) | YES  |     | NULL    |       |
+--------+------------+------+-----+---------+-------+
4 rows in set (0.06 sec)


===================================================


mysql> create table order_items
    -> (
    -> orderid int unsigned not null,
    -> isbn char(13) not null,
    -> quantity tinyint unsigned,
    ->
    -> primary key (orderid, isbn)
    -> );
Query OK, 0 rows affected (0.14 sec)

mysql> desc order_items;
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| orderid  | int(10) unsigned    | NO   | PRI |         |       |
| isbn     | char(13)            | NO   | PRI |         |       |
| quantity | tinyint(3) unsigned | YES  |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
3 rows in set (0.02 sec)


===================================================


mysql> create table book_reviews
    -> (
    -> isbn char(13) not null primary key,
    -> review text
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> desc book_reviews;
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| isbn   | char(13) | NO   | PRI |         |       |
| review | text     | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
2 rows in set (0.05 sec)


===================================================


mysql> show tables;
+---------------------+
| Tables_in_bookorama |
+---------------------+
| book_reviews        |
| books               |
| customers           |
| order_items         |
| orders              |
+---------------------+
5 rows in set (0.00 sec)




===================================================


mysql> insert into customers values
    ->   (3, "Julie Smith", "25 Oak Street", "Airport West"),
    ->   (4, "Alan Wong", "1/47 Haines Avenue", "Box Hill"),
    ->   (5, "Michelle Arthur", "357 North Road", "Yarraville");
Query OK, 3 rows affected (0.13 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into orders values
    ->   (NULL, 5, 69.98, "2000-04-02"),
    ->   (NULL, 3, 49.99, "2000-04-15"),
    ->   (NULL, 4, 74.98, "2000-04-19"),
    ->   (NULL, 5, 24.99, "2000-05-01");
Query OK, 4 rows affected (0.09 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into books values
    ->   ("0-672-31697-8", "Michael Morgan", "Java 2 for Professional Developers", 34.99),

    ->   ("0-672-31745-1", "Thomas Down", "Installing Debian GNU/Linux", 24.99),
    ->   ("0-672-31509-2", "Pruitt, et al.", "Teach Yourself GIMP in 24 Hours", 24.99),
    ->   ("0-672-31769-9", "Thomas Schenk", "Caldera OpenLinux System Administration Unleashed", 49.99);
Query OK, 4 rows affected (0.11 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into order_items values
    ->   (1, "0-672-31697-8", 2),
    ->   (2, "0-672-31769-9", 1),
    ->   (3, "0-672-31769-9", 1),
    ->   (3, "0-672-31509-2", 1),
    ->   (4, "0-672-31745-1", 3);
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> insert into book_reviews values
    ->   ("0-672-31697-8", "Morgan's book is clearly written and goes well beyond
    ">                      most of the basic Java books out there.");
Query OK, 1 row affected (0.06 sec)

















====================================================================

mysql> select * from orders
    -> where customerid=5;
+---------+------------+--------+------------+
| orderid | customerid | amount | date       |
+---------+------------+--------+------------+
|       1 |          5 |  69.98 | 2000-04-02 |
|       4 |          5 |  24.99 | 2000-05-01 |
+---------+------------+--------+------------+
2 rows in set (0.05 sec)






mysql> select * from orders where customerid in(3,4);
+---------+------------+--------+------------+
| orderid | customerid | amount | date       |
+---------+------------+--------+------------+
|       2 |          3 |  49.99 | 2000-04-15 |
|       3 |          4 |  74.98 | 2000-04-19 |
+---------+------------+--------+------------+
2 rows in set (0.03 sec)


mysql> select * from orders where customerid not in(3,4);
+---------+------------+--------+------------+
| orderid | customerid | amount | date       |
+---------+------------+--------+------------+
|       1 |          5 |  69.98 | 2000-04-02 |
|       4 |          5 |  24.99 | 2000-05-01 |
+---------+------------+--------+------------+
2 rows in set (0.00 sec)


mysql> select * from orders where customerid=3 or customerid=4;
+---------+------------+--------+------------+
| orderid | customerid | amount | date       |
+---------+------------+--------+------------+
|       2 |          3 |  49.99 | 2000-04-15 |
|       3 |          4 |  74.98 | 2000-04-19 |
+---------+------------+--------+------------+
2 rows in set (0.05 sec)










mysql> select * from customers;
+------------+-----------------+--------------------+--------------+
| customerid | name            | address            | city         |
+------------+-----------------+--------------------+--------------+
|          3 | Julie Smith     | 25 Oak Street      | Airport West |
|          4 | Alan Wong       | 1/47 Haines Avenue | Box Hill     |
|          5 | Michelle Arthur | 357 North Road     | Yarraville   |
+------------+-----------------+--------------------+--------------+
3 rows in set (0.00 sec)

mysql> select * from orders;
+---------+------------+--------+------------+
| orderid | customerid | amount | date       |
+---------+------------+--------+------------+
|       1 |          5 |  69.98 | 2000-04-02 |
|       2 |          3 |  49.99 | 2000-04-15 |
|       3 |          4 |  74.98 | 2000-04-19 |
|       4 |          5 |  24.99 | 2000-05-01 |
+---------+------------+--------+------------+
4 rows in set (0.00 sec)

mysql>


mysql> select o.orderid, o.amount, o.date
    -> from customers c, orders o
    -> where c.name= 'Julie Smith'
    -> and c.customerid = o.customerid;
+---------+--------+------------+
| orderid | amount | date       |
+---------+--------+------------+
|       2 |  49.99 | 2000-04-15 |
+---------+--------+------------+
1 row in set (0.11 sec)











Question!


도시가 Box Hill인 데이터의 이름과 가격을 구하시오.
select c.name, o.amount
from customers c, orders o
where c.city='Box Hill' and c.customerid=o.customerid;


mysql> select c.name, o.amount
    -> from customers c, orders o
    -> where c.city='Box Hill' and c.customerid=o.customerid;
+-----------+--------+
| name      | amount |
+-----------+--------+
| Alan Wong |  74.98 |
+-----------+--------+
1 row in set (0.00 sec)






mysql> select c.name
    -> from customers c, orders o, order_items oi, books b
    -> where c.customerid = o.customerid
    -> and o.orderid = oi.orderid
    -> and oi.isbn = b.isbn
    -> and b.title like '%Java%';
+-----------------+
| name            |
+-----------------+
| Michelle Arthur |
+-----------------+
1 row in set (0.16 sec)











===================================================
Linux 문구가 들어간 책의 사람 이름과 도시명을 검색하시오.

mysql> select * from book_reviews;
+---------------+----------------------------------------------------------------------------------------
| isbn          | review
+---------------+----------------------------------------------------------------------------------------
| 0-672-31697-8 | Morgan's book is clearly written and goes well beyond
                     most of the basic Java books out there. |
+---------------+----------------------------------------------------------------------------------------
1 row in set (0.02 sec)

mysql> select * from books;
+---------------+----------------+---------------------------------------------------+-------+
| isbn          | author         | title                                             | price |
+---------------+----------------+---------------------------------------------------+-------+
| 0-672-31509-2 | Pruitt, et al. | Teach Yourself GIMP in 24 Hours                   | 24.99 |
| 0-672-31697-8 | Michael Morgan | Java 2 for Professional Developers                | 34.99 |
| 0-672-31745-1 | Thomas Down    | Installing Debian GNU/Linux                       | 24.99 |
| 0-672-31769-9 | Thomas Schenk  | Caldera OpenLinux System Administration Unleashed | 49.99 |
+---------------+----------------+---------------------------------------------------+-------+
4 rows in set (0.00 sec)

mysql> select * from order_items;
+---------+---------------+----------+
| orderid | isbn          | quantity |
+---------+---------------+----------+
|       1 | 0-672-31697-8 |        2 |
|       2 | 0-672-31769-9 |        1 |
|       3 | 0-672-31509-2 |        1 |
|       3 | 0-672-31769-9 |        1 |
|       4 | 0-672-31745-1 |        3 |
+---------+---------------+----------+
5 rows in set (0.00 sec)

mysql> select * from orders;
+---------+------------+--------+------------+
| orderid | customerid | amount | date       |
+---------+------------+--------+------------+
|       1 |          5 |  69.98 | 2000-04-02 |
|       2 |          3 |  49.99 | 2000-04-15 |
|       3 |          4 |  74.98 | 2000-04-19 |
|       4 |          5 |  24.99 | 2000-05-01 |
+---------+------------+--------+------------+
4 rows in set (0.00 sec)

mysql> select * from customers;
+------------+-----------------+--------------------+--------------+
| customerid | name            | address            | city         |
+------------+-----------------+--------------------+--------------+
|          3 | Julie Smith     | 25 Oak Street      | Airport West |
|          4 | Alan Wong       | 1/47 Haines Avenue | Box Hill     |
|          5 | Michelle Arthur | 357 North Road     | Yarraville   |
+------------+-----------------+--------------------+--------------+
3 rows in set (0.00 sec)





select c.name, c.city
from books b, order_items oi, orders o, customers c
where b.title like '%Linux%'
and b.isbn=oi.isbn
and oi.orderid=o.orderid
and o.customerid=c.customerid;









==============================================================================
Question
- Linux 문구가 들어간 책의 이름과 도시명을 검색하시오.

mysql> select b.title, c.city
    -> from books b, order_items oi, orders o, customers c
    -> where b.title like '%Linux%'
    -> and b.isbn=oi.isbn
    -> and oi.orderid=o.orderid
    -> and o.customerid=c.customerid;
+---------------------------------------------------+--------------+
| title                                             | city         |
+---------------------------------------------------+--------------+
| Caldera OpenLinux System Administration Unleashed | Airport West |
| Caldera OpenLinux System Administration Unleashed | Box Hill     |
| Installing Debian GNU/Linux                       | Yarraville   |
+---------------------------------------------------+--------------+
3 rows in set (0.00 sec)


==============================================================================
Question
- Linux 문구가 들어간 책의 고객 이름과 도시명을 검색하시오.

mysql> select c.name, c.city
    -> from books b, order_items oi, orders o, customers c
    -> where b.title like '%Linux%'
    -> and b.isbn=oi.isbn
    -> and oi.orderid=o.orderid
    -> and o.customerid=c.customerid;
+-----------------+--------------+
| name            | city         |
+-----------------+--------------+
| Julie Smith     | Airport West |
| Alan Wong       | Box Hill     |
| Michelle Arthur | Yarraville   |
+-----------------+--------------+
3 rows in set (0.00 sec)








반응형