ãã®ãã¥ãŒããªã¢ã«ã«ã€ããŠ
ãã®ãã¥ãŒããªã¢ã«ã¯ãSQLèšèªïŒDDLãDMLïŒã®ãèšæ¶ã®ã¹ã¿ã³ããã®ãããªãã®ã§ãã ããã¯ãå°éçãªæŽ»åã®éçšã§èç©ãããæ å ±ã§ãããåžžã«ç§ã®é ã®äžã«ä¿åãããŠããŸãã ããã¯ç§ã«ãšã£ãŠååãªæå°å€ã§ãããããŒã¿ããŒã¹ãæäœãããšãã«æããã䜿çšãããŸãã ããå®å šãªSQLæ§é ã䜿çšããå¿ èŠãããå Žåã¯ãéåžžãã€ã³ã¿ãŒãããäžã®MSDNã©ã€ãã©ãªã«å©ããæ±ããŸãã ç§ã®æèŠã§ã¯ããã¹ãŠãé ã®äžã«åããããšã¯éåžžã«é£ãããããã¯ç¹ã«å¿ èŠãããŸããã ããããåºæ¬çãªæ§é ãç¥ãããšã¯éåžžã«äŸ¿å©ã§ãã OracleãMySQLãFirebirdãªã©ãå€ãã®ãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ã§ã»ãŒåã圢åŒã§é©çšã§ããŸãã éãã¯äž»ã«ããŒã¿ã¿ã€ãã«ããã詳现ã¯ç°ãªãå ŽåããããŸãã SQLèšèªã®åºæ¬çãªæ§é ã¯ããã»ã©å€ãã¯ãªãã絶ããç·Žç¿ããã°ããã«èšæ¶ãããŸãã ããšãã°ããªããžã§ã¯ãïŒããŒãã«ãå¶çŽãã€ã³ããã¯ã¹ãªã©ïŒãäœæããã«ã¯ãããŒã¿ããŒã¹ãæäœããç°å¢ïŒIDEïŒã®ããã¹ããšãã£ã¿ãŒãããã°ååã§ãããç¹å®ã®ã¿ã€ãã®ããŒã¿ããŒã¹ïŒMS SQLãæäœããããã«èª¿æŽãããããžã¥ã¢ã«ããŒã«ã調ã¹ãå¿ èŠã¯ãããŸãããOracleãMySQLãFirebirdã...ïŒã ãã¹ãŠã®ããã¹ããç®ã®åã«ãããããšãã°ã€ã³ããã¯ã¹ãå¶çŽãäœæããããã«å€æ°ã®ã¿ããå®è¡ããå¿ èŠããªããããããã¯äŸ¿å©ã§ãã ããŒã¿ããŒã¹ãšã®çµ¶ãéãªãäœæ¥ã«ãããã¹ã¯ãªããã䜿çšããŠãªããžã§ã¯ããäœæãå€æŽãç¹ã«åäœæããããšã¯ãããžã¥ã¢ã«ã¢ãŒãã§è¡ãå Žåãããäœåãé«éã§ãã ãŸããã¹ã¯ãªããã¢ãŒãã§ãïŒãããããæ éã«ïŒããªããžã§ã¯ãã®åœåèŠåãèšå®ããã³å¶åŸ¡ããæ¹ãç°¡åã§ãïŒç§ã®äž»èŠ³çãªæèŠïŒã ããã«ãã¹ã¯ãªããã¯ã1ã€ã®ããŒã¿ããŒã¹ïŒããšãã°ããã¹ãããŒã¿ããŒã¹ïŒã§è¡ãããå€æŽãåã圢åŒã§å¥ã®ããŒã¿ããŒã¹ã«è»¢éããå¿ èŠãããå ŽåïŒçç£çïŒã«äœ¿çšãããšäŸ¿å©ã§ãã
SQLèšèªã¯ããã€ãã®éšåã«åãããŠããŸããããã§ã¯ãæãéèŠãª2ã€ã®éšåãæ€èšããŸãã
- DDL-ããŒã¿å®çŸ©èšèª
- DML-ããŒã¿æäœèšèªã次ã®æ§æèŠçŽ ãå«ãŸããŸãã
- SELECT-ããŒã¿éžæ
- INSERT-æ°ããããŒã¿ãæ¿å ¥ããŸã
- æŽæ°-ããŒã¿æŽæ°
- DELETE-ããŒã¿åé€
- MERGE-ããŒã¿ã®ããŒãž
ãªããªã ç§ã¯å®å家ãªã®ã§ããã®æç§æžã«ã¯çè«ãã»ãšãã©ãªãã®ã§ããã¹ãŠã®æ§æã¯å®éçãªäŸã§èª¬æããŸãã ããã«ãããã°ã©ãã³ã°èšèªãç¹ã«SQLã¯ãèªåã§ãããæããç¹å®ã®æ§æãå®è¡ãããšãã«äœãèµ·ããããç解ããããšã«ãã£ãŠãå®éã«ã®ã¿ç¿åŸã§ãããšä¿¡ããŠããŸãã
ãã®ãã¥ãŒããªã¢ã«ã¯ãã¹ããããã€ã¹ãããã®ååã«åºã¥ããŠäœæãããŠããŸãã ãããé çªã«ããããŠã§ããã°äŸã®çŽåŸã«èªãããšãå¿ èŠã§ãã ãã ããéäžã§ããŒã ã«ã€ããŠããã«è©³ããç¥ãå¿ èŠãããå Žåã¯ãMSDNã©ã€ãã©ãªãªã©ãã€ã³ã¿ãŒãããã§ç¹å®ã®æ€çŽ¢ã䜿çšããŠãã ããã
ãã®ãã¥ãŒããªã¢ã«ãæžããšãã¯ãMS SQL ServerããŒãžã§ã³2014ããŒã¿ããŒã¹ã䜿çšããŸããããã¹ã¯ãªããã«ã¯MS SQL Server Management StudioïŒSSMSïŒã䜿çšããŸããã
MS SQL Server Management StudioïŒSSMSïŒã«ã€ããŠç°¡åã«èª¬æããŸã
SQL Server Management StudioïŒSSMSïŒã¯ãããŒã¿ããŒã¹ã³ã³ããŒãã³ããæ§æã管çãããã³ç®¡çããããã®Microsoft SQL Serverã®ãŠãŒãã£ãªãã£ã§ãã ãã®ãŠãŒãã£ãªãã£ã«ã¯ãã¹ã¯ãªãããšãã£ã¿ãŒïŒäž»ã«äœ¿çšããŸãïŒãšããªããžã§ã¯ãããã³ãµãŒããŒèšå®ã§åäœããã°ã©ãã£ã«ã«ããã°ã©ã ãå«ãŸããŠããŸãã SQL Server Management Studioã®ã¡ã€ã³ããŒã«ã¯ãªããžã§ã¯ããšã¯ã¹ãããŒã©ãŒã§ããŠãŒã¶ãŒã¯ãµãŒããŒãªããžã§ã¯ãã衚瀺ãååŸã管çã§ããŸãã ãã®ããã¹ãã¯ãŠã£ãããã£ã¢ããéšåçã«åããŠããŸãã
æ°ããã¹ã¯ãªãããšãã£ã¿ãŒãäœæããã«ã¯ã[æ°ããã¯ãšãª]ãã¿ã³ã䜿çšããŸãã
çŸåšã®ããŒã¿ããŒã¹ãå€æŽããã«ã¯ãããããããŠã³ãªã¹ãã䜿çšã§ããŸãã
ç¹å®ã®ã³ãã³ãïŒãŸãã¯ã³ãã³ãã®ã°ã«ãŒãïŒãå®è¡ããã«ã¯ããã®ã³ãã³ããéžæããŠ[å®è¡]ãã¿ã³ãŸãã¯[F5]ããŒãæŒããŸãã ãšãã£ã¿ãŒã«çŸåšã³ãã³ãã1ã€ããå«ãŸããŠããªãå ŽåããŸãã¯ãã¹ãŠã®ã³ãã³ããå®è¡ããå¿ èŠãããå Žåã¯ãäœãéžæããå¿ èŠã¯ãããŸããã
ã¹ã¯ãªããïŒç¹ã«ãªããžã§ã¯ãïŒããŒãã«ãåãã€ã³ããã¯ã¹ïŒã®äœæïŒãå®è¡ããåŸãå€æŽã確èªããã«ã¯ãã³ã³ããã¹ãã¡ãã¥ãŒããæŽæ°ã䜿çšããŠã察å¿ããã°ã«ãŒãïŒããŒãã«ãªã©ïŒãããŒãã«èªäœããŸãã¯ãã®äžã®åã°ã«ãŒãã匷調衚瀺ããŸãã
å®éãããã«æããäŸãå®äºããããã«ç¥ã£ãŠããå¿ èŠãããã®ã¯ããã ãã§ãã SSMSãŠãŒãã£ãªãã£ã®æ®ãã®éšåã¯ãèªåã§ç°¡åã«ç¿åŸã§ããŸãã
çè«ã®ããã
ãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ïŒRDBããŸãã¯åã«DBã®ã³ã³ããã¹ãã§ã¯ä»¥äžïŒã¯ãçžäºæ¥ç¶ãããããŒãã«ã®ã³ã¬ã¯ã·ã§ã³ã§ãã 倧ãŸãã«èšããšãããŒã¿ããŒã¹ãšã¯ãããŒã¿ãæ§é åããã圢åŒã§ä¿åãããŠãããã¡ã€ã«ã§ãã
DBMS-ãããã®ããŒã¿ããŒã¹ã®ç®¡çã·ã¹ãã ãã€ãŸã ç¹å®ã®ã¿ã€ãã®ããŒã¿ããŒã¹ïŒMS SQLãOracleãMySQLãFirebirdãªã©ïŒãæäœããããã®ããŒã«ã®ã»ããã§ãã
ã泚æ
ãªããªã 人çã§ã¯ãå£èªã§ãOracle DBãããŸãã¯å®éã«ã¯ãOracle DBMSããæå³ãããOracleããšããèšãããŸããããã®ãã¥ãŒããªã¢ã«ã®æèã§ã¯ãDBããšããçšèªãæã 䜿çšãããŸãã ã³ã³ããã¹ããããç§ã¯ãããæ£ç¢ºã«äœãæå³ããã®ããæ確ã«ãªããšæããŸãã
ããŒãã«ã¯åã®ã³ã¬ã¯ã·ã§ã³ã§ãã åã¯ãã£ãŒã«ããŸãã¯åãšãåŒã°ãããããã®ãã¹ãŠã®åèªã¯åããã®ãè¡šãå矩èªãšããŠäœ¿çšãããŸãã
ããŒãã«ã¯RDBã®ã¡ã€ã³ãªããžã§ã¯ãã§ããããã¹ãŠã®DBDããŒã¿ã¯ããŒãã«ã®åã«1è¡ãã€ä¿åãããŸãã è¡ãã¬ã³ãŒããå矩èªã§ãã
åããŒãã«ãšãã®åã«ååãä»ãããããã®åŸããããã«ã¢ã¯ã»ã¹ãããŸãã
MS SQLã®ãªããžã§ã¯ãã®ååïŒããŒãã«åãååãã€ã³ããã¯ã¹åãªã©ïŒã®æ倧é·ã¯128æåã§ãã
åè -ORACLEããŒã¿ããŒã¹ã§ã¯ããªããžã§ã¯ãã®ååã®æ倧é·ã¯30æåã§ãã ãããã£ãŠãç¹å®ã®ããŒã¿ããŒã¹ã§ã¯ãæåæ°ã®å¶éãæºããããã«ããªããžã§ã¯ãã®åœåèŠåãç¬èªã«äœæããå¿ èŠããããŸãã
SQLã¯ãDBMSãä»ããŠããŒã¿ããŒã¹ã§ã¯ãšãªãå®è¡ã§ããããã«ããèšèªã§ãã ç¹å®ã®DBMSã§ã¯ãSQLèšèªã«ç¹å®ã®å®è£ ïŒç¬èªã®æ¹èšïŒãæãããããšãã§ããŸãã
DDLãšDMLã¯SQLèšèªã®ãµãã»ããã§ãã
- DDLèšèªã¯ãããŒã¿ããŒã¹ã®æ§é ãäœæããã³å€æŽããããã«äœ¿çšãããŸãã ããŒãã«ãšãªã¬ãŒã·ã§ã³ã·ãããäœæ/å€æŽ/åé€ããŸãã
- DMLèšèªã䜿çšãããšãããŒãã«ããŒã¿ãæäœã§ããŸãã 圌女ã®ã©ã€ã³ã§ã ããŒãã«ããããŒã¿ãéžæããããããŒãã«ã«æ°ããããŒã¿ãè¿œå ããããæ¢åã®ããŒã¿ãæŽæ°ããã³åé€ãããã§ããŸãã
SQLã§ã¯ã2çš®é¡ã®ã³ã¡ã³ãïŒåäžè¡ãšè€æ°è¡ïŒã䜿çšã§ããŸãã
--
ãããŠ
/* */
å®éãããã®çè«ã«é¢ãããã¹ãŠã§ååã§ãã
DDL-ããŒã¿å®çŸ©èšèª
ããšãã°ãããã°ã©ããŒã§ã¯ãªã人ã®éåžžã®åœ¢åŒã§åŸæ¥å¡ã«é¢ããããŒã¿ãå«ãããŒãã«ãèããŠã¿ãŸãããã
åŸæ¥å¡çªå· | æ°å | ç幎ææ¥ | é»åã¡ãŒã« | åœ¹è· | éšé |
---|---|---|---|---|---|
1000 | ã€ã¯ããI.I. | 1955幎2æ19æ¥ | i.ivanov@test.tt | ç£ç£ | éå¶ |
1001 | ããããP.P. | 1983幎12æ3æ¥ | p.petrov@test.tt | ããã°ã©ã㌠| IT |
1002 | ã·ãããS.S. | 1976/07/07 | s.sidorov@test.tt | äŒèšå£« | ç°¿èš |
1003 | ã¢ã³ãã¬ãšãA.A. | 1982幎4æ17æ¥ | a.andreev@test.tt | äžçŽããã°ã©ã㌠| IT |
ãã®å ŽåãããŒãã«ã®åã«ã¯æ¬¡ã®ååããããŸãïŒåŸæ¥å¡çªå·ãååãç幎ææ¥ãé»åã¡ãŒã«ã圹è·ãéšéã
ãããã®ååã¯ãå«ãŸããããŒã¿ã®ã¿ã€ãã«ãã£ãŠç¹åŸŽä»ããããŸãã
- åŸæ¥å¡çªå·ã¯æŽæ°ã§ã
- åå-æåå
- ç幎ææ¥-æ¥ä»
- ã¡ãŒã«-æåå
- äœçœ®-ã©ã€ã³
- éšé-ã©ã€ã³
åã¿ã€ãã¯ãç¹å®ã®åãæ ŒçŽã§ããããŒã¿ã®çš®é¡ã瀺ãç¹æ§ã§ãã
ãŸããMS SQLã§äœ¿çšããã次ã®åºæ¬ããŒã¿åã®ã¿ãèŠããŠããã°ååã§ãã
äŸ¡å€ | MS SQLã§ã®æå® | 説æ |
---|---|---|
å¯å€é·æåå | varcharïŒNïŒ
ãã㊠nvarcharïŒNïŒ | æ°å€Nã䜿çšããŠã察å¿ããåã®å¯èœãªæ倧è¡é·ãæå®ã§ããŸãã ããšãã°ããååãåã®å€ã«æ倧30æåãå«ããããšãã§ããå Žåã¯ãnvarcharïŒ30ïŒã«èšå®ããå¿
èŠããããŸãã
varcharãšnvarcharã®éãã¯ãvarcharã䜿çšãããšã1æåã1ãã€ãã®ASCII圢åŒã§æååãä¿åã§ããåæåã2ãã€ãã®Unicode圢åŒã§æååãä¿åã§ããããšã§ãã varcharåã¯ããã®ãã£ãŒã«ãã«Unicodeæåãä¿åããå¿ èŠããªãããšã100ïŒ ç¢ºå®ãªå Žåã«ã®ã¿äœ¿çšããŠãã ããã ããšãã°ãvarcharã¯æ¬¡ã®ããã«é»åã¡ãŒã«ã¢ãã¬ã¹ãä¿åããããã«äœ¿çšã§ããŸãã éåžžãASCIIæåã®ã¿ãå«ãŸããŸãã |
åºå®é·ã¹ããªã³ã° | charïŒNïŒ
ãã㊠ncharïŒNïŒ | ãã®ã¿ã€ãã¯å¯å€é·ã¹ããªã³ã°ãšç°ãªããã¹ããªã³ã°ã®é·ããNæåããçãå Žåãåžžã«ã¹ããŒã¹ã§é·ãNã«åã蟌ãŸãããã®åœ¢åŒã§ããŒã¿ããŒã¹ã«ä¿åãããŸãã ããŒã¿ããŒã¹ã§ã¯ãæ£ç¢ºã«Næåã䜿çšããŸãïŒ1æåã¯charã«1ãã€ããncharåã«2ãã€ãã䜿çšããŸãïŒã ç§ã®ç·Žç¿ã§ã¯ããã®ã¿ã€ãã¯ãã£ãã«äœ¿çšãããã䜿çšãããå Žåãäž»ã«charïŒ1ïŒåœ¢åŒã§äœ¿çšãããŸãã ãã£ãŒã«ãã1æåã§å®çŸ©ãããŠããå Žåã |
æŽæ° | int | ãã®ã¿ã€ãã§ã¯ãåã«æ£ãšè² ã®æŽæ°ã®ã¿ã䜿çšã§ããŸãã åç §çšïŒä»ã§ã¯ããã¯ããŸãé¢ä¿ãããŸããïŒ--2 147 483 648ãã2 147 483 647ãŸã§ã®intåãèš±å¯ããæ°å€ã®ç¯å²ãéåžžãããã¯èå¥åãèšå®ããããã«äœ¿çšãããäž»ãªåã§ãã |
å®æ°ãŸãã¯å®æ° | æµ®ã | ç°¡åã«èšãã°ããããã¯å°æ°ç¹ïŒã³ã³ãïŒãååšã§ããæ°å€ã§ãã |
æ¥ä» | æ¥ä» | åã«æ¥ä»ã®ã¿ãæ ŒçŽããå¿ èŠãããå Žåã¯ãæ¥ä»ãæã幎ã®3ã€ã®ã³ã³ããŒãã³ãã§æ§æãããŸãã ããšãã°ã2014幎2æ15æ¥ïŒ2014幎2æ15æ¥ïŒã ãã®ã¿ã€ãã¯ããå ¥åŠæ¥ãããç幎ææ¥ããªã©ã®åã«äœ¿çšã§ããŸãã æ¥ä»ã®ã¿ãä¿®æ£ããããšãéèŠã§ããå ŽåããŸãã¯æå»ã³ã³ããŒãã³ããéèŠã§ã¯ãªãç Žæ£ã§ããå ŽåããŸãã¯äžæãªå Žåã |
æé | æé | ãã®ã¿ã€ãã¯ãæéããŒã¿ã®ã¿ãåã«æ ŒçŽããå¿
èŠãããå Žåã«äœ¿çšã§ããŸãã æéãåãç§ãããªç§ã ããšãã°ã17ïŒ38ïŒ31.3231603
ããšãã°ãæ¯æ¥ã®ãã©ã€ãåºçºæå»ã |
æ¥æ | æ¥æ | ãã®ã¿ã€ãã§ã¯ãæ¥ä»ãšæå»ã®äž¡æ¹ãåæã«ä¿åã§ããŸãã ããšãã°ã02.15.2014 17ïŒ38ïŒ31.323
ããšãã°ãããã¯ã€ãã³ãã®æ¥ä»ãšæå»ã§ãã |
æ | å°ã | ãã®ã¿ã€ãã¯ããã¯ãã/ãããããã¿ã€ãã®å€ãä¿åããã®ã«äŸ¿å©ã§ãããã¯ããã¯1ãšããŠä¿åããããããããã¯0ãšããŠä¿åãããŸãã |
ãŸãããã£ãŒã«ãã®å€ã¯ãçŠæ¢ãããŠããªãå Žåã¯ç€ºãããŠããªãå¯èœæ§ãããããã®ç®çã«ã¯NULLããŒã¯ãŒãã䜿çšãããŸãã
ãµã³ãã«ãå®è¡ããã«ã¯ãTestãšãããã¹ãããŒã¿ããŒã¹ãäœæããŸãã
次ã®ã³ãã³ããå®è¡ããããšã«ãããåçŽãªããŒã¿ããŒã¹ïŒè¿œå ã®ãã©ã¡ãŒã¿ãŒãæå®ããã«ïŒãäœæã§ããŸãã
CREATE DATABASE Test
ã³ãã³ãã䜿çšããŠããŒã¿ããŒã¹ãåé€ã§ããŸãïŒãã®ã³ãã³ãã«ã¯éåžžã«æ³šæããå¿ èŠããããŸãïŒã
DROP DATABASE Test
ããŒã¿ããŒã¹ã«åãæ¿ããã«ã¯ã次ã®ã³ãã³ããå®è¡ã§ããŸãã
USE Test
ãŸãã¯ãSSMSã¡ãã¥ãŒé åã®ããããããŠã³ãªã¹ããããã¹ãããŒã¿ããŒã¹ãéžæããŸãã ç§ãšäžç·ã«ä»äºããããšããããŒã¿ããŒã¹ãåãæ¿ãããã®æ¹æ³ãæããã䜿çšãããŸãã
ããŒã¿ããŒã¹ã§ãã¹ããŒã¹ãšããªã«æåã䜿çšããŠããã©ãŒã ã®èª¬æããã®ãŸãŸäœ¿çšããŠããŒãã«ãäœæã§ããŸãã
CREATE TABLE []( [ ] int, [] nvarchar(30), [ ] date, [E-mail] nvarchar(30), [] nvarchar(30), [] nvarchar(30) )
ãã®å Žåãè§æ¬åŒ§ã§ååãå²ãå¿ èŠããããŸã[...]ã
ãã ããããŒã¿ããŒã¹ã§ã¯ãå©äŸ¿æ§ãé«ããããã«ããªããžã§ã¯ãã®ãã¹ãŠã®ååãã©ãã³ã¢ã«ãã¡ãããã§æå®ããååã«ã¹ããŒã¹ã䜿çšããªãæ¹ãé©åã§ãã MS SQLã§ã¯ãéåžžãã®å Žåãååèªã¯å€§æåã§å§ãŸããŸããããšãã°ããPersonnel numberããã£ãŒã«ãã§ã¯ãPersonnelNumberãšããååãèšå®ã§ããŸãã ååã«çªå·ã䜿çšããããšãã§ããŸãïŒäŸïŒPhoneNumber1ïŒã
ã泚æ
äžéšã®DBMSã§ã¯ã次ã®PHONE_NUMBERåœå圢åŒãæãŸããå ŽåããããŸããããšãã°ããã®åœ¢åŒã¯ORACLEããŒã¿ããŒã¹ã§ãã䜿çšãããŸãã åœç¶ããã£ãŒã«ãåãèšå®ãããšãã¯ãDBMSã§äœ¿çšãããããŒã¯ãŒããšäžèŽããªãããšãæãŸããã§ãã
ãã®ãããè§æ¬åŒ§ã§å²ãŸããæ§æãå¿ããŠã[Employees]ããŒãã«ãåé€ã§ããŸãã
DROP TABLE []
ããšãã°ãåŸæ¥å¡ãå«ãããŒãã«ã¯ãåŸæ¥å¡ããšåŒã°ãããã®ãã£ãŒã«ãã«ã¯æ¬¡ã®ååãä»ããããšãã§ããŸãã
- ID-åŸæ¥å¡çªå·ïŒåŸæ¥å¡èå¥åïŒ
- åå-åå
- èªçæ¥-ç幎ææ¥
- ã¡ãŒã«-ã¡ãŒã«
- äœçœ®-äœçœ®
- éšé-éšé
å€ãã®å ŽåãIDãã£ãŒã«ãã®ååã«ã¯IDãšããåèªã䜿çšãããŸãã
ããŒãã«ãäœæããŸãã
CREATE TABLE Employees( ID int, Name nvarchar(30), Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30) )
å¿ èŠãªåãæå®ããã«ã¯ãNOT NULLãªãã·ã§ã³ã䜿çšã§ããŸãã
æ¢åã®ããŒãã«ã®å Žåã次ã®ã³ãã³ãã䜿çšããŠãã£ãŒã«ããåå®çŸ©ã§ããŸãã
-- ID ALTER TABLE Employees ALTER COLUMN ID int NOT NULL -- Name ALTER TABLE Employees ALTER COLUMN Name nvarchar(30) NOT NULL
ã泚æ
SQLèšèªã®äžè¬çãªæŠå¿µã¯ãã»ãšãã©ã®DBMSã§åãã§ãïŒå°ãªããšãããããŸã§ã«äœæ¥ããDBMSã§å€æã§ããŸãïŒã ããŸããŸãªDBMSã®DDLã®éãã¯äž»ã«ããŒã¿åã«ããïŒååã ãã§ãªããå®è£ ã®è©³çŽ°ãç°ãªãïŒãSQLèšèªå®è£ ã®ä»æ§ããããã«ç°ãªãå ŽåããããŸãïŒã€ãŸããã³ãã³ãã®æ¬è³ªã¯åãã§ãããããããæ¹èšã«ã¯ããããªéãããããããããŸããããæšæºã¯ãããŸããïŒã SQLã®åºæ¬ãææããŠããããã1ã€ã®DBMSããå¥ã®DBMSã«ç°¡åã«åãæ¿ããããšãã§ããŸãã ãã®å Žåãæ°ããDBMSã§ã®ã³ãã³ãã®å®è£ ã®è©³çŽ°ãã€ãŸã ã»ãšãã©ã®å Žåãé¡äŒŒæ§ãåŒãåºãã ãã§ååã§ãã
æ ¹æ ããªãããã«ãORACLE DBMSã®åãã³ãã³ãã®äŸãããã€ã瀺ããŸãã
-- CREATE TABLE Employees( ID int, -- ORACLE int - () number(38) Name nvarchar2(30), -- nvarchar2 ORACLE nvarchar MS SQL Birthday date, Email nvarchar2(30), Position nvarchar2(30), Department nvarchar2(30) ); -- ID Name ( ALTER COLUMN MODIFY(âŠ)) ALTER TABLE Employees MODIFY(ID int NOT NULL,Name nvarchar2(30) NOT NULL); -- PK ( MS SQL, ) ALTER TABLE Employees ADD CONSTRAINT PK_Employees PRIMARY KEY(ID);
ORACLEã®å Žåãvarchar2ã¿ã€ãã®å®è£ ã«é¢ããŠéãããããŸã;ãšã³ã³ãŒãã£ã³ã°ã¯ããŒã¿ããŒã¹èšå®ã«äŸåããããã¹ãã¯ãããšãã°UTF-8ãšã³ã³ãŒãã£ã³ã°ã§ä¿åã§ããŸãã ããã«ãORACLEã®ãã£ãŒã«ãé·ã¯ããã€ããšæåã®äž¡æ¹ã§æå®ã§ããŸãããã®è¿œå ãªãã·ã§ã³ã§ã¯ãBYTEãšCHARã䜿çšãããŸãããããã¯ããã£ãŒã«ãé·ã®åŸã«ç€ºãããŸããããšãã°ã次ã®ããã«ãªããŸãã
NAME varchar2(30 BYTE) -- 30 NAME varchar2(30 CHAR) -- 30
ORACLEã®varchar2ïŒ30ïŒã®åçŽãªæ瀺ã®å Žåãããã©ã«ãã§BYTEãŸãã¯CHARã®ã©ã¡ãã䜿çšããããã¯ãããŒã¿ããŒã¹èšå®ã«äŸåããIDEèšå®ã§æå®ãããããšããããŸãã äžè¬çã«ãæ··ä¹±ããããšãããã®ã§ãORACLEã®å Žåãvarchar2åã䜿çšãããšïŒããšãã°ãUTF-8ãšã³ã³ãŒãã䜿çšããå Žåã«ãããæ£åœåãããããšããããŸãïŒãæ瀺çã«CHARãèšè¿°ããããšã奜ã¿ãŸãïŒéåžžãæååã®é·ããæåã§èªã¿åãæ¹ã䟿å©ã§ãïŒ ïŒ
ãã ãããã®å ŽåãããŒãã«ã«æ¢ã«ããŒã¿ãããå Žåãã³ãã³ããæ£åžžã«å®è¡ããã«ã¯ãããŒãã«ã®ãã¹ãŠã®è¡ã§IDãã£ãŒã«ããšååãã£ãŒã«ãã«å ¥åããå¿ èŠããããŸãã ãããäŸã§ç€ºããIDãPositionãDepartmentãã£ãŒã«ãã®ããŒã¿ãããŒãã«ã«æ¿å ¥ããŸããããã¯æ¬¡ã®ã¹ã¯ãªããã§å®è¡ã§ããŸãã
INSERT Employees(ID,Position,Department) VALUES (1000,N'',N''), (1001,N'',N''), (1002,N'',N''), (1003,N' ',N'')
ãã®å ŽåãINSERTã³ãã³ãããšã©ãŒãã¹ããŒããŸãã 貌ãä»ãããšãã«ãå¿ èŠãª[åå]ãã£ãŒã«ãã®å€ãæå®ããŸããã§ããã
å ã®ããŒãã«ã«ãã®ããŒã¿ãæ¢ã«ããå ŽåãALTER TABLE Employees ALTER COLUMN ID int NOT NULLã³ãã³ãã¯æåããALTER TABLE Employees ALTER COLUMN Name int NOT NULLã³ãã³ãã¯ãšã©ãŒã¡ãã»ãŒãžãçºè¡ããŸããã Nameãã£ãŒã«ãã«NULLïŒæå®ãããŠããªãïŒå€ãããããšã
[åå]ãã£ãŒã«ãã«å€ãè¿œå ããããŒã¿ãåå ¥åããŸãã
INSERT Employees(ID,Position,Department,Name) VALUES (1000,N'',N'',N' ..'), (1001,N'',N'',N' ..'), (1002,N'',N'',N' ..'), (1003,N' ',N'',N' ..')
ãŸããæ°ããããŒãã«ãäœæãããšãã«NOT NULLãªãã·ã§ã³ãçŽæ¥äœ¿çšã§ããŸãã CREATE TABLEã³ãã³ãã®ã³ã³ããã¹ãã§ã
ãŸãã次ã®ã³ãã³ãã䜿çšããŠããŒãã«ãåé€ããŸãã
DROP TABLE Employees
次ã«ãå¿ èŠãªåIDãšååãæã€ããŒãã«ãäœæããŸãã
CREATE TABLE Employees( ID int NOT NULL, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30) )
ååã®åŸã«NULLãæžã蟌ãããšãã§ããŸããããã¯ãNULLå€ïŒæå®ãªãïŒãèš±å¯ãããããšãæå³ããŸããããã®ç¹æ§ã¯ããã©ã«ãã§æ³å®ããããããããã¯å¿ èŠãããŸããã
æ¢åã®åããªãã·ã§ã³ã«ããå Žåã¯ã次ã®ã³ãã³ãæ§æã䜿çšããå¿ èŠããããŸãã
ALTER TABLE Employees ALTER COLUMN Name nvarchar(30) NULL
ãŸãã¯åã«ïŒ
ALTER TABLE Employees ALTER COLUMN Name nvarchar(30)
ãŸãããã®ã³ãã³ãã䜿çšããŠããã£ãŒã«ãã®ã¿ã€ããå¥ã®äºææ§ã®ããã¿ã€ãã«å€æŽãããããã£ãŒã«ãã®é·ããå€æŽãããã§ããŸãã ããšãã°ã[åå]ãã£ãŒã«ãã50æåã«æ¡åŒµããŠã¿ãŸãããã
ALTER TABLE Employees ALTER COLUMN Name nvarchar(50)
äž»ããŒ
ããŒãã«ãäœæãããšãã¯ãåè¡ã«äžæã®äžæã®åãŸãã¯åã®ã»ãããå¿ èŠã§ãããã®ã¬ã³ãŒãã¯ããã®äžæã®å€ã«ãã£ãŠäžæã«èå¥ã§ããŸãã ãã®å€ã¯ãããŒãã«ã®äž»ããŒãšåŒã°ããŸãã EmployeesããŒãã«ã®å Žåããã®ãããªäžæã®å€ã¯IDåïŒãåŸæ¥å¡ã®äººäºçªå·ããå«ã-ãã®å Žåããã®å€ã¯ååŸæ¥å¡ã«å¯ŸããŠäžæã§ãããç¹°ãè¿ãããšã¯ã§ããŸããïŒã§ãã
次ã®ã³ãã³ãã䜿çšããŠãæ¢åã®ããŒãã«ãžã®äž»ããŒãäœæã§ããŸãã
ALTER TABLE Employees ADD CONSTRAINT PK_Employees PRIMARY KEY(ID)
ããã§ããPK_Employeesãã¯äž»ããŒå¶çŽã®ååã§ãã éåžžãPK_ãã¬ãã£ãã¯ã¹ã䜿çšããŠäž»ããŒã«ååãä»ãããã®åŸã«ããŒãã«åãä»ããŸãã
äž»ããŒãè€æ°ã®ãã£ãŒã«ãã§æ§æãããŠããå Žåããããã®ãã£ãŒã«ãã¯ãã«ã³ãã§åºåãããæ¬åŒ§å ã«ãªã¹ãããå¿ èŠããããŸãã
ALTER TABLE _ ADD CONSTRAINT _ PRIMARY KEY(1,2,âŠ)
MS SQLã§ã¯ãäž»ããŒãå ¥åãããã¹ãŠã®ãã£ãŒã«ãã«NOT NULLç¹æ§ãå¿ èŠã§ããããšã«æ³šæããŠãã ããã
ãŸããäž»ããŒã¯ããŒãã«ã®äœææã«çŽæ¥æ±ºå®ã§ããŸãã CREATE TABLEã³ãã³ãã®ã³ã³ããã¹ãã§ã ããŒãã«ãåé€ããŸãã
DROP TABLE Employees
次ã«ã次ã®æ§æã䜿çšããŠäœæããŸãã
CREATE TABLE Employees( ID int NOT NULL, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30), CONSTRAINT PK_Employees PRIMARY KEY(ID) -- PK , )
äœæåŸãããŒã¿ããŒãã«ã«å ¥åããŸãã
INSERT Employees(ID,Position,Department,Name) VALUES (1000,N'',N'',N' ..'), (1001,N'',N'',N' ..'), (1002,N'',N'',N' ..'), (1003,N' ',N'',N' ..')
è¡šã®äž»ããŒã1ã€ã®åã®å€ã®ã¿ã§æ§æãããå Žåã次ã®æ§æã䜿çšã§ããŸãã
CREATE TABLE Employees( ID int NOT NULL CONSTRAINT PK_Employees PRIMARY KEY, -- Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30) )
å®éãå¶éã®ååãèšå®ããããšã¯ã§ããŸããããã®å Žåãã·ã¹ãã åïŒãPK__Employee__3214EC278DA42077ããªã©ïŒãå²ãåœãŠãããŸãã
CREATE TABLE Employees( ID int NOT NULL, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30), PRIMARY KEY(ID) )
ãŸãã¯ïŒ
CREATE TABLE Employees( ID int NOT NULL PRIMARY KEY, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30) )
ãã ãã次ã®ããã«ãæ°žç¶ããŒãã«ã®å¶çŽåãåžžã«æ瀺çã«èšå®ããããšããå§ãããŸãã æ瀺çã«äžããããç解å¯èœãªååãä»ãããšããã®åŸã§æäœãããããªããŸããããšãã°ãåé€ã§ããŸãã
ALTER TABLE Employees DROP CONSTRAINT PK_Employees
ãã ããå¶éã®ååãæå®ããªããã®ãããªçãæ§æã¯ãäžæããŒã¿ããŒã¹ããŒãã«ïŒäžæããŒãã«ã®ååãïŒãŸãã¯##ã§å§ãŸãïŒãäœæãããšãã«äŸ¿å©ã«äœ¿çšããã䜿çšåŸã«åé€ãããŸãã
ãŸãšãããš
çŸæç¹ã§ã¯ã次ã®ã³ãã³ãã確èªããŸããã
- CREATE TABLE table_nameïŒãã£ãŒã«ããšãã®ã¿ã€ããå¶éã®åæïŒ-çŸåšã®ããŒã¿ããŒã¹ã«æ°ããããŒãã«ãäœæããŸãã
- DROP TABLE table_name-çŸåšã®ããŒã¿ããŒã¹ããããŒãã«ãåé€ããŸãã
- ALTER TABLE table_name ALTER COLUMN column_name ...-åã¿ã€ããæŽæ°ããããèšå®ãå€æŽãããããŸãïŒããšãã°ãNULLãŸãã¯NOT NULLç¹æ§ãæå®ããŸãïŒã
- ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY ïŒfield1ãfield2ã...ïŒ-æ¢åã®ããŒãã«ã«äž»ããŒãè¿œå ããŸãã
- ALTER TABLE table_name DROP CONSTRAINT constraint_name-ããŒãã«ããå¶çŽãåé€ããŸãã
äžæããŒãã«ã«ã€ããŠå°ã
MSDNããã®ã¯ãªããã³ã°ã MS SQL Serverã«ã¯ãããŒã«ã«ïŒïŒïŒãšã°ããŒãã«ïŒ##ïŒã®2çš®é¡ã®äžæããŒãã«ããããŸãã ããŒã«ã«äžæããŒãã«ã¯ãSQL Serverã€ã³ã¹ã¿ã³ã¹ãšã®ã»ãã·ã§ã³ãæåã«äœæãããŠããã«å®äºãããŸã§ãäœæè ã«ã®ã¿è¡šç€ºãããŸãã ãŠãŒã¶ãŒãSQL Serverã®ã€ã³ã¹ã¿ã³ã¹ããåæãããšãããŒã«ã«äžæããŒãã«ã¯èªåçã«åé€ãããŸãã ã°ããŒãã«äžæããŒãã«ã¯ããããã®ããŒãã«ãäœæããåŸã®æ¥ç¶ã»ãã·ã§ã³äžã«ãã¹ãŠã®ãŠãŒã¶ãŒã«è¡šç€ºããããããã®ããŒãã«ãåç §ãããã¹ãŠã®ãŠãŒã¶ãŒãSQL Serverã€ã³ã¹ã¿ã³ã¹ããåæããããšåé€ãããŸãã
äžæããŒãã«ã¯ãtempdbã·ã¹ãã ããŒã¿ããŒã¹ã«äœæãããŸãã ããããäœæããå Žåãã¡ã€ã³ããŒã¿ããŒã¹ãè©°ãŸãããŸããããã以å€ã®å ŽåãäžæããŒãã«ã¯éåžžã®ããŒãã«ãšå®å šã«åäžã§ãããDROP TABLEã³ãã³ãã䜿çšããŠåé€ããããšãã§ããŸãã å€ãã®å ŽåãããŒã«ã«ïŒïŒïŒäžæããŒãã«ã䜿çšãããŸãã
äžæããŒãã«ãäœæããã«ã¯ãCREATE TABLEã³ãã³ãã䜿çšã§ããŸãã
CREATE TABLE #Temp( ID int, Name nvarchar(30) )
MS SQLã®äžæããŒãã«ã¯éåžžã®ããŒãã«ã«äŒŒãŠãããããDROP TABLEã³ãã³ãèªäœã§é©å®åé€ããããšãã§ããŸãã
DROP TABLE #Temp
ãŸããäžæããŒãã«ïŒããã³éåžžã®ããŒãã«ïŒãäœæããSELECT ... INTOæ§æã䜿çšããŠãã¯ãšãªã«ãã£ãŠè¿ãããããŒã¿ãããã«åã蟌ãããšãã§ããŸãã
SELECT ID,Name INTO #Temp FROM Employees
ã泚æ
DBMSã«ãã£ãŠã¯ãäžæããŒãã«ã®å®è£ ãç°ãªãå ŽåããããŸãã ããšãã°ãORACLEããã³Firebird DBMSã§ã¯ãäžæããŒãã«ã®æ§é ã¯ããã®äžã®ããŒã¿ã¹ãã¬ãŒãžã®è©³çŽ°ã瀺ãCREATE GLOBAL TEMPORARY TABLEã³ãã³ãã䜿çšããŠäºåã«å®çŸ©ããå¿ èŠããããŸãããã®åŸããŠãŒã¶ãŒã¯ã¡ã€ã³ããŒãã«ã§ããã確èªããéåžžã®ããŒãã«ã®ããã«æäœããŸãã
DBæ£èŠå-ãµãããŒãã«ïŒãã³ãããã¯ïŒãžã®åå²ãšé¢ä¿ã®å®çŸ©
çŸåšã®EmployeesããŒãã«ã«ã¯ãããšãã°1人ã®åŸæ¥å¡ãš2人ç®ã®åŸæ¥å¡ã«å¯ŸããŠåã«ãITãã瀺ãããšãã§ããããããŠãŒã¶ãŒããPositionãããã³ãDepartmentããã£ãŒã«ãã«ããã¹ããå ¥åã§ãããšããæ¬ ç¹ããããŸãã ããITéšéãã3çªç®ã®ãITããå ¥åããŸãã ãã®çµæããŠãŒã¶ãŒãäœãæå³ããããã€ãŸãæ確ã«ãªããŸããã ãããã®åŸæ¥å¡ã¯1ã€ã®éšéã®åŸæ¥å¡ã§ããããããšããŠãŒã¶ãŒãèšè¿°ãããŠããããããã¯3ã€ã®ç°ãªãéšéã§ããïŒ ããã«ããã®å Žåãäžéšã®ã¬ããŒãã®ããŒã¿ãæ£ããã°ã«ãŒãåã§ããŸãããåéšéã®ã³ã³ããã¹ãã§åŸæ¥å¡æ°ã衚瀺ããå¿ èŠãããå ŽåããããŸãã
2çªç®ã®æ¬ ç¹ã¯ããã®æ å ±ã®ä¿åéãšãã®è€è£œãã€ãŸãéè€ã§ãã åŸæ¥å¡ããšã«éšéã®ãã«ããŒã ã衚瀺ãããŸããããã«ã¯ãéšéåã®åãã£ã©ã¯ã¿ãŒãæ ŒçŽããããã®ããŒã¿ããŒã¹å ã®å Žæãå¿ èŠã§ãã
3çªç®ã®æ¬ ç¹ã¯ãããšãã°ããProgrammerãã®äœçœ®ããJunior Programmerãã«å€æŽããå¿ èŠãããå Žåãªã©ãæçš¿ã®ååãå€æŽãããå Žåã«ãããã®ãã£ãŒã«ããæŽæ°ããã®ãé£ããããšã§ãã ãã®å ŽåãPositionããProgrammerãã«çããããŒãã«ã®åè¡ãå€æŽããå¿ èŠããããŸãã
ãããã®æ¬ ç¹ãåé¿ããããã«ãããŒã¿ããŒã¹ã®ããããæ£èŠåã䜿çšãããŸã-ãµãããŒãã«ãåç §ããŒãã«ã«ãããç²ç ããŸãã çè«ã®ãžã£ã³ã°ã«ã«è¡ã£ãŠæšæºåœ¢ãäœã§ããããç 究ããå¿ èŠã¯ãããŸãã;æ£èŠåã®æ¬è³ªãç解ããã ãã§ååã§ãã
åç §ãPositionsããšãDepartmentsãã®2ã€ã®ããŒãã«ãäœæããŠã¿ãŸãããã1ã€ç®ã¯Positionsã2ã€ç®ã¯ããããDepartmentsãšåŒã³ãŸãã
CREATE TABLE Positions( ID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_Positions PRIMARY KEY, Name nvarchar(30) NOT NULL ) CREATE TABLE Departments( ID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_Departments PRIMARY KEY, Name nvarchar(30) NOT NULL )
ããã§ã¯ãæ°ããIDENTITYãªãã·ã§ã³ã䜿çšããããšã«æ³šæããŠãã ãããããã¯ãIDåã®ããŒã¿ã«ã1ããå§ãŸãã1ãã€å¢åããŠèªåçã«çªå·ãä»ããããããšã瀺ããŸãã æ°ããã¬ã³ãŒããè¿œå ãããšãå€1ã2ã3ãªã©ãé çªã«å²ãåœãŠãããŸãã ãã®ãããªãã£ãŒã«ãã¯éåžžãèªåã€ã³ã¯ãªã¡ã³ããšåŒã°ããŸãã ããŒãã«ã«ã¯IDENTITYããããã£ãæã€ãã£ãŒã«ãã1ã€ã ãå®çŸ©ã§ããŸããéåžžã¯ãå¿ ããšããããã§ã¯ãããŸãããããã®ãããªãã£ãŒã«ãã¯ãã®ããŒãã«ã®äž»ããŒã§ãã
ã泚æ
ããŸããŸãªDBMSã§ãã«ãŠã³ã¿ã䜿çšãããã£ãŒã«ãã®å®è£ ã¯ç¬èªã®æ¹æ³ã§å®è¡ã§ããŸãã ããšãã°ãMySQLã§ã¯ããã®ãããªãã£ãŒã«ãã¯AUTO_INCREMENTãªãã·ã§ã³ã䜿çšããŠå®çŸ©ãããŸãã ORACLEããã³Firebirdã§ã¯ã以åã¯ãã®æ©èœã¯ã·ãŒã±ã³ã¹ïŒSEQUENCEïŒã䜿çšããŠãšãã¥ã¬ãŒãã§ããŸããã ããããORACLEã®ç¥ãéãã§ã¯ããªãã·ã§ã³GENERATED AS IDENTITYãè¿œå ãããŸããã
EmployeesããŒãã«ã®Positionãã£ãŒã«ããšDepartmentãã£ãŒã«ãã«èšé²ãããŠããçŸåšã®ããŒã¿ã«åºã¥ããŠããããã®ããŒãã«ã«èªåçã«å ¥åããŸãããã
-- Name Positions, Position Employees INSERT Positions(Name) SELECT DISTINCT Position FROM Employees WHERE Position IS NOT NULL --
DepartmentsããŒãã«ã«ã€ããŠãåãããšãè¡ããŸãã
INSERT Departments(Name) SELECT DISTINCT Department FROM Employees WHERE Department IS NOT NULL
PositionsããŒãã«ãšDepartmentsããŒãã«ãéããšãIDãã£ãŒã«ãã«ãã£ãŠçªå·ä»ãã®å€ã»ããã衚瀺ãããŸãã
SELECT * FROM Positions
ID | ãåå |
---|---|
1 | äŒèšå£« |
2 | ç£ç£ |
3 | ããã°ã©ã㌠|
4 | äžçŽããã°ã©ã㌠|
SELECT * FROM Departments
ID | ãåå |
---|---|
1 | éå¶ |
2 | ç°¿èš |
3 | IT |
ãããã®ããŒãã«ã¯ããžã§ãã®å²ãåœãŠãšéšéã®ãã£ã¬ã¯ããªã®åœ¹å²ãæãããŸãã次ã«ãæçš¿ãšéšéã®èå¥åãåç §ããŸãããŸããEmployeesããŒãã«ã«æ°ãããã£ãŒã«ããäœæããŠãèå¥åããŒã¿ãä¿åããŸãã
-- ID ALTER TABLE Employees ADD PositionID int -- ID ALTER TABLE Employees ADD DepartmentID int
åç §ãã£ãŒã«ãã®ã¿ã€ãã¯ããã£ã¬ã¯ããªå ãšåãã§ããå¿ èŠããããŸãããã®å Žåã¯ãintã§ãã
ãŸãã1ã€ã®ã³ãã³ãã§è€æ°ã®ãã£ãŒã«ããäžåºŠã«ããŒãã«ã«è¿œå ããŠãã³ã³ãã§åºåããããã£ãŒã«ãããªã¹ãããããšãã§ããŸãã
ALTER TABLE Employees ADD PositionID int, DepartmentID int
次ã«ããããã®ãã£ãŒã«ãã®ãªã³ã¯ïŒãªã³ã¯å¶é-å€éšããŒïŒãäœæããŸããããã«ããããŠãŒã¶ãŒã¯ããã£ã¬ã¯ããªå ã®IDå€ã«å«ãŸããªãå€ã«ããŒã¿ããã£ãŒã«ãã«æžã蟌ãããšãã§ããªããªããŸãã
ALTER TABLE Employees ADD CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID)
ãããŠã2çªç®ã®ãã£ãŒã«ãã«ãåãããšãè¡ããŸãã
ALTER TABLE Employees ADD CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID)
ããã§ããŠãŒã¶ãŒã¯ãããã®ãã£ãŒã«ãã«å¯Ÿå¿ãããã£ã¬ã¯ããªã®IDå€ã®ã¿ãå ¥åã§ããŸãããããã£ãŠãæ°ããéšéãŸãã¯åœ¹è·ã䜿çšããã«ã¯ããŸãé©åãªãã£ã¬ã¯ããªã«æ°ãããšã³ããªãè¿œå ããå¿ èŠããããŸãããªããªã圹è·ãšéšéã¯ãã£ã¬ã¯ããªã«1ã€ã®ã³ããŒã§ä¿åãããããã«ãªããŸãããååãå€æŽããã«ã¯ããã£ã¬ã¯ããªå ã§ã®ã¿å€æŽããã ãã§ååã§ãã
åç §å¶çŽã®ååã¯éåžžãã³ã³ããžããã§ãããæ¥é èŸãFK_ãã§æ§æãããŸãããã®åŸãããŒãã«åãç¶ããã¢ã³ããŒã¹ã³ã¢ã®åŸã«åç §ããŒãã«ã®èå¥åãåç §ãããã£ãŒã«ãã®ååãç¶ããŸãã
èå¥åïŒIDïŒã¯éåžžããªã¬ãŒã·ã§ã³ã·ãããšããã«æ ŒçŽãããå€ã«ã®ã¿äœ¿çšãããå éšå€ã§ãããã»ãšãã©ã®å Žåãå®å šã«ç¡é¢å¿ãªã®ã§ãã¬ã³ãŒãã®åé€åŸãªã©ãããŒãã«ã§ã®äœæ¥äžã«çºçããäžé£ã®æ°åã®ç©Žãåãé€ãå¿ èŠã¯ãããŸããåèæžããã
ãŸããå Žåã«ãã£ãŠã¯ããªã³ã¯ãããã€ãã®ãã£ãŒã«ãã«æŽçã§ããŸãã
ALTER TABLE ADD CONSTRAINT _ FOREIGN KEY(1,2,âŠ) REFERENCES _(1,2,âŠ)
ãã®å ŽåãããŒãã«ãreference_tableãã§ã¯ãäž»ããŒã¯ããã€ãã®ãã£ãŒã«ãã®çµã¿åããã§è¡šãããŸãïŒfield1ãfield2ã...ïŒã
å®éãããã§ãPositionIDãã£ãŒã«ããšDepartmentIDãã£ãŒã«ãããã£ã¬ã¯ããªã®IDå€ã§æŽæ°ããŸãããã®ç®çã®DMLã«ã¯UPDATEã³ãã³ãã䜿çšããŸãã
UPDATE e SET PositionID=(SELECT ID FROM Positions WHERE Name=e.Position), DepartmentID=(SELECT ID FROM Departments WHERE Name=e.Department) FROM Employees e
ãªã¯ãšã¹ããå®è¡ããŠäœãèµ·ãã£ãã®ãèŠãŠã¿ãŸãããïŒ
SELECT * FROM Employees
ID | ãåå | èªçæ¥ | ã¡ãŒã« | åœ¹è· | éšé | äœçœ®ID | DepartmentID |
---|---|---|---|---|---|---|---|
1000 | ã€ã¯ããI.I. | ãã« | ãã« | ç£ç£ | éå¶ | 2 | 1 |
1001 | ããããP.P. | ãã« | ãã« | ããã°ã©ã㌠| IT | 3 | 3 |
1002 | ã·ãããS.S. | ãã« | ãã« | äŒèšå£« | ç°¿èš | 1 | 2 |
1003 | ã¢ã³ãã¬ãšãA.A. | ãã« | ãã« | äžçŽããã°ã©ã㌠| IT | 4 | 3 |
ãã¹ãŠãPositionIDããã³DepartmentIDãã£ãŒã«ãã«ã¯ãEmployeesããŒãã«ã®Positionããã³Departmentãã£ãŒã«ãã®ããžã·ã§ã³ããã³éšéã®å¿ èŠæ§ã®èå¥åãå ¥åãããŠããŸãããããã®ãã£ãŒã«ããåé€ã§ããŸãã
ALTER TABLE Employees DROP COLUMN Position,Department
ããã§ãããŒãã«ã¯æ¬¡ã®ãã©ãŒã ãååŸããŸããã
SELECT * FROM Employees
ID | ãåå | èªçæ¥ | ã¡ãŒã« | äœçœ®ID | DepartmentID |
---|---|---|---|---|---|
1000 | ã€ã¯ããI.I. | ãã« | ãã« | 2 | 1 |
1001 | ããããP.P. | ãã« | ãã« | 3 | 3 |
1002 | ã·ãããS.S. | ãã« | ãã« | 1 | 2 |
1003 | ã¢ã³ãã¬ãšãA.A. | ãã« | ãã« | 4 | 3 |
ã€ãŸãæçµçã«ã¯åé·ãªæ å ±ãä¿åããå¿ èŠããªããªããŸãããããã§ã圹è·ãšéšçœ²ã®çªå·ã«ãã£ãŠãåç §ããŒãã«ã®å€ã䜿çšããŠäžæã«ååã決å®ã§ããŸãã
SELECT e.ID,e.Name,p.Name PositionName,d.Name DepartmentName FROM Employees e LEFT JOIN Departments d ON d.ID=e.DepartmentID LEFT JOIN Positions p ON p.ID=e.PositionID
ID | ãåå | PositionName | éšçœ²å |
---|---|---|---|
1000 | ã€ã¯ããI.I. | ç£ç£ | éå¶ |
1001 | ããããP.P. | ããã°ã©ã㌠| IT |
1002 | ã·ãããS.S. | äŒèšå£« | ç°¿èš |
1003 | ã¢ã³ãã¬ãšãA.A. | äžçŽããã°ã©ã㌠| IT |
ãªããžã§ã¯ãã€ã³ã¹ãã¯ã¿ãŒã§ã¯ããã®ããŒãã«ã§äœæããããã¹ãŠã®ãªããžã§ã¯ãã確èªã§ããŸããããããããããã®ãªããžã§ã¯ãã§ããŸããŸãªæäœãå®è¡ããããšãã§ããŸã-ããšãã°ããªããžã§ã¯ãã®ååå€æŽãåé€ã
ãŸããããŒãã«ãããèªäœãåç §ã§ããããšã«ã泚æããŠãã ãããååž°ãªã³ã¯ãäœæã§ããŸããããšãã°ãåŸæ¥å¡ãå«ãããŒãã«ã«å¥ã®ManagerIDãã£ãŒã«ããè¿œå ããŸããããã¯ããã®åŸæ¥å¡ãå ±åããåŸæ¥å¡ã瀺ããŸãããã£ãŒã«ããäœæããŸãã
ALTER TABLE Employees ADD ManagerID int
ãã®ãã£ãŒã«ãã§ã¯NULLå€ã䜿çšã§ããŸãããããšãã°ãåªç§ãªåŸæ¥å¡ãããªãå Žåããã£ãŒã«ãã¯ç©ºã«ãªããŸãã
次ã«ãEmployeesããŒãã«ã«å€éšããŒãäœæããŸãã
ALTER TABLE Employees ADD CONSTRAINT FK_Employees_ManagerID FOREIGN KEY (ManagerID) REFERENCES Employees(ID)
ããã§ã¯ããã€ã¢ã°ã©ã ãäœæããŠãããŒãã«éã®é¢ä¿ãã©ã®ããã«èŠããããèŠãŠã¿ãŸãããã
ãã®çµæã次ã®å³ã衚瀺ãããŸãïŒEmployeesããŒãã«ã¯PositionsããŒãã«ãšDepertmentsããŒãã«ã«ãªã³ã¯ãããèªèº«ãåç §ããŸãïŒïŒ
æåŸã«ãåç §ããŒè¿œå ã®ãªãã·ã§ã³ON DELETE CASCADEããã³ON UPDATE CASCADEãå«ããããšãã§ããŸãããããã®ãªãã·ã§ã³ã¯ãåç §ããŒãã«ã§åç §ãããŠããã¬ã³ãŒããåé€ãŸãã¯æŽæ°ãããšãã®åäœæ¹æ³ã«ã€ããŠèª¬æããŸãããããã®ãªãã·ã§ã³ãæå®ãããŠããªãå Žåãå¥ã®ããŒãã«ããã®ãªã³ã¯ãæã€ã¬ã³ãŒãã®ãã£ã¬ã¯ããªããŒãã«ã®IDãå€æŽããããšãããã®ã¬ã³ãŒããåç §ãããã¹ãŠã®è¡ãåé€ãããŸã§ãã£ã¬ã¯ããªãããã®ãããªãšã³ããªãåé€ããããšãã§ããŸããããã ãããããã®è¡ã®ãªã³ã¯ãå¥ã®å€ã«æŽæ°ããŸãã
ããšãã°ãFK_Employees_DepartmentIDã®ON DELETE CASCADEãªãã·ã§ã³ã䜿çšããŠããŒãã«ãåäœæããŸãã
DROP TABLE Employees CREATE TABLE Employees( ID int NOT NULL, Name nvarchar(30), Birthday date, Email nvarchar(30), PositionID int, DepartmentID int, ManagerID int, CONSTRAINT PK_Employees PRIMARY KEY (ID), CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID) ON DELETE CASCADE, CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID), CONSTRAINT FK_Employees_ManagerID FOREIGN KEY (ManagerID) REFERENCES Employees(ID) ) INSERT Employees (ID,Name,Birthday,PositionID,DepartmentID,ManagerID)VALUES (1000,N' ..','19550219',2,1,NULL), (1001,N' ..','19831203',3,3,1003), (1002,N' ..','19760607',1,2,1000), (1003,N' ..','19820417',4,3,1000)
DepartmentsããŒãã«ããID 3ã®éšéãåé€ããŸãã
DELETE Departments WHERE ID=3
EmployeesããŒãã«ã®ããŒã¿ãèŠãŠã¿ãŸãããã
SELECT * FROM Employees
ID | ãåå | èªçæ¥ | ã¡ãŒã« | äœçœ®ID | DepartmentID | ãããŒãžã£ãŒID |
---|---|---|---|---|---|---|
1000 | ã€ã¯ããI.I. | 1955-02-19 | ãã« | 2 | 1 | ãã« |
1002 | ã·ãããS.S. | 1976-06-07 | ãã« | 1 | 2 | 1000 |
ã芧ã®ãšãããEmployeesããŒãã«ã®éšé3ã®ããŒã¿ãåé€ãããŠããŸãã
ON UPDATE CASCADEãªãã·ã§ã³ã¯åæ§ã«åäœããŸããããã£ã¬ã¯ããªå ã®IDå€ãæŽæ°ãããšãã«æ©èœããŸããããšãã°ããžã§ããã£ã¬ã¯ããªã®ãžã§ãIDãå€æŽãããšãEmployeesããŒãã«ã®DepartmentIDã¯ããã£ã¬ã¯ããªã«èšå®ããæ°ããIDå€ã«æŽæ°ãããŸãããããããã®å Žåããããå®èšŒããããšã¯åã«æ©èœããŸããã DepartmentsããŒãã«ã®IDåã«ã¯IDENTITYãªãã·ã§ã³ãããã次ã®ã¯ãšãªãå®è¡ã§ããŸããïŒéšéID 3ã30ã«å€æŽïŒã
UPDATE Departments SET ID=30 WHERE ID=3
äž»ãªããšã¯ãããã2ã€ã®ãªãã·ã§ã³ON DELETE CASCADEããã³ON UPDATE CASCADEã®æ¬è³ªãç解ããããšã§ãããããã®ãªãã·ã§ã³ã¯ãã£ãã«äœ¿çšããªããããåç §å¶çŽã§æå®ããåã«æ éã«æ€èšããããšããå§ãããŸãã誀ã£ãŠãã£ã¬ã¯ããªããŒãã«ãããšã³ããªãåé€ãããšã倧ããªåé¡ã«ã€ãªãããé£éåå¿ãåŒãèµ·ããå¯èœæ§ããããŸãã
éšé3ã埩å ããŸãã
-- / IDENTITY SET IDENTITY_INSERT Departments ON INSERT Departments(ID,Name) VALUES(3,N'') -- / IDENTITY SET IDENTITY_INSERT Departments OFF
TRUNCATE TABLEã³ãã³ãã䜿çšããŠãEmployeesããŒãã«ãå®å šã«ã¯ãªã¢ããŸãã
TRUNCATE TABLE Employees
ãããŠåã³ãåã®INSERTã³ãã³ãã䜿çšããŠããŒã¿ããªããŒãããŸãã
INSERT Employees (ID,Name,Birthday,PositionID,DepartmentID,ManagerID)VALUES (1000,N' ..','19550219',2,1,NULL), (1001,N' ..','19831203',3,3,1003), (1002,N' ..','19760607',1,2,1000), (1003,N' ..','19820417',4,3,1000)
ãŸãšãããš
çŸæç¹ã§ã¯ãããã«ããã€ãã®DDLã³ãã³ããç¥èã«è¿œå ãããŠããŸãã
- ãã£ãŒã«ãã«IDENTITYããããã£ãè¿œå -ãã®ãã£ãŒã«ããããŒãã«ã®èªåå ¥åïŒã«ãŠã³ã¿ãŒãã£ãŒã«ãïŒã«ããããšãã§ããŸãã
- ALTER TABLE _ ADD ___ â ;
- ALTER TABLE _ DROP COLUMN _ â ;
- ALTER TABLE _ ADD CONSTRAINT _ FOREIGN KEY () REFERENCES _() â .
â UNIQUE, DEFAULT, CHECK
UNIQUEå¶çŽã䜿çšãããšãç¹å®ã®ãã£ãŒã«ããŸãã¯äžé£ã®ãã£ãŒã«ãã®åè¡ã®å€ã¯äžæã§ããå¿ èŠããããšèšããŸããEmployeesããŒãã«ã®å ŽåãEmailãã£ãŒã«ãã«ãã®ãããªå¶éã課ãããšãã§ããŸããå€ããŸã å®çŸ©ãããŠããªãå Žåã®ã¿ãã¡ãŒã«ã«å€ãäºåå ¥åããŸãã
UPDATE Employees SET Email='i.ivanov@test.tt' WHERE ID=1000 UPDATE Employees SET Email='p.petrov@test.tt' WHERE ID=1001 UPDATE Employees SET Email='s.sidorov@test.tt' WHERE ID=1002 UPDATE Employees SET Email='a.andreev@test.tt' WHERE ID=1003
ãããŠä»ãããªãã¯ãã®ãã£ãŒã«ãã«å¶çŽã®äžææ§ã課ãããšãã§ããŸãïŒ
ALTER TABLE Employees ADD CONSTRAINT UQ_Employees_Email UNIQUE(Email)
ããã§ããŠãŒã¶ãŒã¯è€æ°ã®åŸæ¥å¡ããåãé»åã¡ãŒã«ãå ¥åã§ããªããªããŸãã
äžææ§å¶çŽã¯éåžžã次ã®ããã«åç §ãããŸããæåã«æ¥é èŸãUQ_ãã次ã«ããŒãã«åãç¶ããã¢ã³ããŒã¹ã³ã¢ã®åŸã«ãã®å¶éã課ããããã£ãŒã«ãã®ååããããŸãã
ãããã£ãŠããã£ãŒã«ãã®çµã¿åãããããŒãã«è¡ã®ã³ã³ããã¹ãã§äžæã§ãªããã°ãªããªãå Žåãããããã³ã³ãã§åºåã£ãŠãªã¹ãããŸãã
ALTER TABLE _ ADD CONSTRAINT _ UNIQUE(1,2,âŠ)
DEFAULTå¶çŽããã£ãŒã«ãã«è¿œå ããããšã«ãããæ°ããã¬ã³ãŒããæ¿å ¥ãããšãã«ãã®ãã£ãŒã«ããINSERTã³ãã³ããã£ãŒã«ããªã¹ãã«ãªã¹ããããŠããªãå Žåã«çœ®æãããããã©ã«ãå€ãèšå®ã§ããŸãããã®å¶éã¯ãããŒãã«ã®äœææã«çŽæ¥èšå®ã§ããŸãã
æ°ãããã£ãŒã«ããååæ¥ããããŒãã«Employeesã«è¿œå ããŠHireDateãšããååãä»ãããã®ãã£ãŒã«ãã®ããã©ã«ãå€ãçŸåšã®æ¥ä»ã«ãªããšããŸãããã
ALTER TABLE Employees ADD HireDate date NOT NULL DEFAULT SYSDATETIME()
ãŸãã¯ãHireDateåãæ¢ã«ååšããå Žåã次ã®æ§æã䜿çšã§ããŸãã
ALTER TABLE Employees ADD DEFAULT SYSDATETIME() FOR HireDate
ããã§ã¯ãå¶éã®ååã瀺ããŸããã§ãããDEFAULTã®å Žåãããã¯ããã»ã©éèŠã§ã¯ãªããšããæèŠããããŸãããããããããªããè¯ãæ¹æ³ã§ããããããªããç§ã¯ããªããæ beã§ããå¿ èŠã¯ãªããšæãã®ã§ãéåžžã®ååãèšå®ãã¹ãã§ããããã¯æ¬¡ã®ããã«è¡ãããŸãã
ALTER TABLE Employees ADD CONSTRAINT DF_Employees_HireDate DEFAULT SYSDATETIME() FOR HireDate
以åã«ãã®ãããªåããªãã£ããããåã¬ã³ãŒãã«è¿œå ãããšãçŸåšã®æ¥ä»å€ãHireDateãã£ãŒã«ãã«æ¿å ¥ãããŸãã
ãã¡ãããæ瀺çã«èšå®ããªãå Žåãã€ãŸãæ°ããã¬ã³ãŒããè¿œå ãããšãã«ãçŸåšã®æ¥ä»ãèªåçã«æ¿å ¥ãããŸããåã®ãªã¹ãã§æå®ããªãã§ãã ãããè¿œå ãããå€ã®ãªã¹ãã§HireDateãã£ãŒã«ããæå®ããã«ããããäŸã§ç€ºããŸãããã
INSERT Employees(ID,Name,Email)VALUES(1004,N' ..','s.sergeev@test.tt')
äœãèµ·ãã£ãã®ãèŠãŠã¿ãŸãããïŒ
SELECT * FROM Employees
ID | ãåå | èªçæ¥ | ã¡ãŒã« | äœçœ®ID | DepartmentID | ãããŒãžã£ãŒID | éã |
---|---|---|---|---|---|---|---|
1000 | ã€ã¯ããI.I. | 1955-02-19 | i.ivanov@test.tt | 2 | 1 | ãã« | 2015-04-08 |
1001 | ããããP.P. | 1983-12-03 | p.petrov@test.tt | 3 | 4 | 1003 | 2015-04-08 |
1002 | ã·ãããS.S. | 1976-06-07 | s.sidorov@test.tt | 1 | 2 | 1000 | 2015-04-08 |
1003 | ã¢ã³ãã¬ãšãA.A. | 1982-04-17 | a.andreev@test.tt | 4 | 3 | 1000 | 2015-04-08 |
1004 | Sergeev S.S. | ãã« | s.sergeev@test.tt | ãã« | ãã« | ãã« | 2015-04-08 |
CHECKãã§ãã¯å¶çŽã¯ããã£ãŒã«ãã«æ¿å ¥ãããå€ããã§ãã¯ããå¿ èŠãããå Žåã«äœ¿çšãããŸããããšãã°ãåŸæ¥å¡èå¥åïŒIDïŒã§ããåŸæ¥å¡çªå·ãã£ãŒã«ãã«ãã®å¶éã課ããŠããŸãããã®å¶éã䜿çšããŠãåŸæ¥å¡çªå·ã«ã¯1000ã1999ã®å€ãå¿ èŠã§ãããšèšããŸãã
ALTER TABLE Employees ADD CONSTRAINT CK_Employees_ID CHECK(ID BETWEEN 1000 AND 1999)
éåžžãå¶éã«ã¯åãååãä»ããããæåã«ãã¬ãã£ãã¯ã¹ãCK_ãã次ã«ããŒãã«åãšãã®å¶éãé©çšããããã£ãŒã«ãã®ååãä»ããããŸãã
ç¡å¹ãªãšã³ããªãæ¿å ¥ããŠãå¶éãæ©èœããããšã確èªããŠã¿ãŸãããïŒå¯Ÿå¿ãããšã©ãŒã衚瀺ãããŸãïŒã
INSERT Employees(ID,Email) VALUES(2000,'test@test.tt')
æ¿å ¥ãããå€ã1500ã«å€æŽããã¬ã³ãŒããæ¿å ¥ãããŠããããšã確èªããŸãã
INSERT Employees(ID,Email) VALUES(1500,'test@test.tt')
ååãæå®ããã«UNIQUEããã³CHECKå¶çŽãäœæããããšãã§ããŸãã
ALTER TABLE Employees ADD UNIQUE(Email) ALTER TABLE Employees ADD CHECK(ID BETWEEN 1000 AND 1999)
ããããããã¯è¯ãç¿æ £ã§ã¯ãªããå¶éã®ååãæ瀺çã«èšå®ããæ¹ãè¯ãã§ããåŸã§ããå°é£ã«ãªããã®ãææ¡ããã«ã¯ããªããžã§ã¯ããéããŠããã®åå ã確èªããå¿ èŠããããŸãã
é©åãªååãä»ãããšãå¶éã«é¢ããå€ãã®æ å ±ããã®ååã§çŽæ¥èŠã€ããããšãã§ããŸãã
ãããŠãããã«å¿ããŠãããŒãã«ããŸã ååšããªãå Žåã¯ãããŒãã«ãäœæãããšãã«ããããã¹ãŠã®å¶éãããã«äœæã§ããŸããããŒãã«ãåé€ããŸãã
DROP TABLE Employees
ãããŠã1ã€ã®CREATE TABLEã³ãã³ãã«ãã£ãŠäœæããããã¹ãŠã®å¶éã䜿çšããŠåäœæããŸãã
CREATE TABLE Employees( ID int NOT NULL, Name nvarchar(30), Birthday date, Email nvarchar(30), PositionID int, DepartmentID int, HireDate date NOT NULL DEFAULT SYSDATETIME(), -- DEFAULT CONSTRAINT PK_Employees PRIMARY KEY (ID), CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID), CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID), CONSTRAINT UQ_Employees_Email UNIQUE (Email), CONSTRAINT CK_Employees_ID CHECK (ID BETWEEN 1000 AND 1999) )
æåŸã«ãåŸæ¥å¡ã®ããŒãã«ã«æ¿å ¥ããŸãã
INSERT Employees (ID,Name,Birthday,Email,PositionID,DepartmentID)VALUES (1000,N' ..','19550219','i.ivanov@test.tt',2,1), (1001,N' ..','19831203','p.petrov@test.tt',3,3), (1002,N' ..','19760607','s.sidorov@test.tt',1,2), (1003,N' ..','19820417','a.andreev@test.tt',4,3)
PRIMARY KEYããã³UNIQUEå¶çŽãäœæãããšãã«äœæãããã€ã³ããã¯ã¹ã«ã€ããŠå°ã
äžèšã®ã¹ã¯ãªãŒã³ã·ã§ããã§ãããããã«ãPRIMARY KEYå¶çŽãšUNIQUEå¶çŽãäœæãããšãåãååïŒPK_EmployeesãšUQ_Employees_EmailïŒã®ã€ã³ããã¯ã¹ãèªåçã«äœæãããŸãããããã©ã«ãã§ã¯ãäž»ããŒã®ã€ã³ããã¯ã¹ã¯CLUSTEREDãšããŠäœæãããä»ã®ãã¹ãŠã®ã€ã³ããã¯ã¹ã®ã€ã³ããã¯ã¹ã¯NONCLUSTEREDãšããŠäœæãããŸããã¯ã©ã¹ã¿ã€ã³ããã¯ã¹ã®æŠå¿µã¯ããã¹ãŠã®DBMSã«ããããã§ã¯ãªãããšãèšã£ãŠãã䟡å€ããããŸããããŒãã«ã«ã¯ã1ã€ã®ã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ïŒCLUSTEREDïŒã®ã¿ãå«ããããšãã§ããŸãã CLUSTERED-ããŒãã«ãšã³ããªããã®ã€ã³ããã¯ã¹ã«ãã£ãŠãœãŒããããããšãæå³ããŸãããŸãããã®ã€ã³ããã¯ã¹ã¯ããŒãã«å ã®ãã¹ãŠã®ããŒã¿ã«çŽæ¥ã¢ã¯ã»ã¹ã§ãããšèšãããšãã§ããŸããããã¯ãããã°ããŒãã«ã®ã¡ã€ã³ã€ã³ããã¯ã¹ã§ããããã«å€§ãŸãã«èšãã°ãããã¯ããŒãã«ã«ãã«ãã§åºå®ãããã€ã³ããã¯ã¹ã§ããã¯ã©ã¹ã¿åã€ã³ããã¯ã¹ã¯ãã¯ãšãªã®æé©åã«åœ¹ç«ã€éåžžã«åŒ·åãªããŒã«ã§ããçŸæç¹ã§ã¯ããããèŠããŠãããŠãã ãããèšããããªãã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ããã©ã€ããªããŒã§ã¯ãªããå¥ã®ã€ã³ããã¯ã¹ã«äœ¿çšãããããã«ããã©ã€ããªããŒãäœæãããšãã«ãNONCLUSTEREDãªãã·ã§ã³ãæå®ããå¿ èŠããããŸãã
ALTER TABLE _ ADD CONSTRAINT _ PRIMARY KEY NONCLUSTERED(1,2,âŠ)
ããšãã°ãPK_Employeeså¶éã€ã³ããã¯ã¹ãã¯ã©ã¹ã¿ãŒåãããUQ_Employees_Emailå¶éã€ã³ããã¯ã¹ãã¯ã©ã¹ã¿ãŒåããŸãããããŸãããããã®å¶éãåé€ããŸãã
ALTER TABLE Employees DROP CONSTRAINT PK_Employees ALTER TABLE Employees DROP CONSTRAINT UQ_Employees_Email
次ã«ãCLUSTEREDããã³NONCLUSTEREDãªãã·ã§ã³ã䜿çšããŠããããäœæããŸãã
ALTER TABLE Employees ADD CONSTRAINT PK_Employees PRIMARY KEY NONCLUSTERED (ID) ALTER TABLE Employees ADD CONSTRAINT UQ_Employees_Email UNIQUE CLUSTERED (Email)
ããã§ãEmployeesããŒãã«ãããã§ããããåŸããšã³ããªãUQ_Employees_Emailã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ã§ãœãŒããããŠããããšãããããŸãã
SELECT * FROM Employees
ID | ãåå | èªçæ¥ | ã¡ãŒã« | äœçœ®ID | DepartmentID | éã |
---|---|---|---|---|---|---|
1003 | ã¢ã³ãã¬ãšãA.A. | 1982-04-17 | a.andreev@test.tt | 4 | 3 | 2015-04-08 |
1000 | ã€ã¯ããI.I. | 1955-02-19 | i.ivanov@test.tt | 2 | 1 | 2015-04-08 |
1001 | ããããP.P. | 1983-12-03 | p.petrov@test.tt | 3 | 3 | 2015-04-08 |
1002 | ã·ãããS.S. | 1976-06-07 | s.sidorov@test.tt | 1 | 2 | 2015-04-08 |
ãã以åã¯ãPK_Employeesã€ã³ããã¯ã¹ãã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã§ãã£ãå Žåãã¬ã³ãŒãã¯ããã©ã«ãã§IDãã£ãŒã«ãã§ãœãŒããããŠããŸããã
ãã ãããã®å Žåãããã¯ã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ã®æ¬è³ªã瀺ãåãªãäŸã§ããã»ãšãã©ã®å ŽåãEmployeesããŒãã«ãžã®ã¯ãšãªã¯IDãã£ãŒã«ãã§è¡ãããå Žåã«ãã£ãŠã¯ããã£ã¬ã¯ããªèªäœãšããŠæ©èœããããšããããŸãã
ãã£ã¬ã¯ããªã®å Žåãéåžžãã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ã¯äž»ããŒã«åºã¥ããŠæ§ç¯ããããšããå§ãããŸãããªã¯ãšã¹ãã§ã¯ããã£ã¬ã¯ããªã®èå¥åãåç §ããŠãããšãã°ååïŒåœ¹è·ãéšçœ²ïŒãååŸããããšããããããŸããããã§ãã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãããŒãã«ã®è¡ã«çŽæ¥ã¢ã¯ã»ã¹ã§ããããšãäžã§æžããããšãæãåºããŠãã ãããè¿œå ã®ãªãŒããŒããããªãã§ä»»æã®åã®å€ãååŸã§ããŸãã
ã¯ã©ã¹ã¿åã€ã³ããã¯ã¹ã¯ãéžæãæãé »ç¹ã«è¡ããããã£ãŒã«ãã«é©çšãããšæçã§ãã
ãµãã²ãŒããã£ãŒã«ãã®ããŒãã«ã«ããŒãäœæãããå ŽåããããŸãããã®å Žåãããé©åãªã€ã³ããã¯ã¹ã®CLUSTEREDã€ã³ããã¯ã¹ãªãã·ã§ã³ãä¿åãããµãã²ãŒãäž»ããŒãäœæãããšãã«NONCLUSTEREDãªãã·ã§ã³ãæå®ãããšäŸ¿å©ã§ãã
ãŸãšãããš
ãã®æ®µéã§ããALTER TABLE table_name ADD CONSTRAINT constraint_name ...ããšãã圢åŒã®ã³ãã³ãã«ãã£ãŠäœæããããæãåçŽãªåœ¢åŒã®ããããçš®é¡ã®å¶çŽã«ééããŸããã
- äž»ã㌠-äž»ããŒã
- å€éšã㌠-ãªã³ã¯ã®èšå®ãšããŒã¿ã®åç §æŽåæ§ã®å¶åŸ¡ã
- äžæ -äžææ§ãäœæã§ããŸãã
- CHECK-å ¥åãããããŒã¿ã®æ£ç¢ºæ§ãèæ ®ããŸãã
- DEFAULT-ããã©ã«ãå€ãèšå®ã§ããŸãã
- ãŸãããã¹ãŠã®å¶éãã³ãã³ã«䜿çšããŠé€å»ããããšãã§ããããšã¯æ³šç®ã«å€ããALTER TABLEæ table_nameã®DROP CONSTRAINTã® CONSTRAINT_NAMEããã
æã ã¯ãŸããéšåçã«è§£äœã€ã³ããã¯ã¹ãšã¯ã©ã¹ã¿æŠå¿µïŒè§ŠãCLUSTEREDïŒãšéã¯ã©ã¹ã¿ïŒNONCLUSTEREDïŒã€ã³ããã¯ã¹ã
ã«ã¹ã¿ã ã€ã³ããã¯ã¹ã®äœæ
ç¬ç«ãšã¯ãPRIMARY KEYãŸãã¯UNIQUEãå¶éããããã«äœæãããªãã€ã³ããã¯ã¹ãæå³ããŸãã
次ã®ã³ãã³ãã䜿çšããŠããã£ãŒã«ãããšã®ã€ã³ããã¯ã¹ãäœæã§ããŸãã
CREATE INDEX IDX_Employees_Name ON Employees(Name)
ããã§CLUSTEREDãNONCLUSTEREDãUNIQUEãªãã·ã§ã³ãæå®ããããšãã§ããŸãããŸããåã ã®ASCãã£ãŒã«ãïŒããã©ã«ãïŒãŸãã¯DESCã®ãœãŒãæ¹åãæå®ããããšãã§ããŸãã
CREATE UNIQUE NONCLUSTERED INDEX UQ_Employees_EmailDesc ON Employees(Email DESC)
éã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ãäœæããå ŽåãNONCLUSTEREDãªãã·ã§ã³ã¯æ¬¡ã®ããã«è§£æŸã§ããŸããããã¯ããã©ã«ãã§æ瀺ãããŠããŸããããã§ã¯ãã³ãã³ãå ã®CLUSTEREDãŸãã¯NONCLUSTEREDãªãã·ã§ã³ã®äœçœ®ã瀺ãããã«ç€ºãããŠããŸãã
次ã®ã³ãã³ãã§ã€ã³ããã¯ã¹ãåé€ã§ããŸãã
DROP INDEX IDX_Employees_Name ON Employees
å¶çŽãªã©ã®åçŽãªã€ã³ããã¯ã¹ã¯ãCREATE TABLEã³ãã³ãã®ã³ã³ããã¹ãã§äœæã§ããŸãã
ããšãã°ãããŒãã«ãå床åé€ããŸãã
DROP TABLE Employees
ãããŠãäœæããããã¹ãŠã®å¶éãšã€ã³ããã¯ã¹ã1ã€ã®CREATE TABLEã³ãã³ãã§åäœæããŸãã
CREATE TABLE Employees( ID int NOT NULL, Name nvarchar(30), Birthday date, Email nvarchar(30), PositionID int, DepartmentID int, HireDate date NOT NULL CONSTRAINT DF_Employees_HireDate DEFAULT SYSDATETIME(), ManagerID int, CONSTRAINT PK_Employees PRIMARY KEY (ID), CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID), CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID), CONSTRAINT FK_Employees_ManagerID FOREIGN KEY (ManagerID) REFERENCES Employees(ID), CONSTRAINT UQ_Employees_Email UNIQUE(Email), CONSTRAINT CK_Employees_ID CHECK(ID BETWEEN 1000 AND 1999), INDEX IDX_Employees_Name(Name) )
æåŸã«ãåŸæ¥å¡ã®ããŒãã«ã«æ¿å ¥ããŸãã
INSERT Employees (ID,Name,Birthday,Email,PositionID,DepartmentID,ManagerID)VALUES (1000,N' ..','19550219','i.ivanov@test.tt',2,1,NULL), (1001,N' ..','19831203','p.petrov@test.tt',3,3,1003), (1002,N' ..','19760607','s.sidorov@test.tt',1,2,1000), (1003,N' ..','19820417','a.andreev@test.tt',4,3,1000)
ããã«ãå€ãINCLUDEã§æå®ããããšã«ãããå€ãéã¯ã©ã¹ã¿ãŒåã€ã³ããã¯ã¹ã«å«ããããšãã§ããããšã«æ³šæããŠãã ããã ã€ãŸããã®å ŽåãINCLUDEã€ã³ããã¯ã¹ã¯ã¯ã©ã¹ã¿ãŒã€ã³ããã¯ã¹ã«äŒŒãŠãããã€ã³ããã¯ã¹ãããŒãã«ã«ãã«ãã§åºå®ããããå¿ èŠãªå€ãã€ã³ããã¯ã¹ã«ãã«ãã§åºå®ãããŸãããããã£ãŠããã®ãããªã€ã³ããã¯ã¹ã¯ãã¯ãšãªã¯ãšãªïŒSELECTïŒã®ããã©ãŒãã³ã¹ãå€§å¹ ã«åäžãããããšãã§ããŸãããªã¹ãããããã¹ãŠã®ãã£ãŒã«ããã€ã³ããã¯ã¹å ã«ããå ŽåãããããããŒãã«ã«ãŸã£ããã¢ã¯ã»ã¹ããå¿ èŠã¯ãããŸããããã ããããã«ãããã€ã³ããã¯ã¹ã®ãµã€ãºãèªç¶ã«å¢å ããŸãããªã¹ãããããã£ãŒã«ãã®å€ã¯ã€ã³ããã¯ã¹ã§è€è£œãããŸãã
MSDNããã®ã¯ãªããã³ã°ãã€ã³ããã¯ã¹ãäœæããããã®äžè¬çãªæ§æ
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name ON <object> ( column [ ASC | DESC ] [ ,...n ] ) [ INCLUDE ( column_name [ ,...n ] ) ]
ãŸãšãããš
ã€ã³ããã¯ã¹ã䜿çšãããšãããŒã¿ãµã³ããªã³ã°ïŒSELECTïŒã®é床ãåäžããŸãããã€ã³ããã¯ã¹ã䜿çšãããšãããŒãã«ããŒã¿ã®å€æŽçãäœäžããŸããåå€æŽåŸãã·ã¹ãã ã¯ç¹å®ã®ããŒãã«ã®ãã¹ãŠã®ã€ã³ããã¯ã¹ãåæ§ç¯ããå¿ èŠããããŸãã
ãããã®å Žåãããµã³ãã«ã®ããã©ãŒãã³ã¹ãšããŒã¿ã®å€æŽã®äž¡æ¹ãæšæºã«éããããã«ãæé©ãªãœãªã¥ãŒã·ã§ã³ã§ãããŽãŒã«ãã³å¹³åãèŠã€ããããšããå§ãããŸããã€ã³ããã¯ã¹ãšãã®æ°ãäœæããããã®æŠç¥ã¯ãããŒãã«å ã®ããŒã¿ãå€æŽãããé »åºŠãªã©ãå€ãã®èŠå ã«äŸåããŸãã
DDLã®çµè«
ã芧ã®ãšãããDDLèšèªã¯ãäžèŠãããšæããã»ã©è€éã§ã¯ãããŸãããããã§ã¯ã3ã€ã®ããŒãã«ã ãã§åäœããåºæ¬æ§é ã®ã»ãŒãã¹ãŠã衚瀺ã§ããŸããã
äž»ãªããšã¯æ¬è³ªãç解ããããšã§ãããæ®ãã¯å®è·µã®åé¡ã§ãã
SQLãšåŒã°ãããã®ãã°ãããèšèªãåŠã¶ã®ã¯å¹žéã§ãã
ããŒã2-habrahabr.ru/post/255523