1 ๋ถ„ ์†Œ์š”

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•ด์„œโ€ฆ


1. ํ‚ค

  • ๊ธฐ๋ณธ ํ‚ค (Primaty Key, PK)

    ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” ๊ฐ’

  • ์™ธ๋ž˜ ํ‚ค (Foreign Key, FK)

    ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋Š” ๊ฐ’



2. JOIN

  • INNER JOIN (๋‚ด๋ถ€ ์กฐ์ธ == ๋™๋“ฑ ์กฐ์ธ)

    orders inner join book on orders.bookid = book.bookid;

  • OUTER JOIN (์™ธ๋ถ€ ์กฐ์ธ)

    • left join

      ์™ผ์ชฝ ํ…Œ์ด๋ธ” ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์ ธ์˜ด

    • right join

      ์˜ค๋ฅธ์ชฝ ํ…Œ์ด๋ธ” ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์ ธ์˜ด

    • full join

      ํ…Œ์ด๋ธ”์„ ์ „์ฒด ๋‹ค ํ•ฉ์ณ์„œ ๊ฐ€์ ธ์˜ด

    *์‚ฌ์‹ค left join๊ณผ right join์€ ๊ฐ™์€ ๊ฐœ๋…์ด๋‹ค (๋ฐฉํ–ฅ์„ฑ๋งŒ ๋‹ค๋ฅผ ๋ฟ)



3. UNION (ํ•ฉ์ง‘ํ•ฉ)

  • ์˜ˆ์ œ

    select * from customer where address like โ€˜๋Œ€ํ•œ๋ฏผ๊ตญ%โ€™
    union โ€“ union all : ์ค‘๋ณต์ด ๋˜๋”๋ผ๋„ ํ•ฉ์ง‘ํ•ฉ ํ•ด์คŒ
    select c.* from customer c, orders o where c.custid = o.custid;

    • ์ฃผ์†Œ๊ฐ€ โ€˜๋Œ€ํ•œ๋ฏผ๊ตญ~โ€™์ธ ๊ณ ๊ฐ๊ณผ c.custid = o.custid์ธ ๊ณ ๊ฐ์˜ ์ •๋ณด๋ฅผ ํ•ฉ์นจ


4. IN

์กฐ๊ฑด์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ด > ex) select * from book where publisher in (โ€˜๊ตฟ์Šคํฌ์ธ โ€™, โ€˜๋Œ€ํ•œ๋ฏธ๋””์–ดโ€™);



5. ์ค‘์ฒฉ์งˆ์˜

select ๋ฌธ ์•ˆ์— select ๋ฌธ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์งˆ์˜

  • ๊ตฌ์กฐ

    select (์Šค์นผ๋ผ ๋ถ€์†์งˆ์˜)
    from // ์ธ๋ผ์ธ ๋ทฐ (๊ฐ€์ƒ ํ…Œ์ด๋ธ”)
    where (๋ถ€์†์งˆ์˜)



5. INSERT (์‚ฝ์ž…)

ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€

  • ๊ตฌ์กฐ

    insert into ํ…Œ์ด๋ธ”๋ช… ( ์†์„ฑ๋ช…, โ€ฆ values ( ๊ฐ’1, โ€ฆ );

  • ์˜ˆ์ œ

    insert into customer (custid, name, address, phone)
    values (6, โ€œ์ด์ด๋ฆ„โ€, โ€œ๋Œ€ํ•œ๋ฏผ๊ตญ ์ธ์ฒœโ€, โ€œ000-0000-0000โ€); - ์ด๋ฆ„์ด โ€œ์ด์ด๋ฆ„โ€์ธ ๊ณ ๊ฐ์˜ id์™€ ์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ ์ถ”๊ฐ€



6. UPDATE (์ˆ˜์ •)

ํ…Œ์ด๋ธ” ์•ˆ์˜ ๋ฐ์ดํ„ฐ ์ˆ˜์ •

  • ๊ตฌ์กฐ

    update ํ…Œ์ด๋ธ”๋ช…
    set ( ์†์„ฑ๋ช…1 = ๊ฐ’, ์†์„ฑ๋ช…2 = ๊ฐ’2 โ€ฆ )
    where ์กฐ๊ฑด์‹



7. DELETE (์‚ญ์ œ)

ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ์‚ญ์ œ

  • ๊ตฌ์กฐ

    delete from ํ…Œ์ด๋ธ”๋ช…
    where ์กฐ๊ฑด์‹

    • ์กฐ๊ฑด์‹ ์•ˆ์“ฐ๋ฉด ํ…Œ์ด๋ธ” ์ „์ฒด๊ฐ€ ์‚ฌ๋ผ์งโ€ฆ
  • ์˜ˆ์ œ

    delete from customer where address like โ€˜%์ธ์ฒœโ€™;

    • ์ฃผ์†Œ๊ฐ€ ์ธ์ฒœ์œผ๋กœ ๋๋‚˜๋Š” ๊ณ ๊ฐ์˜ ์ •๋ณด ์‚ญ์ œ


ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