[TIL]21.08.26

๋ฐ•์ฃผํ™ยท2021๋…„ 8์›” 26์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
59/104

๐Ÿ‘จโ€๐Ÿ’ป์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

DATABASE๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

: File l/o๋Š” ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ๋˜๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ ํ˜น์€ ์„œ๋ฒ„์—์„œ ํ•ญ์ƒ ํ•„ํ„ฐ๋ง์„ ํ•ด์•ผ๋˜๋Š” ๋ฐ, ์„œ๋ฒ„์˜ ๋ถ€ํ•˜๊ฐ€ ์‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ผ๋Š” ์„œ๋ฒ„(?)๋ฅผ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. SQL์€ DATABASE์— Query(์š”์ฒญ, ์งˆ์˜๋ฌธ)์„ ํ•ด์„œ ์‘๋‹ต์„ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์–ธ์–ด์ด๋‹ค.



๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ


1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
CREATE DATABASE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„
2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ (npm init, git init ๊ณผ ๋น„์Šท)
USE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„
3. ์ƒ์„ฑํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ…Œ์ด๋ธ”์ƒ์„ฑ
CREATE TABLE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„(
  // ํ•„๋“œ์ด๋ฆ„ ํ•„๋“œ ํƒ€์ž… ๊ทธ์™ธ์˜ ์†์„ฑ ์ˆœ์œผ๋กœ ์„ ์–ธํ•˜๊ฒŒ ๋œ๋‹ค
  id int PRIMARY KEY AUTO_INCREMENT, // PRIMARY KEY๋ฉด์„œ ์ž๋™ ์ฆ๊ฐ€๋˜๋„๋ก ์„ค์ •
  name varchar(255),
  email varchar(255) // ๋ฌธ์ž์—ด์„ 255 ์”ฉ ์“ฐ๊ฒ ๋‹ค๋Š” ๋ง์ด๋‹ค.
)
4. ํ…Œ์ด๋ธ” ์ •๋ณด ํ™•์ธ
DESCRIBE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„



SQL

: SQL๋ฌธ๋ฒ•์€ ์—ฌ๊ธฐ๋ณด๋‹ค ์ž˜ ์„ค๋ช…๋˜์–ด์žˆ๋Š” ๊ณณ์ด ๋งŽ์œผ๋‹ˆ,, ๋‚ด๊ฐ€ ํ‹€๋ฆฐ ๋ฌธ์žฅ๋“ค์„ ์ ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.
  1. SELECT FROM Persons WHERE FirstName LIKE 'a%'
    : Personsํ…Œ์ด๋ธ”์— FirstName ํŠน์„ฑ์—์„œ a๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ž€ ๋œป์ด๋‹ค.
    ์ด๋•Œ SELECT๋Š” ๋ฐ์ดํ„ฐ ์…‹์— ํฌํ•จ๋  ํŠน์„ฑ์„ ํŠน์ •ํ•œ๋‹ค.
    ์€ ๋ชจ๋“  ๋ฐ์ดํ„ฐํƒ€์ž…์„ ์˜๋ฏธํ•˜๊ณ 
    'hello wolrd' <<== ๋ฌธ์ž์—ด
    2 <<== ์ˆซ์ž
    15 + 3 <<== ๊ฐ„๋‹จํ•œ ์—ฐ์‚ฐ

  2. SELECT * FROM Persons WHERE FirstName = 'Peter' AND LastName = 'Jackson'
    : ๋ชจ๋“  ํŠน์„ฑ์„ Persons ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ FirstNameํŠน์„ฑ์ด Perter์ธ๊ฒฝ์šฐ๋งŒ ๊ทธ๋ฆฌ๊ณ  LastNameํŠน์„ฑ์ด 'Jackson'๊ฒฝ์šฐ๋งŒ
    ๊ฐ€์ ธ์˜ค๋ž€ ๋œป์ด๋‹ค.

    ์ด๋•Œ WHERE์€ = ๋ฟ๋งŒ์•„๋‹ˆ๊ณ  <=, <, >=, > ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

  3. SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
    : ๋ชจ๋“  ํŠน์„ฑ์„ Persons ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ LastName์ธ ํŠน์„ฑ์—์„œ Hansen๊ณผ Pettersen์‚ฌ์ด์— ์žˆ๋Š” ๊ฐ’๋“ค์„ ๊ฐ€์ ธ์˜ค๋ผ๋Š” ๋œป

  4. SELECT DISTINCT ํŠน์„ฑ

    SELECT
     DISTINCT
       ํŠน์„ฑ_1
       ,ํŠน์„ฑ_2
       ,ํŠน์„ฑ_3
    FROM ํ…Œ์ด๋ธ”_์ด๋ฆ„
  5. ORDER BY ํŠน์„ฑ // ์˜ค๋ฆ„์ฐจ์ˆœ
    ORDER BY ํŠน์„ฑ DESC // ๋‚ด๋ฆผ์ฐจ์ˆœ

  6. INSERT INTO Persons VALUES('Jimmy', 'Jadson')
    INSERT INTO Persons (LastName) VALUES ('Olsen')
    : INSERT INTO VALUES์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€๊ฐ€๋Šฅ
    ์ฒซ๋ฒˆ์งธ๋Š” Personsํ…Œ์ด๋ธ”์— 'Jimmy', 'Jadson'์„ ๋„ฃ๊ฒ ๋‹ค๋Š” ๋œป
    ๋‘๋ฒˆ์งธ๋Š” Personsํ…Œ์ด๋ธ”์˜ LastNameํ•„๋“œ์— 'Olsen'์„ ๋„ฃ๊ฒ ๋‹ค๋Š” ๋œป

  7. UPDATE Persons SET LastName = 'Nilsen' WHERE LastName = 'Hansen'
    : Personsํ…Œ์ด๋ธ”์˜ LastNameํ•„๋“œ์˜ 'Nilsen'์„ 'Hansen'๋กœ ๋ฐ”๊พธ๊ฒ ๋‹ค๋Š” ๋œป

  8. DELETE FROM Persons WHERE FirstName = 'Peter'

  1. SELECT COUNT (*) FROM Persons

  2. INNER JOIN

  3. ORDER BY Country, City

  4. UPDATE Customers
    SET City = 'Oslo'
    AND Country = 'Norway'

profile
๊ณ ํ†ต์—†๋Š” ์„ฑ์žฅ์€ ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค....

0๊ฐœ์˜ ๋Œ“๊ธ€