ITãšã³ãžãã¢ã¯SQLã¢ã³ããã¿ãŒã³ãèªãã¹ãïŒ è»œããŸãšã
ããã«ã¡ã¯@s_tsukaã§ããä»åã¯çããDBãã¿ã§ãã
SQLã¢ã³ããã¿ãŒã³ãšããæ¬ããããŸããããããªããªãè¯ãæ¬ãããèªãã§ã¿ãŸããã
[tmkm-amazon]4873115892[/tmkm-amazon]
çµè«ãšããŠã¯ããªãå匷ã«ãªããŸãããDBãSQLãæ±ã人ã¯çµ¶å¯Ÿã«ïŒåºŠã¯èªãã æ¹ãè¯ãã§ããããæè§ãªã®ã§è»œããŸãšããŠãããããšæããŸãã
æŠèŠ
ã¢ã³ããã¿ãŒã³ã®æ¬ãªã®ã§ããã¿ãŒã³ãåæãããŠããŸãããã ãã®ãã¿ãŒã³ã¯è¯ããã¿ãŒã³ã§ã¯ãªãæªããã¿ãŒã³ã§ãã
ããããªããªã圹ã«ç«ã£ãŠãèªãã§ãæäžäœåºŠããããŒãã®èšèšãã£ãïŒãããã¯èããïŒããšããwwwwwwããšãªããŸãããå¿è«è§£æ±ºçä»ãã§è§£èª¬ãããŠãããããããã«ãªããŸãã
以äžãã¢ã³ããã¿ãŒã³ãšè§£æ±ºçã軜ããŸãšããŠãããŸããã»ãšãã©åå¿é²çšãªã®ã§ã詳现ã¯æ¬ãè²·ã£ãŠèªã¿ãŸãããã
ã¢ã³ããã¿ãŒã³
以äžã«å¿ããªãããã«ã¢ã³ããã¿ãŒã³ããŸãšããŠãããŸãã
ã¢ã³ããã¿ãŒã³ãªã®ã§ãã¿ãªããç䌌ãã¡ããã¡ã§ããïŒïŒçµ¶å¯Ÿã§ã¯ãªãïŒ
1. ãžã§ã€ãŠã©ãŒã¯ïŒä¿¡å·ç¡èŠïŒ
ã«ã³ãåºåãã®å€ãå ¥ãã¡ãããã€ã§ãã
BOOK_ID | NAME | TAGS |
1 | Spring MVC | Java,Spring,SpringMVC |
2 | ã¯ãããŠã®Scala | Scala,å ¥é |
ããããããŒãã«ããã£ããšãã«TAGSã®éšåã«ã«ã³ãåºåãã§å€ãå ¥ããŠããã®ãNGã§ãã亀差ããŒãã«ãäœã£ãŠè§£æ±ºããŸãããã
2. ãã€ãŒãããªãŒïŒçŽ æŽãªæšïŒ
éå±€æ§é ãè¡šçŸãã¡ãããã€ã§ãã
COMMENT_ID | COMMENT | PARENT_ID |
1 | è¯ãèšäºã§ããïŒ | NULL |
2 | è¯ãèšäºã£ãŠãããã ã®ãŸãšãã§ããïŒ | 1 |
3 | ããã圹ã«ç«ã€ããããªãããªïŒ | 2 |
PARENT_IDã«ãã£ãŠåŒçšãè¡šçŸããŠããŸãããã¹ã¬ããã£ã³ã°ãšèšãã°è¯ãã®ããªïŒããããããŒãã«ã¯ç¢ºãã«ããŒã¿ã¹ãã¢ãšããŠæ£ããã§ãããSQL䜿ãã·ãŒã³ãèãããšããªãé¢åã§ãããªã®ã§ãéå ããŒãã«ã䜿ãç代æ¿æ¡ãèããŸãããã
3. IDãªã¯ã¯ã€ã¢ãïŒãšããããIDïŒ
ãªãã§ãããã§ãIDãã€ããããAUTO INCREMENTã ãããããååãâIDâã ã£ããããã¢ã¬ã§ããïŒAUTO INCREMENTãæªããšãã§ã¯ãªãã§ãïŒ
ID | ARTICLE_ID | TAG_ID |
10001 | 2941 | 10 |
10002 | 2941 | 23 |
10003 | 2942 | 8 |
ããã¯äº€å·®ããŒãã«ãšåŒã°ãããã®ã§ãARTICLEãšTAGãã€ãŸãèšäºãšïŒè€æ°ã®ïŒã¿ã°ãçµã³ã€ããããŒãã«ã§ããARTICLE_IDãšTAG_IDã®è€åäž»ããŒã§è¯ãã®ã«ãIDãšããäœèšãªäž»ããŒåãäœã£ãŠããŸãããããNGã§ããåé·ã ã£ãããéè€ã¬ã³ãŒãã§ãã¡ãã£ãããâIDâãšããã®ã埮åŠã§JOINæžããšãã«IDåã ããã ãšUSINGã䜿ããªãã£ãããèŠèªãèŸãã£ããã
äž»ããŒã¯ã¡ãããšèããŸãããã
4. ããŒã¬ã¹ãšã³ããªïŒå€éšããŒå«ãïŒ
å€éšããŒä»ããªã人å€ãããã£ãŠããã¢ã¬ã§ãã
å人çã«ã¯å€éšããŒã¯ãã£ã¡ãä»ããã掟ã§ããããŒã¿æ§é ãšããŒã¿ã¯ãªãã¹ãïŒå¶çŽã§ïŒå®ããã掟ã
å€éšããŒãããªã掟ãšããã«ãããšãã¯ãã®ç« ãåŒçšããŸãããw
5. EAVïŒãšã³ãã£ãã£ã»ã¢ããªãã¥ãŒãã»ããªã¥ãŒïŒ
æ®éDBã®ããŒãã«ãäœããšãã«ãäœãæ ŒçŽãããããèããŠãåããäœãããã§ãããæè»æ§ãéèŠããŠãããè¡ã§ããããšããã¢ã¬ã§ãã
FILE_ID | ATTRIBUTE_NAME | ATTRIBUTE_VALUE |
1 | extension | png |
1 | size | 3441238 |
1 | created_by | s_tsuka |
1 | created_at | 2015-03-21 20:57:00 |
1 | locked | false |
æ³åã§ããããšæããŸãããäžèšã®åããŒã¿ã¯åãããªããŠè¡ãªã®ã§ãããªã䜿ãåæãæªãã§ããå¶çŽã䜿ããªãã£ãããããããæ°å€ãæååãçåœå€ãæ··ãããã§ãããæ°å€ã¯å®ã¯â3441238âãšããããã«æååã§è¡šçŸããªãã¡ããããªãã£ããã»ã»ã»ããããŠå€åãããã¢ããªåŽã§äœ¿ããšããæ³£ããèŠãŸããã¢ããªåŽã§ã¯é åãšããŠäœ¿ãããã®ã§ã¯ãªãäœããã®entityãšããŠäœ¿ãããã¯ãã§å€æãå¿ èŠãšããã»ã»ã»
解決æ¹æ³ã¯ç¶æ¿ã£ãœãããšãããããããã§ããããã®èŸºãã®çµç·¯ã¯ã¡ãã£ãšäžæã説æã§ããªãã®ã§æ¬ãèªã¿ãŸãããã
6. ããªã¢ãŒãã£ãã¯é¢é£
ããŒã¿ã«ãã£ãŠèŠã芪ããŒãã«ãå€ãããã€ã§ãã
COMMENT_ID | TYPE | COMMENT | PARENT_ID |
1 | blog | è¯ãblogã§ããïŒ | 101 |
2 | article | ãã®èšäºã¯å匷ã«ãªã£ãïŒ | 4210 |
3 | article | ãã£ã»ãŒ | 4211 |
4 | blog | èšäºåã£ãŠããªãŒ | 101 |
BLOGããŒãã«ãšARTICLEããŒãã«ããã£ããšããŠãã·ã¹ãã ã§ã¯ãã®ã©ã¡ãã«å¯ŸããŠãã³ã¡ã³ããã§ãããšããŸãããããŠãPARENT_IDãæãããŒãã«ã¯ããŒã¿ã«ãã£ãŠç°ãªããŸã(BLOGãARTICLEã)ã
äžèšã®ã±ãŒã¹ã ãšããªã¢ãŒãã£ãã¯ãªæ§é ã§ã¯ãªãã®ã§ã埮åŠã§ããã»ã»ã»ããããã±ãŒã¹ã¯NGã§ã解決çãšããŠå ±éã®èŠªããŒãã«ãšããææ¡ãããŠããŸãããŸããšã«ãã詳现ã¯æ¬ãèªã¿ãŸãããã
7. ãã«ãã«ã©ã ã¢ããªãã¥ãŒãïŒè€æ°åå±æ§ïŒ
ãžã§ã€ãŠã©ãŒã¯ãšåãã ãã©åãè€æ°äœã£ããã€ã§ããæ£èŠåãããŠããªãããŒãã«ã®ããšã§ãã
ARTICLE_ID | TAG1 | TAG2 | TAG3 | TAG4 |
1 | Scala | å ¥é | NULL | NULL |
2 | å ¥é | Java | NULL | Java8 |
ããããã ãã§Bad smellããããŸããããæ€çŽ¢ãšãèãããšé ãçãã§ããéè€èæ ®ããæŽæ°ãåé€ãšããåãããã€ãã§ããã
åŸå±ããŒãã«äœã£ãŠå¯Ÿå¿ããŸãããã
8. ã¡ã¿ããŒã¿ããªãã«ïŒã¡ã¿ããŒã¿å€§å¢æ®ïŒ
å šãåãããŒãã«ãªã®ã«å¹Žãæã§åãããŠããããåããããªåãé£ç¶ããŠãããã€ã§ãã
CREATE TABLE LOG_2013 ( ... );
CREATE TABLE LOG_2014 ( ... );
CREATE TABLE LOG_2015 ( ... );
ããŒã¿ãå€ãããããåããããšããã±ãŒã¹ãå€ããšæããŸããå€ãããŒã¿ã¯äœ¿ããªãããããšãã£ãŠããŒãã«ïŒã€ã«ãããšã¯ãšãªãé ããªãããã¿ãããªã
äžèšã®ãããªã±ãŒã¹ãªãOKã ããå®éããããçµéšã¯ãããŸãããLOG_2014ããŒãã«ã®2014/12/31ã®ããŒã¿ãééã£ãŠãã®ã§ãUPDATEããŠ2015幎ã®ããŒã¿ã«ãããïŒã£ãŠãšãã¯UPDATEãããªããŠDELETE INSERTãå¿ èŠã ã£ããããŸãè²ã NGã§ããã
DBã®ããŒãã£ã·ã§ãã³ã°ã®æ©èœã䜿ããŸãããã
9. ã©ãŠã³ãã£ã³ã°ãšã©ãŒïŒäžžã誀差ïŒ
FLOATãšãDOUBLEã®åã䜿ã£ãŠèª€å·®ã§åŸã§å°ããã€ã§ãã
誀差ã¯ITãšã³ãžãã¢ã®åºç€ç¥èããšãããšç¢ºãã«ãããªãã§ãããå®ã¯ããã§ã¯ãªããŠãèšç®æ©ç§åŠãåŠãã§ããªããšã³ãžãã¢ã¯çµæ§ããã®ã§ãæå€ãšããã£ããããŸãã
æµ®åå°æ°ç¹æ°ãšèª€å·®ãç¥ããªã人ã¯ãããåŠã¶ãšè¯ãã§ããããã®äžã§éçºããã¢ããªã±ãŒã·ã§ã³ãããŒã¿ã®èŠä»¶ãèæ ®ããŠNUMBERãFLOATãDOUBLEãNUMERICãDECIMALã®ã©ãã䜿ããèæ ®ãã¹ãã§ãããã
10. ãµãŒãã£ã¯ã³ãã¬ãŒããŒïŒïŒïŒã®ãã¬ãŒããŒïŒ
ããããEnumãšããããç¹å®ã®å€ãããŒãã«ã«ä¿åãããã€ã§ãã
CREATE TABLE Article (
status VARCHAR(20) CHECK (status IN ('Draft', 'Private Open', 'Open'))
);
äžèšã®å Žåã¯CHECKã§ãããENUMã§ãåãã§ããããã ãšå€ã®åè£ã¯CHECKãšããŠè¡šçŸãããŠããã®ã§ãïŒç°¡åã«ã¯ïŒãã®åè£äžèŠ§ãåããªãããå€ã®åè£ãè¿œå ããããšãé¢åã ã£ãããšè²ã ãã€ãã§ãã
ãããããšãã¯äŸãã°ArticleStatusãšãããããªããŒãã«ãå¥ã«äœããããã«åè£å€ãæ¿å ¥ããŠãåŸã¯å€éšããŒã§äœ¿ãããã«ããŸãããã
11. ãã¡ã³ãã ãã¡ã€ã«ïŒå¹»ã®ãã¡ã€ã«ïŒ
DBã«ãã¡ã€ã«ãä¿åããããªãã®ã§ãDBã«ã¯ãã¡ã€ã«ã®ãã¹ãå ¥ãã€ã€ããã¡ã€ã«ã¯ãã¡ã€ã«ã·ã¹ãã ã«ä¿åããã»ã»ãšãããã€ã§ãã
確ãã«DBã«ãã¡ã€ã«å ¥ããããªãæ°æã¡ãåãããŸãããå人çã«ã¯å ¥ãã掟ã§ãã
ãã¡ã€ã«ã·ã¹ãã ã«å ¥ããªããšãã©ã³ã¶ã¯ã·ã§ã³ãšãæŽåæ§ãšãè²ã ãããŸãããã解決çãšããŠã¯åœç¶ã§ãããBLOGãšãMEDIUMBLOGãšãã§ãã
12. ã€ã³ããã¯ã¹ã·ã§ããã¬ã³ïŒéé²ã·ã§ããã¬ã³ïŒ
ã€ã³ããã¯ã¹ããŸã£ããã€ããªãã£ãããã€ããŸãã£ãŠããŸã£ãããããã€ã§ãã
ã€ã³ããã¯ã¹ãå¹ãã¯ãšãªãšå¹ããªãã¯ãšãªãç解ããŠããªããšããè€åã€ã³ããã¯ã¹ã®ããšãåãã£ãŠããªããšãã
MENTORã®ååã«åºã¥ããŠå¹æçãªã€ã³ããã¯ã¹ç®¡çãããŸãããã
13. ãã£ã¢ã»ãªãã»ãžã»ã¢ã³ããŠã³ïŒææã®unknownïŒ
|| æŒç®åã§NULLãšæååãçµåããŠãçµæã¯NULLã§ãããšããŸãNULLé¢é£è²ã ã§ãã
NULLã¯äœããŠãunkownã«ãªãã®ã§ã¡ãããšIS NULL, IS NOT NULLããŸãããã
COALESCE, NVL, ISNULLãªã©ã®é¢æ°ãèŠããŠãããŸãããã
14. ã¢ã³ãã®ã¥ã¢ã¹ã°ã«ãŒãïŒææ§ãªã°ã«ãŒãïŒ
GROUP BY + MAXãšãã§ã²ã£ããããå€ãååšããè¡å šäœããååŸããã±ãŒã¹ãªã©ã®ãã€ã§ãã
以äžã¯æžç±ã®äŸããã®ãŸãŸåŒçšãããŠé ããŸãã
SELECT product_id, MAX(date_reported) AS latest, bug_id FROM Bugs INNER JOIN BugsProducts USING (bug_id) GROUP BY product_id;
Bill Karwin (2013). SQLã¢ã³ããã¿ãŒã³ æ ªåŒäŒç€Ÿãªã©ã€ãªãŒã»ãžã£ãã³
äžèšã®ãã¡ãªã¯ãšãªã¯ãã補åIDããšã«ã°ã«ãŒãã³ã°ããŠãææ°ã®ãã°å ±åæ¥ä»ãšäžç·ã«ãã®ãã°ã®IDãåºãããïŒããšããã¯ãšãªã§ããã€ãŸãå補åã®æãæè¿ã®ãã°ãç¥ãããèš³ã§ãã
ã§ãããããã¯æ®å¿µãªããå€ãã®DBã§ã¯ãšã©ãŒã«ãªãããMySQLã§ã¯å®è¡ã§ããŠãæå³éãã®å€ã«ãªããšã¯éããŸãããbug_idã¯ã°ã«ãŒãã³ã°ããã³éèšã®å¯Ÿè±¡ã§ãªãã®ã§ãäœãåºãã°è¯ãã®ãDBãåãããªãã®ã§ãã
察çã¯è²ã ããã®ã§è©³ããããšã¯æ¬ãèªã¿ãŸããããã¡ãã£ãšé£ããJOINãšãã§å¯Ÿå¿ããŸãã
 15. ã©ã³ãã ã»ã¬ã¯ã·ã§ã³
