MySQL 4.0 講義

補充教材 - 完整個案

某出版社有以下的資料要放入資料庫中

Author_ID Author_Name Author_TEL Product_ID Product_Name Product_Type Product_Price Publish_Year
A01 Tom 29011111 P008 PHP 5 Textbook 480 2007
A01 Tom 29011111 P009 MySQL 5 Textbook 520 2008
A02 Helen 29022222 P003 Harry Potter 1 Novel 650 2001
A02 Helen 29022222 P004 Harry Potter 2 Novel 660 2004
A02 Helen 29022222 P007 Harry Potter 3 Novel 670 2007
A03 Jerry 29033333 P001 PCHome Magazine 300 2000
A03 Jerry 29033333 P005 Green Giant Novel 450 2005
A04 Alice 29044444 P002 PCDIY Magazine 280 2001
A04 Alice 29044444 P006 Visual Basic Textbook 530 2006

Q1. 請依第一正規化來拆解以上表格

Ans: 拆解成2個表格

Author Table

Author_ID Author_Name Author_TEL
A01 Tom 29011111
A02 Helen 29022222
A03 Jerry 29033333
A04 Alice 29044444

Product Table

Product_ID Product_Name Product_Type Author_ID Product_Price Publish_Year
P008 PHP 5 Textbook A01 480 2007
P009 MySQL 5 Textbook A01 520 2008
P003 Harry Potter 1 Novel A02 650 2001
P004 Harry Potter 2 Novel A02 660 2004
P007 Harry Potter 3 Novel A02 670 2007
P001 PCHome Magazine A03 300 2000
P005 Green Giant Novel A03 450 2005
P002 PCDIY Magazine A04 280 2001
P006 Visual Basic Textbook A04 530 2006

Q2. 建立資料庫 s00_db

Ans:

mysql -u s00 -p

mysql>create database s00_db;

Q3. 使用資料庫 s00_db

Ans:

use s00_db;

Q4. 建立 上述的Author Table

Ans:

create table author (
Author_ID char(5) not null,
Author_Name char(20) not null,
Author_TEL char(10) not null,
primary key(Author_ID)
);

Q5. 顯示 Author Table的所有欄位

Ans:

show columns from author;

Q6. 建立 上述的Product Table

Ans:

create table product (
Product_ID char(6) not null,
Product_Name char(30) not null,
Product_Type char(20) not null,
Author_ID char(5) not null,
Product_Price int not null,
Publish_Year int not null,
primary key(Product_ID)
);

Q7. 顯示 Product Table的所有欄位

Ans:

show columns from product;

Q8. 新增 上述 Author Table的4筆記錄

Ans:

insert into author(Author_ID,Author_Name,Author_TEL)
values ('A01','Tom','29011111'),
('A02','Helen','29022222'),
('A03','Jerry','29033333'),
('A04','Alice','29044444');

Q9. 顯示 Author Table的所有欄位的資料

Ans:

select * from author;

Q10. 新增 上述 Product Table的4筆記錄

Ans:

insert into product(Product_ID,Product_Name,Product_Type,Author_ID,Product_Price,Publish_Year)
values('P008','PHP 5','Textbook','A01','480','2007'),
('P009','MySQL 5','Textbook','A01','520','2008'),
('P003','Harry Potter 1','Novel','A02','650','2001'),
('P004','Harry Potter 2','Novel','A02','660','2004'),
('P007','Harry Potter 3','Novel','A02','670','2007'),
('P001','PCHome','Magazine','A03','300','2000'),
('P005','Green Giant','Novel','A03','450','2005'),
('P002','PCDIY','Magazine','A04','280','2001'),
('P006','Visual Basic','Textbook','A04','530','2006');

Q11. 顯示 Product Table的所有欄位的資料

Ans:

select * from product;

Q12. 顯示 單價小於500元的產品名稱和單價

Ans:

select Product_Name,Product_Price
from product
where Product_Price < 500;

Q13. 顯示 2005年以後出版的產品的所有資料

Ans:

select *
from product
where Publish_Year > 2005;

Q14. 顯示 小說類的產品的所有資料

Ans:

select *
from product
where Product_Type='Novel';

Q15. 顯示 Product Table的所有欄位的資料,依產品編號由小到大排序

Ans:

select *
from product
order by product_ID ASC;

Q16. 顯示 Product Table的所有欄位的資料,依產品單價由大到小排序

Ans:

select *
from product
order by product_price DESC;

Q17. 顯示 該出版社原有的資料內容

Ans:

select *
from author,product
where author.Author_ID=product.Author_ID;

Q18. 顯示 有寫過教科書的作者名字

Ans:

select Author_name
from author,product
where author.Author_ID=product.Author_ID and Product_Type='Textbook';

Q19. 顯示 有寫過教科書的作者名字(去除重覆出現的名字)

Ans:

select distinct Author_name
from author,product
where author.Author_ID=product.Author_ID and Product_Type='Textbook';