• Like
Mysql簡易教學
Upcoming SlideShare
Loading in...5
×
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
398
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
2
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • http://stackoverflow.com/questions/2023476/which-is-faster-char1-or-tinyint1-why
  • 雖然用的是 primary key 可是只有部分用到
  • http://dev.mysql.com/doc/refman/5.0/en/internal-temporary-tables.html

Transcript

  • 1. 新生訓練8/20 - MySQL 講者 :黃健瑋 高速網路實驗室 High Speed Network Group Lab
  • 2. Database & table高速網路實驗室High Speed Network Group Lab 2
  • 3. Variable  int  4 byte  smallint  2 byte  tinyint  1 byte  bigint  8 byte高速網路實驗室High Speed Network Group Lab 3
  • 4. Variable  char  固定長度  varchar  儲存內容長度可變動  長度還是不能亂給! (temporary table)  盡量不要超過 170  ‘a’ 和’我’在 char(varchar) 裡都只佔一格  text  長度較長的,例如文章內容  資料不存在 table 裡高速網路實驗室High Speed Network Group Lab 4
  • 5. Variable  假如要表達’是’和’否’,該用 ??  bool or tinyint or char(1)?高速網路實驗室High Speed Network Group Lab 5
  • 6. Variable  enum(‘y’,’n’) 最快 !!  Speed: enum(‘y’,’n’) > char(1) > bool = tinyint(1)高速網路實驗室High Speed Network Group Lab 6
  • 7. Variable  datetime  年月日時秒  date  幾月幾號請用這個  timestamp  當下時間  錯誤例子  選課結束日期用 datetime ,且只輸入年月日  now() 和 datetime 比較會有錯高速網路實驗室High Speed Network Group Lab 7
  • 8. Query  查詢 SELECT  新增 INSERT INTO  刪除 DELETE FROM  修改 UPDATE SET高速網路實驗室High Speed Network Group Lab 8
  • 9. Query  查詢 SELECT  SELECT 欄位 1, 欄位 2 FROM 表 WHERE  SELECT * FROM 表 WHERE  欄位 = ‘ 資料’ AND 欄位 = ‘ 資料’  盡量只取自己要的欄位高速網路實驗室High Speed Network Group Lab 9
  • 10. Query  錯誤例子 SELECT b FROM table WHERE a=‘a’ …… SELECT c FROM table WHERE a=‘a’ …… SELECT d FROM table WHERE a=‘a’  修正 SELECT b, c, d FROM table WHERE a=‘a’高速網路實驗室High Speed Network Group Lab 10
  • 11. Query  course  name, propery  teacher_course  course_name, teacher  取出某個老師的課程 ?  使用 join(inner join) !  SELECT C2.course_name FROM course AS C1, teacher_course AS C2 WHERE C2.teacher = ‘tom’ AND C1.name = C2.course_name高速網路實驗室High Speed Network Group Lab 11
  • 12. Query  如果要從課程表中取出多個不同性質的課程名稱 ?  SELECT name FROM course WHERE property = ‘a’ OR property = ‘b’ OR property = ‘c’ ……  ?高速網路實驗室High Speed Network Group Lab 12
  • 13. Query  較好的寫法  SELECT name FROM course WHERE property IN (‘a’, ‘b’, ‘c’, ……)高速網路實驗室High Speed Network Group Lab 13
  • 14. Query  如果要取得課程表中修課人數最多的課名 ?  如果只是取出課程表中最多的修課人數 ?  用 MAX() 函式  SELECT MAX(number) FROM course高速網路實驗室High Speed Network Group Lab 14
  • 15. Query  較好的寫法  SELECT name FROM course WHERE number = (SELECT MAX(number) FROM course) OR  SELECT name FROM course AS C1, (SELECT MAX(number) AS max_number FROM course) AS C2 WHERE C1.number = C2.max_number  錯誤寫法  SELECT name FROM course WHERE number IN (SELECT MAX(number) FROM course)高速網路實驗室High Speed Network Group Lab 15
  • 16. Query  怎麼取課程表內各個性質裡最多的人數? SELECT MAX(number) WHERE property = ‘a’ SELECT MAX(number) WHERE property = ‘b’ SELECT MAX(number) WHERE property = ‘c’ ……高速網路實驗室High Speed Network Group Lab 16
  • 17. Query  較好的寫法  SELECT MAX(number) GROUP BY property高速網路實驗室High Speed Network Group Lab 17
  • 18.  Group by & distinct高速網路實驗室High Speed Network Group Lab 18
  • 19. Query  新增 INSERT INTO  INSERT INTO 資料表 ( 欄位 1, 欄位 2, 欄位 3) VALUES (‘1’, ‘2’, ‘3’)  INSERT INTO 資料表 ( 欄位 1, 欄位 2, 欄位 3) VALUES (‘1’, ‘2’, ‘3’), (‘4’, ‘5’, ‘6’)高速網路實驗室High Speed Network Group Lab 19
  • 20. Query  刪除 DELETE FROM  DELETE FROM 資料表  所有資料列都刪掉,資料表還在  DELETE FROM 資料表 WHERE 欄位 = ‘ 資料’  符合條件的資料列  可以先用 SELECT 看會刪到什麼資料高速網路實驗室High Speed Network Group Lab 20
  • 21. Query  刪除性質是’ a’ 的課程  DELETE FROM course WHERE property = ‘a’  檢查  SELECT * FROM course WHERE property = ‘a’高速網路實驗室High Speed Network Group Lab 21
  • 22. Query  修改 UPDATE SET  UPDATE 資料表 SET 欄位 1 = ‘ 資料’ , 欄位 2 = ‘ 資料’ , 欄位 3 = ‘ 資料’ WHERE 欄位 = ‘ 資料’高速網路實驗室High Speed Network Group Lab 22
  • 23. Index  索引種類  Primary key: 唯一,不可為 NULL 的索引  Unique: 唯一,可為 NULL 的索引  Index: 一般索引  索引對照的問題  Ex: primay key(name, property, online, number)  SELECT * FROM course WHERE name = ‘a’ AND online = ‘y’ AND number = ‘5’高速網路實驗室High Speed Network Group Lab 23
  • 24. EXPLAIN  EXPLAIN  觀察 query 下的好不好的指令  Ex: EXPLAIN SELECT * FROM course WHERE name = ‘a’;高速網路實驗室High Speed Network Group Lab 24
  • 25. EXPLAIN  重點  Rows  掃了多少行  Type  const > eq_ref > ref > range > ALL (Performance)  Key  用到的索引是哪個高速網路實驗室High Speed Network Group Lab 25
  • 26. EXPLAIN  範例 ( 行數不滿意、 key 沒完全對到 )高速網路實驗室High Speed Network Group Lab 26
  • 27.  範例高速網路實驗室High Speed Network Group Lab 27
  • 28. Temporary Tables  When create?  ORDER BY + GROUP BY  DISTINCT combined with ORDER BY  When in disk?  table include ‘BLOB’ or ‘TEXT’  any column in a GROUP BY or DISTINCT clause larger than 512 bytes高速網路實驗室High Speed Network Group Lab 28
  • 29. DB 效能  MySQL 參數設定  參數值並不是越大越好  一些參數值該如何設定 ?  參考 16 上的 wiki  可用的工具  mysqltuner.pl  Tuning-primer.sh高速網路實驗室High Speed Network Group Lab 29