Banner

My Tech Blog (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)

์˜ค๋Š˜์˜ ๋ช…์–ธ
" ๊ตญ๊ฐ€๊ฐ€ ๋‹น์‹ ์„ ์œ„ํ•ด ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์„์ง€ ๋ฌป์ง€ ๋ง๊ณ , ๋‹น์‹ ์ด ๊ตญ๊ฐ€๋ฅผ ์œ„ํ•ด ๋ฌด์—‡์„ ํ•  ์ˆ˜ ์žˆ์„์ง€ ๋ฌผ์–ด๋ผ. "
- ์กด F. ์ผ€๋„ค๋”” (๋ฏธ๊ตญ 35๋Œ€ ๋Œ€ํ†ต๋ น)
SQL (Strunctured Query Language) ๋ถ„๋ฅ˜ โ‘  ๋ฐ์ดํ„ฐ ์ •์˜์–ด (DDL: Data Definition Language)โ†’ ๋…ผ๋ฆฌ/๋ฌผ๋ฆฌ์  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์ •์˜CREATE (์ƒ์„ฑ)CREATE DOMAIN / SCHEMA / TABLE / VIEW / INDEX โ†’ ์ƒ์„ฑALTER (๋ณ€๊ฒฝ)TABLE ์ด๋ฆ„ ๋ณ€๊ฒฝ โ†’ ALTER TABLE / ์ปฌ๋Ÿผ ์ถ”๊ฐ€DROP (์‚ญ์ œ)DROP DOMAIN / SCHEMA / TABLE / VIEW / INDEX โ†’ ์‚ญ์ œ* CASCADE : ์ฐธ์กฐํ•˜๋Š” ๋ชจ๋“  ๊ฐœ์ฒด ํ•จ๊ป˜ ์ œ๊ฑฐ* RESTRICTED : ์ œ๊ฑฐํ•  ์š”์†Œ๋ฅผ ๋‹ค๋ฅธ ๊ฐœ์ฒด๊ฐ€ ์ฐธ์กฐ ์‹œ ์ œ๊ฑฐ ์ทจ์†Œ  โ‘ก ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด (DML : Data MAnipulation Language)SELECT (๊ฒ€์ƒ‰)SELECT FROM ํ…Œ์ด๋ธ”๋ช… ..
โ–  ์ •๊ทœํ™”์ด์ƒ(Anomaly) ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ์ค‘๋ณต์„ฑ/์ข…์†์„ฑ ์ตœ์†Œํ™” ํ•˜๊ธฐ ์œ„ํ•œ ์ž‘์—…๋…ผ๋ฆฌ์  ์„ค๊ณ„ ๋‹จ๊ณ„์—์„œ ์ˆ˜ํ–‰, ์†์„ฑ ์ˆ˜๊ฐ€ ์ ์€ ํ…Œ์ด๋ธ”๋กœ ๋ถ„ํ• ๋˜์–ด ๊ด€๋ฆฌ๊ฐ€ ์šฉ์ดํ•ด์ง๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์•ˆ์ •์„ฑ ์ตœ๋Œ€ํ™” / ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์‹œ ๋ฆด๋ ˆ์ด์…˜ ์žฌ๊ตฌ์„ฑ ํ•„์š” ์ตœ์†Œํ™” โ–  ์ด์ƒ ํ˜„์ƒ ์ข…๋ฅ˜์‚ฝ์ž… ์ด์ƒ : ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์‹œ ๋ถˆํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•จ๊ป˜ ์‚ฝ์ž…์‚ญ์ œ ์ด์ƒ : ํŠœํ”Œ ์‚ญ์ œ ์‹œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ์‚ญ์ œ๊ฐฑ์‹  ์ด์ƒ : ์ผ๋ถ€๋งŒ ์ˆ˜์ •๋˜์–ด ๋ฐ์ดํ„ฐ ๋ถˆ์ผ์น˜ โ†’ ์ •๋ณด ๋ชจ์ˆœ ๋ฐœ์ƒ โ–  ์ •๊ทœํ™” ๊ณผ์ • ์™ธ์šฐ๋Š” ๋ฒ• : ๋„๋ถ€์ด๊ฒฐ๋‹ค์กฐ(๋‘๋ถ€ ์ด๊ฑธ ๋‹ค์ค˜)์ œ 1 ์ •๊ทœํ˜•๋ชจ๋“  ๋„๋ฉ”์ธ(Domain)์ด ์›์ž ๊ฐ’๋งŒ์œผ๋กœ ๋˜์–ด ์žˆ์Œ์ œ 2 ์ •๊ทœํ˜•- ๊ธฐ๋ณธํ‚ค๊ฐ€ ์•„๋‹Œ ์†์„ฑ์ด ๊ธฐ๋ณธํ‚ค์— ๋Œ€ํ•œ ์™„์ „ ํ•จ์ˆ˜์  ์ข…์† ๋งŒ์กฑ- ๋ถ€๋ถ„์  ํ•จ์ˆ˜ ์ข…์†์„ ์ œ๊ฑฐํ•œ ์ •๊ทœํ˜•์ œ 3 ์ •๊ทœํ˜•๊ธฐ๋ณธํ‚ค๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ์†์„ฑ์ด ๊ธฐ๋ณธํ‚ค..
๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ์ •๋ฆฌํ•œ ๊ฒƒ  โ–  ์Šคํ‚ค๋งˆ์™ธ๋ถ€ ์Šคํ‚ค๋งˆ(์„œ๋ธŒ์Šคํ‚ค๋งˆ)์‚ฌ์šฉ์ž ๊ด€์ ์˜ ์Šคํ‚ค๋งˆ โ†’ ํ•˜๋‚˜์˜ DB์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์™ธ๋ถ€ ์Šคํ‚ค๋งˆ๊ฐ€ ์กด์žฌ์‚ฌ์šฉ์ž, ํ”„๋กœ๊ทธ๋žจ๋งˆ๋‹ค ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๋…ผ๋ฆฌ์  ๊ตฌ์กฐ๋กœ ์กด์žฌ๊ฐœ๋… ์Šคํ‚ค๋งˆ์‚ฌ์šฉ์ž์™€ DB ๊ด€๋ฆฌ์ž ๊ด€์ ์˜ ์Šคํ‚ค๋งˆ / DB์˜ ์ „์ฒด์ ์ธ ๋…ผ๋ฆฌ์  ๊ตฌ์กฐ์ผ๋ฐ˜์ ์œผ๋กœ ํ•˜๋‚˜์˜ DB์—๋Š” ํ•˜๋‚˜์˜ ๊ฐœ๋… ์Šคํ‚ค๋งˆ ์กด์žฌโ†’ ๋ฐ์ดํ„ฐ ๊ฐœ์ฒด/๊ด€๊ณ„/์ œ์•ฝ์กฐ๊ฑด/์ ‘๊ทผ๊ถŒํ•œ/๋ฌด๊ฒฐ์„ฑ ๊ทœ์น™ ๋ช…์„ธ๋‚ด๋ถ€ ์Šคํ‚ค๋งˆDB ์„ค๊ณ„์ž/๊ฐœ๋ฐœ์ž ๊ด€์ ์˜ ์Šคํ‚ค๋งˆ๊ฐœ๋… ์Šคํ‚ค๋งˆ๋ฅผ ๋ฌผ๋ฆฌ์  ์ €์žฅ์žฅ์น˜์— ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜โ†’ ๋ฌผ๋ฆฌ์  ๊ตฌ์กฐ / ๋‚ด๋ถ€ ๋ ˆ์ฝ”๋“œ์˜ ๋ฌผ๋ฆฌ์  ์ˆœ์„œ   โ–  ๋ฐ์ดํ„ฐ ์–ธ์–ดDDL(Data Definition Language)๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ/์ œ์•ฝ ์กฐ๊ฑด ์ •์˜DML(Data Manipulation ~)๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ/์กฐ์ž‘์— ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ดDCL(Data Control ~..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…๐Ÿ˜‰ 2. ๋ฌธ์ œ ์š”์•ฝ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑดPATIENTDOCTORAPPOINTMENTํ™˜์ž ์ •๋ณด์˜์‚ฌ ์ •๋ณด์ง„๋ฃŒ ์˜ˆ์•ฝ ๋ชฉ๋กPT_NO, PT_NAME, GEND_CD, AGE, TLNODR_NAME, DR_ID, LCNS_NO, HIRE_YMD, MCDP_CD, TLNOAPNT_YMD, APNT_NO, PT_NO, MCDP_CD, MDDR_ID, APNT_CNCL_YN, APNT_CNCL_YMDํ™˜์ž๋ฒˆํ˜ธ, ํ™˜์ž์ด๋ฆ„, ์„ฑ๋ณ„์ฝ”๋“œ, ๋‚˜์ด, ์ „ํ™”๋ฒˆํ˜ธ์˜์‚ฌ์ด๋ฆ„, ์˜์‚ฌID, ๋ฉดํ—ˆ๋ฒˆํ˜ธ, ๊ณ ์šฉ์ผ์ž, ์ง„๋ฃŒ๊ณผ์ฝ”๋“œ, ์ „ํ™”๋ฒˆํ˜ธ์ง„๋ฃŒ ์˜ˆ์•ฝ์ผ์‹œ, ์ง„๋ฃŒ์˜ˆ์•ฝ๋ฒˆํ˜ธ, ํ™˜์ž๋ฒˆํ˜ธ, ์ง„๋ฃŒ๊ณผ์ฝ”๋“œ, ์˜์‚ฌID, ์˜ˆ์•ฝ์ทจ์†Œ์—ฌ๋ถ€, ์˜ˆ์•ฝ์ทจ์†Œ๋‚ ์งœ ๋ฌธ์ œ์ชผ๊ฐœ๊ธฐโœ… 2022๋…„ 4์›” 13์ผ AP.APNT_YMD LIKE '2022-04-13%'โœ… ์ทจ์†Œ๋˜์ง€ ์•Š์€..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โŒ 2. ์‹คํŒจํ•œ ์‹œ๋„์œ„์น˜ํ‹€๋ฆฐ๋ถ€๋ถ„๋งž๋Š” ์ฟผ๋ฆฌ์„ค๋ช…SELECTAVERAGEAVG()ํ‰๊ท ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜AVERAGE()๊ฐ€ ์•„๋‹ˆ๊ณ AVG()์ž„ YEAR(YM)YEAR(YM) AS `YEAR`๋ณ„์นญ ์จ์•ผ ํ•จ์ปฌ๋Ÿผ๋ช… YEAR๋กœ ์ถœ๋ ฅ ROUND(AVG(PM_VAL1),3) ROUND(AVG(PM_VAL1),2)์†Œ์ˆ˜์…‹์งธ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜๋ ค๋ฉด ๋‘˜์งธ์ž๋ฆฌ๊นŒ์ง€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜ํƒ€๋‚˜์•ผ ํ•˜๋‹ˆ๊นŒROUND(์ปฌ๋Ÿผ๋ช…, 2)๋กœ ํ•ด์•ผ ํ•จWHERELocation2 IS '์ˆ˜์›'Location2 = '์ˆ˜์›'IS๋Š” NULL ๊ฐ’๊ณผ์˜ ๋น„๊ต์—์„œ ๋งŒ ์‚ฌ์šฉ๋จORDER BYYEAR(YM)YEARSQL์˜ ์‹คํ–‰์ˆœ์„œ๋Š”ORDER BY์ ˆ์ด ๊ฐ€์žฅ๋งˆ์ง€๋ง‰์— ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—ALIAS ๋ช…์œผ๋กœ ์จ์ค˜๋„ ๋œ๋‹ค๊ผญ ๋ณ„์นญ ์จ์•ผํ•˜๋Š” ๊ฑด ์•„๋‹˜ SELECT YEAR(YM) AS YEAR,..
๐Ÿ“‘ 1. ๋ฃจ์‹œ์™€ ์—˜๋ผ ์ฐพ๊ธฐ โญ ์ •๋‹ต์ฝ”๋“œSELECT ANIMAL_ID, NAME, SEX_UPON_INTAKEFROM ANIMAL_INSWHERE NAME IN ('Lucy','Ella','Pickle','Rogan','Sabrina','Mitty')๐Ÿ“‘ 2. ํ•œ ํ•ด์— ์žก์€ ๋ฌผ๊ณ ๊ธฐ์ˆ˜ ๊ตฌํ•˜๊ธฐโญ ์ •๋‹ต์ฝ”๋“œCOUNT(ID)์— `FISH_COUNT` ๋ผ๋Š” ๋ณ„์นญ์„ ์ฃผ์–ด์•ผ ํ•จ๋ฌธ์ œ ์˜ˆ์‹œ์—์„œ DATE ํ˜•์‹์ด 'YYYY/MM/DD'๋กœ ๋˜์–ด ์žˆ๋Š” ๊ฑธ ๋ณด๊ณ  ์•ฝ๊ฐ„ ๋ณต์žกํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค.SELECT COUNT(ID) AS FISH_COUNTFROM FISH_INFOWHERE DATE_FORMAT(Time, '%Y/%m/%d') LIKE '2021%' ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ํ’€์ดํ•œ ๊ฒƒ์„ ํ™•์ธํ–ˆ๋Š”๋ฐ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š”..
๐Ÿ“‘ 1. ๋ฌธ์ œ์„ค๋ช…โญ 2. ์ •๋‹ต์ฝ”๋“œSELECT Animal_id, name, CASE WHEN Sex_upon_intake LIKE '%Neutered%' OR Sex_upon_intake LIKE '%Spayed%' THEN 'O' ELSE 'X' END AS ์ค‘์„ฑํ™”FROM ANIMAL_INSORDER BY ANIMAL_ID; ๐Ÿ˜Š 3. TMIANSI SQL ํ‘œ์ค€์—์„œ๋Š” ALIAS ์— ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ ์จ๋„ ์ƒ๊ด€์€ ์—†๋‹ค. (๋‚˜๋Š” ๋”ฐ์˜ดํ‘œ ์ผ์Œ)SELECT Animal_id, name, CASE WHEN Sex_upon_intake LIKE '%Neutered%' ..
์œˆ๋„์šฐํ‚ค > MYSQL Command Line Client MySQL ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅํ•œ ๋’คCREATE DATABASE BBS;USE BBS; BBS๋ผ๋Š”  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋งŒ๋“ค์–ด ์คŒ ์ด์ œ ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•  ์ฐจ๋ก€ CREATE TABLE USER ๋กœ ์œ ์ € ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ์ด๋ฆ„, ์„ฑ๋ณ„, ์ด๋ฉ”์ผ ์ปฌ๋Ÿผ 5๊ฐœ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  USERID๋ฅผ PK๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ์Œ.SHOW TABLES; ์œ„ ๋ช…๋ น์–ด๋กœ ํ…Œ์ด๋ธ” ๋ณด๊ธฐ  DESC USER; `DESC + ํ…Œ์ด๋ธ”๋ช…`์œผ๋กœ ํ˜„์žฌ ํ…Œ์ด๋ธ” ํ˜•ํƒœ ๋ณด์—ฌ์คŒ            ํ…Œ์ŠคํŠธ๋กœ ํ™๊ธธ๋™์ด๋ผ๋Š” ์œ ์ €์˜ ์ •๋ณด๋ฅผ ํ…Œ์ด๋ธ”์— ํ•œ ์ค„ ๋„ฃ์–ด ์คŒ ์ฐธ๊ณ ๋กœ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋“ค์–ด๊ฐ”๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด `select * from user;`  ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ๋“ค์–ด์™”์œผ๋ฉด `commit` ํ•ด ์ค€๋‹ค.  ์—ฌ๊ธฐ๊นŒ์ง€ ํ–ˆ์œผ๋ฉด ์ด์ œ..
์ƒ๋‹จ์œผ๋กœ