ç¹å®ã®ããŒãã«ããã©ã³ãã ã«ïŒè¡ååŸãããšããã¯ãšãªãçºè¡ãããã€ã§ãã
SELECT * FROM Article ORDER BY RAND() LIMIT 1;
ããŒãã«ã®ããŒã¿ãå¢ããã»ã©é ããªã£ãŠè¡ããŸãã
ã¢ããªåŽã§ã©ã³ãã ãªIDãçæããŠããããæå®ããŠãè¯ãã§ãããSQLã§ããããå Žåãã¯ãšãªã工倫ããŸãã1ãæ倧å€ã®éã®ã©ã³ãã ãªå€ãšãã
16. ãã¢ãã³ãºã»ãµãŒããšã³ãžã³ïŒè²§è ã®ãµãŒããšã³ãžã³ïŒ
å šææ€çŽ¢ãããããŠLIKE â%foo%â;ãšãã£ãŠããŸããã€ã§ãã
ããŒã¿ãå°ãªããªãåé¡ã¯ãªãã§ãããããŒã¿ãå€ããªãã°%foo%ã§ã¯ã€ã³ããã¯ã¹ãå¹ããªãã®ã§ããªãé ããªã£ãŠããŸããŸãã
ãªã®ã§ãDBã«ä»å±ããŠããããããã¯DB以å€ã®å šææ€çŽ¢ãšã³ãžã³ã䜿ããŸããããäŸãã°MySQL 5.6.4ã§ã¯å šææ€çŽ¢æ©èœãæèŒããããã©æ¥æ¬èªå¯Ÿå¿ããŠããªããšããã£ããããŸãã
䜿ã£ãŠããDBã«æ¥æ¬èªå¯Ÿå¿å šææ€çŽ¢ãšã³ãžã³ããããªããããããªããã°Apache LuceneãElasticsearchã䜿ãæãã§ããããã
17. ã¹ãã²ããã£ã¯ãšãª
éèšã»ã°ã«ãŒãã³ã°ãªã©ãé§äœ¿ããŠè€éãªã¯ãšãªãäœããã€ã§ãã
è€éãªãã®ãäœãã°äžçºã§åžæããçµæãåããŸãããã¡ã³ãã倧å€ã ã£ããããã®ã§ãåå²ããŸãããããšãã話ã§ãã
18. ã€ã³ããªã·ããã«ã©ã ïŒæé»ã®åïŒ
SELECT * ãšããããã«ã¯ã€ã«ãã«ãŒãã䜿ããã€ã§ãã
ã¯ã€ã«ãã«ãŒãã¯æ¥œã ãã100%æªããšããããã§ã¯ãªãã§ãããããã䜿ãããã¯å€å°é¢åã§ãåãåæããæ¹ãå®å šã ãã確å®ã ããããšãã話ã§ãã
19. ãªãŒããã«ãã¹ã¯ãŒãïŒèªã¿åãå¯èœãã¹ã¯ãŒãïŒ
USERããŒãã«ãšãã®PASSWORDåã«å¹³æã®ïŒçã®ïŒãã¹ã¯ãŒããä¿åããŠããŸããã€ã§ãã絶察èŸããŸãããã
ãªãçã®ãã¹ã¯ãŒããä¿åããŠã¯ãããªãã®ãã¯ä»¥äžã®æ¬ãåèã«ãªããŸããITãšã³ãžãã¢ã§ã»ãã¥ãªãã£ã«çããªãã°èªãã§ãããŸãããã
[tmkm-amazon]4797361190[/tmkm-amazon]
DBã®ããã·ã¥é¢æ°äœ¿ãã°OKãšãã人ãèŠæ³šæã§ããã¢ããªãšDBéã®ã»ãã¥ãªãã£çãªæå³ã§ã
ç°¡åã«èšããšè§£æ±ºç㯠SSL + ã¢ããªåŽã§ã®ããã·ã¥å + åå¥ãœã«ã + ã¹ãã¬ããã³ã°ã§ãã
20. SQLã€ã³ãžã§ã¯ã·ã§ã³
ãããäžèšã®æ¬ã«èŒã£ãŠããŸãã
解決çã¯äžèšã§èšããšPrepared statementã§ãã
21. ã·ã¥ãŒãããŒã»ããŒãããªãŒã¯ïŒç䌌ããŒæœçæ§ïŒ
AUTOINCREMENTALãªIDåãäœã£ãã¯è¯ããã©ãæ¬ ããŠããIDãããã®ã§ãç¡çã«åããããšãããã€ã§ãã
ç¡çã«åããããšããŠã¯ãããŸãããæ¬ çªãæ°æã¡æªããŠãææ ¢ããŸããããIDã足ããªããªãã®ãæããªã64bitæŽæ°ã®åã«ããŠãããŸããããTwitterã®ãã€ãŒãã§ãããŸã 53ã ã54bitã§ãã
ã·ãŒã»ããŒã»ãšãã«ïŒèããã®ã«çïŒ
SQLãå€åäœããã®Client(äŸãã°JDBCãšã)ããå®è¡ãããšæããŸããããã®ãšãã«ã¬ã¹ãã³ã¹ã³ãŒããç¡èŠããããäœãåé¡ãçºçããŠãããšãã«ãœãŒã¹äžã®SQLããèªãŸãªããšãããã€ã§ãã
ã¬ã¹ãã³ã¹ã³ãŒãã¯ã¡ãããšèŠãŸãããã
ãœãŒã¹äžã®SQLæ§ç¯ãééã£ãŠããã±ãŒã¹ãšãããã®ã§ãSQLãã°èŠããprepared statementã®SQLèŠããããšã«ããã³ãŒãçºãã以å€ãããŸãããã
23. ãã£ããããã£ãã¯ã»ã€ãã¥ããã£ïŒå€äº€ç¹æš©ïŒ
ããã¯ãªããDBãšããããã¯äžè¬çãªè©±ã§ããã
ææžåã»ããŒãžã§ã³ç®¡çã»ãã¹ããªã©ã
24. ããžãã¯ããŒã³ãºïŒéæ³ã®è±ïŒ
MVCã®MãåçŽåãããããŠããããActiveRecordãªããžã§ã¯ããã®ãã®ã ãã ã£ãããããã€ã§ãã
MVCã®ããããåéããšããŠãMã¯Modelã§Modelã¯ãã ã®ããŒã¿æ§é ããšãããã®ããããšæããŸããïŒã»ã»ã»ã§ãããïŒïŒ
ããã¯ééã£ãŠããŠModelã¯ããžãã¹ããžãã¯ãã®ãã®ãªã®ã§ãããŒã¿æ§é ãããã ãã©ããã£ãšéãè²ã åŠçã責åãæã£ãŠããŸãã
ãªã®ã§M = ActiveRecordã ãããšãã¯ééã£ãŠããŠãããŸã§ActiveRecordãhas aã§æã£ãŠãããšèããŸãããã
ïŒãšãããããªè©±ã§ãïŒ
ç ã®å
ã€ã³ã·ãã³ã察å¿ã®è©±ã§ããSQLã¢ã³ããã¿ãŒã³ãšãããéçºãããžã§ã¯ãã¢ã³ããã¿ãŒã³ãªæãã®è©±ãªã®ã§çç¥ããŸãã
ãŸãšã
ããªãçç¥ããŠããŸããŸããããç°¡åã«ãŸãšããŠã¿ãŸããã
SQLã¢ã³ããã¿ãŒã³ãããªãã®è¯æžãªã®ã§ãDBAãDBãå©çšããŠããITãšã³ãžãã¢ããããŠããããäžã«åºãåŠçãã¿ãªãããã²ãšãèªãã§ãããŸãããã
[tmkm-amazon]4873115892[/tmkm-amazon]