ํ’€์Šคํƒ ์›น๐ŸŒ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ ๐Ÿง‘๐Ÿฝโ€๐Ÿ’ป
โž• ์ธ๊ณต์ง€๋Šฅ ๊ด€์‹ฌ ๐Ÿค–


Categories


Recent views

  • 1
  • 2
  • 3
  • 4
  • 5

Database SQL ๊ธฐ์ดˆ

Database SQL ๊ธฐ์ดˆ

DB

  • ์ฒด๊ณ„ํ™”๋œ ๋ฐ์ดํ„ฐ๋“ค์˜ ๋ชจ์ž„
  • ์—ด, ์นผ๋Ÿผ, ๊ฐ ์—ด์—๋Š” ๊ณ ์œ ํ•œ ๋ฐ์ดํ„ฐ ํ˜•์‹์ด ์ง€์ •๋จ, ๋ฐ์ดํ„ฐ์˜ ์†์„ฑ
  • ํ–‰, ๋ ˆ์ฝ”๋“œ, 1๊ฐœ์˜ ๋ฐ์ดํ„ฐ
  • ์Šคํ‚ค๋งˆ, ํ‹€ ๊ฐ ์—ด์˜ ์š”์†Œ, ์ฆ‰ ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ์˜ ๋‚ด์šฉ์„ ์ •์˜ํ•จ

    ๊ตฌ์„ฑ์š”์†Œ

    1. ๊ฐœ์ฒด, entity( ์‚ฌ์›๋“ค)
    2. ๊ทธ๋“ค์ด ๊ฐ€์ง€๋Š” ์†์„ฑ, attribute (์‚ฌ์›๋ฒˆํ˜ธ, ์„ฑ๋ช…, ๋ถ€์„œ , ์ง์ฑ…)
    3. ๊ฐœ์ฒด ์‚ฌ์ด์˜ ๊ด€๊ณ„ (relation)

      ORM(Object-Relational Mapping)

  • SQL์ด ๋ชฐ๋ผ๋„ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์œผ๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์–ธ์–ด, ํŒŒ์ด์ฌ ์–ธ์–ด(์ดˆ์•ˆ, ์ฒญ์‚ฌ์ง„)์„ SQL ๋ฌธ์œผ๋กœ ํ•ด์„ํ•˜์—ฌ ๊ฐ์ฒด๋กœ ์ด์šฉํ•˜๊ฒŒ ํ•ด์คŒ, ์ค‘๊ฐ„ ๋ฒˆ์—ญ์ž

    CRUD๋ž€?

  • ๋ฐ์ดํ„ฐ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” 4๊ฐ€์ง€
  • C: CREATE
  • R: READ
  • U: UPDATE
  • D: DELETE

    RDBMS(๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ)

  • ๊ฐœ์ฒด์™€ ๊ฐœ์ฒด ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด 2์ฐจ์›์˜ ํ‘œ๋ฅผ ์‚ฌ์šฉ
  • ์ฐจ์ˆ˜ : ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์˜ ์ˆ˜, ์นด๋””๋„๋Ÿฌํ‹ฐ : ๊ฐœ์ฒด์ˆ˜ ??

    SQLite

  • ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ๋„ฃ์–ด ์‚ฌ์šฉํ•˜๋Š” ๋น„๊ต์  ๊ฐ€๋ฒผ์šด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(์•ˆ๋“œ๋กœ์ด๋“œ, ์ž„๋ฒ ๋””๋“œ , django ๋“ฑ), ์˜คํ”ˆ์†Œ์Šค์ด๋ฉฐ ๋กœ์ปฌ์— ๊ฐ„๋‹จํ•œ DB ๊ตฌ์„ฑ ๊ฐ€๋Šฅ,

    ๊ธฐ๋ณธ ์šฉ์–ด ์ •์˜

    1. ์Šคํ‚ค๋งˆ(scheme) : ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ ์ž๋ฃŒ์˜ ๊ตฌ์กฐ, ํ‘œํ˜„ ๋ฐฉ๋ฒ•, ๊ด€๊ณ„ ๋“ฑ์„ ์ •์˜ํ•œ ๊ตฌ์กฐ, ๋ช…์„ธ์„œ, ๋ฐ์ดํ„ฐ ๊ทœ๊ฒฉ, ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ •์˜, ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์˜ ๊ตฌ์กฐ์™€ ์ œ์•ฝ ์กฐ๊ฑด์— ๊ด€๋ จํ•œ ์ „๋ฐ˜์ ์ธ ๋ช…์„ธ๋ฅผ ๊ธฐ์ˆ .
    2. ํ…Œ์ด๋ธ”: ์Šคํ‚ค๋งˆ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„, ์—‘์…€ ํ‘œ ๊ฐ™์€๊ฑฐ, ๋ฐ์ดํ„ฐ๋“ค์˜ ์ง‘ํ•ฉ
    3. ์—ด(์นผ๋Ÿผ, ์–ดํŠธ๋ฆฌ๋ทฐํŠธ, ์†์„ฑ, Column): ๊ณ ์œ ํ•œ ๋ฐ์ดํ„ฐ ํ˜•์‹์˜ ์ง€์ •
    4. ํ–‰(๋ ˆ์ฝ”๋“œ, ๋ฐ์ดํ„ฐ, row):
    5. PK(๊ธฐ๋ณธ ํ‚ค, Primary Key) : ๊ฐ ํ–‰(๋ ˆ์ฝ”๋“œ)์˜ ๊ณ ์œ ํ•œ ๊ฐ’, ๋ฐ˜๋“œ์‹œ ์„ค์ •๋จ, ๊ด€๋ฆฌ ๋ฐ ๊ด€๊ณ„ ์„ค์ •์‹œ ํ™œ์šฉ

      SQL ๊ฐœ๋…

  • SQL(Structured Query Language)๋Š” RDBMS์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ํŠน์ˆ˜ ๋ชฉ์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค, ์ž๋ฃŒ์˜ ๊ฒ€์ƒ‰, ๊ด€๋ฆฌ, ์Šคํ‚ค๋งˆ ์ƒ์„ฑ๊ณผ ์ˆ˜์ •, ๊ฐ์ฒด ์ ‘๊ทผ ์กฐ์ • ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ๊ณ ์•ˆ

    ๋ฌธ๋ฒ• ์ข…๋ฅ˜

    1. DDL(Data Definition Language) - ๋ฐ์ดํ„ฐ ์ •์˜ ์–ธ์–ด.๊ตฌ์กฐ, ์ฆ‰ ํ…Œ์ด๋ธ”๊ณผ ์Šคํ‚ค๋งˆ๋ฅผ ์ •์˜
    • CREATE, DROP, ALTER ๋“ฑ
      1. DML(Data Manipulation Language) - ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด. ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ, ์ˆ˜์ •, ์‚ญ์ œ, ์กฐํšŒ
    • INSERT(๋ฐ์ดํ„ฐ์‚ฝ์ž…), UPDATE(๊ฐฑ์‹ ), DELETE(์‚ญ์ œ, ํ–‰์ œ๊ฑฐ), SELECT(๋ฐ์ดํ„ฐ, ๊ฒ€์ƒ‰)
      1. DCL(Data Control Language) - ๋ฐ์ดํ„ฐ ์ œ์–ด ์–ธ์–ด, ์‚ฌ์šฉ์ž ๊ถŒํ•œ ์ œ์–ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ
    • GRANT, REVOKE, COMMIT, ROLLBACK

      ์‹ค์Šต

      ๊ธฐ๋ณธ sql ๋™์ž‘

      1. sqlite3.exe๊ฐ€ ์žˆ๋Š” ํด๋”์—์„œ ํ„ฐ๋ฏธ๋„๋กœ sqlite3 ์น˜๋ฉด
        ```sql
        $ sqlite3
        SQLite version 3.29.0 2019-07-10 17:32:03
        Enter ".help" for usage hints.
        Connected to a transient in-memory database.
        Use ".open FILENAME" to reopen on a persistent database.
        sqlite>
์ƒ๋‹จ์ฒ˜๋Ÿผ ํ‘œ์‹œ๋œ๋‹ค.  ๋ช…๋ น์–ด ์•ž์— `.`์„ ์ž…๋ ฅํ•˜๋ฉด DB ๊ด€๋ จ ๋ช…๋ น์–ด์ด๋‹ค.
- ์˜ˆ๋ฅผ ๋“ค์–ด .exit๋กœ ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค
2. sqlite3 ํŒŒ์ผ ์ƒ์„ฑ๋ฒ• 
	1) sqlite3 test.sqlite3 ์ž…๋ ฅ
	2) .databases ์ž…๋ ฅ
```sql
sqlite3 test.sqlite3
SQLite version 3.29.0 2019-07-10 17:32:03
Enter ".help" for usage hints.
sqlite> .databases
main: C:\Users\student\sqlite\test.sqlite3
sqlite>

  • test๋Š” ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๋ช…
    1. csv ํŒŒ์ผ ๊ฐ€์ ธ์™€์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋งŒ๋“ค๊ธฐ
      ```sql
      sqlite> .mode csv
      sqlite> .import hellodb.csv examples
- hellodb๋Š” csv ํŒŒ์ผ ์ด๋ฆ„
- .mode csv๋Š” ์–ด๋–ค ํŒŒ์ผ์„ ๋Œ€์ƒ์œผ๋กœ ํ• ๊ฒƒ์ธ๊ฐ€?
- ๋’ค .import ๋Š” ๊ทธ ํŒŒ์ผ์„ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋กœ ๋งŒ๋“ค๊ณ , ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ examples๋กœ ๋งŒ๋“ฆ
- 
4. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๋ˆˆ์œผ๋กœ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค๊ธฐ
```sql
SELECT * FROM examples;
1,"๊ธธ๋™","ํ™",600,"์ถฉ์ฒญ๋„",010-2424-1232

    • ์€ ์ „๋ถ€ ๋‹ค๋ผ๋Š” ํ‘œํ˜„์— ๊ฐ€๊นŒ์›€, examples๋ผ๋Š” ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ ์ „๋ถ€(*) ๋ฐ˜ํ™˜ํ•ด ๋ผ๋Š” ๋œป
  • ์—ฌ๊ธฐ์„œ SELECT, FROM ๋“ฑ์€ ํ‚ค์›Œ๋“œ์ด๋ฉฐ, ํ‚ค์›Œ๋“œ๋Š” ๋Œ€๋ฌธ์ž๊ฐ€ ๊ด€๋ก€(์†Œ๋ฌธ์ž๋กœ ๋˜๊ธดํ•จ)
    1. ๋” ์˜ˆ์˜๊ฒŒ ๊ฐ€์ ธ์˜ค๊ธฐ
      ```sql
      sqlite> .headers on
      sqlite> .mode column
      sqlite> SELECT * FROM examples;
      id first_name last_name age country phone
      โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”โ€”-
      1 ๊ธธ๋™ ํ™ 600 ์ถฉ์ฒญ๋„ 010-2424-1232
- .headers๋กœ ํ—ค๋”๋ฅผ ์ ๋Š” ๋ชจ๋“œ๋กœ, .mode column์œผ๋กœ ๊ฐ ์ปฌ๋Ÿผ๋งˆ๋‹ค ๊ตฌ๋ณ„ํ•˜๋Š” ๋ชจ๋“œ๋กœ ๋ฐ”๊พผ ๋“ฏ?
6. ํŒŒ์ผ๋กœ ๋ช…๋ น์–ด ์ ์–ด์„œ ์‹คํ–‰ํ•˜๊ธฐ
	1) 00_intro.sql ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ
	
	> 00_intro.sql ํŒŒ์ผ ๋‚ด์šฉ
```sql
-- sql ์ฃผ์„์ฒ˜๋ฆฌ๋Š” --๋กœ ํ•œ์ค„
-- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
.database


-- csvํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ธฐ sqlite์˜ ๊ธฐ๋Šฅ์ž„ sql ๋ฌธ๋ฒ• ์•„๋‹˜, ; ํ•„์š” ์—†์Œ
.mode csv
.import hellodb.csv examples -- ์™ธ๋ถ€ ํŒŒ์ผ์—์„œ ์Šคํ‚ค๋งˆ ๊ฐ€์ ธ์˜ค๊ธฐ (์ „๋ถ€ TEXT๋กœ ๊ฐ€์ ธ์˜ด)

-- ์˜ˆ์˜๊ฒŒ ๋ณด๊ธฐ
.headers on
.mode column

-- ํ…Œ์ด๋ธ” ์กฐํšŒ sql ๋ฌธ๋ฒ• ๋์—๋Š” ๊ผญ ;๋ฅผ ๋ถ™์ž„
SELECT * FROM examples;

  • CAST(๊ฐ’ AS INTEGER) = INTEGER๋กœ ํ˜•๋ณ€ํ™˜์œผ๋กœ ์ ์ ํžˆ ํ˜•๋ณ€ํ™˜ ๊ฐ€๋Šฅ

2) ํ„ฐ๋ฏธ๋„์—์„œ .read 00_intro.sql ๋ช…๋ น์–ด๋กœ ์‹คํ–‰( ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์ผ์‹œ)

sqlite> .read 00_intro.sql
main: C:\Users\student\sqlite\test.sqlite3
id,first_name,last_name,age,country,phone
1,"๊ธธ๋™","ํ™",600,"์ถฉ์ฒญ๋„",010-2424-1232
id,first_name,last_name,age,country,phone
1,"๊ธธ๋™","ํ™",600,"์ถฉ์ฒญ๋„",010-2424-1232

CRUD ๋™์ž‘

๋ฐ์ดํ„ฐ ์ƒ์„ฑ CREATE, Table ์ƒ์„ฑ

1) 01_DDL.sql ํŒŒ์ผ ์ƒ์„ฑ > 01_DDL.sql ํŒŒ์ผ ๋‚ด์šฉ ```sql -- DDL(๋ฐ์ดํ„ฐ ์ •์˜ ์–ธ์–ด) CREATE TABLE classmates (
id INTEGER PRIMARY KEY,
name TEXT ); -- ํŒŒ์ด์ฌ๊ณผ ๋‹ฌ๋ฆฌ ๋งˆ์ง€๋ง‰ ์š”์†Œ์— ',' ๋‚จ๊ธฐ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚จ -- ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ” ๋ชฉ๋ก ์กฐํšŒ .tables

โ€“ ์Šคํ‚ค๋งˆ ์กฐํšŒ, ์ฃผ์„๋„ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.
.schema classmates

โ€“ ํ…Œ์ด๋ธ” ์‚ญ์ œ
DROP TABLE classmates;
.tables

1) ํ„ฐ๋ฏธ๋„์— .read 01_DDL.sql

> ํ„ฐ๋ฏธ๋„ ์ž…๋ ฅ
```sql
sqlite> .read 01_DDL.sql
classmates
CREATE TABLE classmates (
    id INTEGER PRIMARY KEY, -- INTEGER๋Š” INT๋กœ ์ค„์—ฌ์จ๋„ ๋จ, ๋‹จ PRIMARY KEY๋Š” INTEGER๋กœ ์จ์•ผํ•จ, ์•„๋‹์‹œ๋Š” INT๋กœ ์จ๋„ ๋จ
    name TEXT 
);

๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ (INSERT)

1. data ์ถ”๊ฐ€ (INSERT)	: ํŠน์ • table์— ์ƒˆ๋กœ์šด ํ–‰์„ ์ถ”๊ฐ€ํ•ด ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
	- INSERT INTO talbe(column1, column2,...)
	- VALUES (value1, value2, ...)
2. 02_CRUD.sql ํŒŒ์ผ ์ƒ์„ฑ

> 02_CRUD.sql ํŒŒ์ผ ๋‚ด์šฉ ```sql
-- ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ CREATE TABLE classmates (
name TEXT,
age INT,
address TEXT );

โ€“ CREATE โ€“ ;์ด ๋“ฑ์žฅํ•˜์ง€ ์•Š์œผ๋ฉด 1์ค„๋กœ ์ธ์‹ํ•จ
INSERT INTO classmates (
name,
age,
address
)
VALUES ('์œค์ค€์„', 27, '๊ด‘์ฃผ');

.headers on
.mode column

โ€“ ํ™•์ธ
SELECT * FROM classmates;

3. ํ„ฐ๋ฏธ๋„์— .read 02_CRUD.sql ์‹คํ–‰

> ํ„ฐ๋ฏธ๋„ ๋‚ด์šฉ, classmate 1๊ฐœ ์ถ”๊ฐ€
```sql
sqlite> .read 02_CRUD.sql
id          name        age         address
----------  ----------  ----------  ----------
1           ์œค์ค€์„         27          ๊ด‘์ฃผ

  1. ํ•œ๋ฒˆ ๋” ์‹คํ–‰

ํ„ฐ๋ฏธ๋„ ๋‚ด์šฉ, ๊ฐ™์€ ๋‚ด์šฉ์ด ๋“ค์–ด์žˆ๋Š” classmate 1๊ฐœ ๋” ์ถ”๊ฐ€
```sql
sqlite> .read 02_CRUD.sql
id name age address
โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”-
1 ์œค์ค€์„ 27 ๊ด‘์ฃผ
2 ์œค์ค€์„ 11 ๊ด‘์ฃผ

5. ํŒŒ์ผ์—์„œ ์š”์†Œ ํ•˜๋‚˜๋ฅผ ๋นผ์„œ .read 02_CRUD.sql ์‹คํ–‰
> ๋ณ€๊ฒฝ๋œ 02_CRUD.sql ํŒŒ์ผ ๋‚ด์šฉ
```sql
-- ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ
CREATE TABLE classmates (
    name TEXT,
    age INT,
    address TEXT
);
-- CREATE -- ;์ด ๋“ฑ์žฅํ•˜์ง€ ์•Š์œผ๋ฉด 1์ค„๋กœ ์ธ์‹ํ•จ
INSERT INTO classmates (
name,
address
)
VALUES ('์œค์ค€์„', '๊ด‘์ฃผ');

.headers on
.mode column

-- ํ™•์ธ
SELECT * FROM classmates;

age ์นธ์ด ๋น„์–ด์žˆ๋Š” classmate 1๊ฐœ ๋” ์ถ”๊ฐ€
```sql
sqlite> .read 02_CRUD.sql
id name age address
โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”-
1 ์œค์ค€์„ 27 ๊ด‘์ฃผ
2 ์œค์ค€์„ 11 ๊ด‘์ฃผ
3 ์œค์ค€์„ ๊ด‘์ฃผ

6.  ํŒŒ์ผ์„ ๋˜ ๋ณ€๊ฒฝํ•œ ์˜ˆ์‹œ

> ๋ณ€๊ฒฝ๋œ 02_CRUD.sql ํŒŒ์ผ ๋‚ด์šฉ

```sql
CREATE TABLE classmates (
    name TEXT,
    age INT,
    address TEXT
);
INSERT INTO classmates -- ๋ชจ๋“  ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„๋•Œ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ
VALUES('ํ™๊ธธ๋™', 30, '์„œ์šธ');

SELECT rowid, * FROM classmates; -- ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ rowid๋ฅผ ๊ฐ™์ด ์ถœ๋ ฅ 

DROP TABLE classmates; -- ํ…Œ์ด๋ธ” ์‚ญ์ œ

ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
```sql
sqlite> .read 02_CRUD.sql
rowid name age address
โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”-
1 ํ™๊ธธ๋™ 30 ์„œ์šธ

7. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๋ฌด๊ฒฐ์„ฑ์˜ ์›์น™์„ ์œ„ํ•œ NOT NULL ์ถ”๊ฐ€
> ๋ณ€๊ฒฝ๋œ 02_CRUD.sql ํŒŒ์ผ ๋‚ด์šฉ
```sql
DROP TABLE classmates;
.tables
CREATE TABLE classmates (
    id INTEGER PRIMARY KEY NOT NULL,
    name TEXT NOT NULL, -- NOT NULL์„ ๋„ฃ์œผ๋ฉด ๋น„์šด์ฑ„๋กœ ๋„ฃ์„ ์ˆ˜ ์—†๋‹ค.
    age INT NOT NULL,
    address TEXT NOT NULL
);
INSERT INTO classmates 
VALUES
(1, '์œค์ค€์„', 27, '๊ด‘์ฃผ'),
(2, '์˜ค์ฐฝํฌ', 11, '๊ด‘์ฃผ'),
(3, '๋ฐ•๋‚˜๋ž˜', 25, '์„œ์šธ'),
(4, '์ด์š”์…‰', 29, '๊ตฌ๋ฏธ'),
(5, '๊น€์ฒ ์ˆ˜', 27, '๋Œ€์ „'); 

-- ์œ„์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ๊บผ๋ฒˆ์— ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.
SELECT * FROM classmates;

๋งŒ์•ฝ NOT NULL์ธ ๊ฐ’์„ ๋นˆ ์ฑ„๋กœ ์“ฐ๋ฉด ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.
```sql
Error: near line 9: NOT NULL constraint failed: classmates.address

- ์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ๊ณต๋ฐฑ์ด ๋˜๋Š”๊ฑธ ๋ฐฉ์ง€ํ•œ๋‹ค.
> ๋งŒ์•ฝ id ๊ฐ’์ด ๊ฒน์น˜๋ฉด ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.
```sql
Error: near line 11: UNIQUE constraint failed: classmates.id

  • ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•ด ์ง์ ‘ ๋„ฃ์ง€ ๋ง๊ณ  sql์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๊ฐ’์„ ์“ฐ์ž

  • ๋งŒ์•ฝ id๋ฅผ ์ง์ ‘ ์ง‘์–ด ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค๋ฉด NOT NULL ๋Œ€์‹  AUTOINCREMENT ๋ฅผ ๋„ฃ์–ด์„œ ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•˜๊ฒŒ ํ•ด์ฃผ์ž.(์ด๋Ÿฌ๋ฉด ์ง€์›Œ์ง„ id๋ฅผ ์ƒˆ๋กœ์šด ์—ด์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค, SQLite์—์„œ๋Š” ๋น„์ถ”์ฒœํ•จ(๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ฆ๊ฐ€))

  • AUTOINCREMENT๋ฅผ ์“ฐ๋ฉด ๊ทธ ๋ถ€๋ถ„์€ ์ œ์™ธ ์—ด์„ ๋‚˜์—ดํ•ด์ค˜์•ผํ•จ

    AUTOINCREMENT ์˜ˆ์‹œ
    ```sql
    CREATE TABLE bands(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    debut INTEGER
    );
    INSERT INTO bands (name, debut) VALUES
    ('Queen', 1973),
    ('Coldplay', 1998),
    ('MCR', 2001);

> CREATE TABLE classmates์˜ ์˜ˆ์‹œ
```sql
id INT PRIMARY KEY AUTOINCREMENT, 
-- ๋˜๋Š” ์•„์˜ˆ id ์•ˆ์จ์„œ ์ž๋™์ƒ์„ฑ๋˜๊ฒŒ๋”

์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ œํ•œํ•ด์„œ ๊ฐ€์ ธ์˜ค๊ธฐ(READ)

select ๋ช…๋ น๋ฌธ
```sql
SELECT rowid, name FROM classmates; โ€“ ์ „์ฒด ๋ฐ์ดํ„ฐ์ค‘ ์ผ๋ถ€๋งŒ ๊ฐ€์ ธ์˜ค๋Š”๋ฒ•
โ€“ ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
examples
rowid name
โ€”โ€”โ€”- โ€”โ€”โ€”-
1 ์œค์ค€์„
2 ์˜ค์ฐฝํฌ
3 ๋ฐ•๋‚˜๋ž˜
4 ์ด์š”์…‰
5 ๊น€์ฒ ์ˆ˜

>ํŠน์ •ํ•œ ํ…Œ์ด๋ธ”์—์„œ ์›ํ•˜๋Š” ๊ฐฏ์ˆ˜๋งŒํผ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ(LIMIT ๋ช…๋ น์–ด)
```sql
SELECT rowid, name FROM classmates LIMIT 1; -- ์ „์ฒด ๋ฐ์ดํ„ฐ์ค‘ 1๊ฐœ๋งŒ ๊ฐ€์ ธ์˜ค๋Š”๋ฒ•
-- ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
sqlite> .read 02_CRUD.sql
examples
rowid       name
----------  ----------
1           ์œค์ค€์„

๋ฐ์ดํ„ฐ ๋ช‡๊ฐœ๋ฅผ ์Šคํ‚ตํ•˜๊ณ  ๊ฐ€์ ธ์˜ค๊ธฐ(OFFSET ๋ช…๋ น์–ด)
```sql
SELECT rowid, name FROM classmates LIMIT 1 OFFSET 2; โ€“ ์ „์ฒด ๋ฐ์ดํ„ฐ์ค‘ ์ฒ˜์Œ 2๊ฐœ๋ฅผ ์Šคํ‚ตํ•˜๊ณ  1๊ฐœ๋งŒ ๊ฐ€์ ธ์˜ค๋Š”๋ฒ•
โ€“ ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
sqlite> .read 02_CRUD.sql
examples
rowid name
โ€”โ€”โ€”- โ€”โ€”โ€”-
3 ๋ฐ•๋‚˜๋ž˜

- LIMIT์™€ ํ•จ๊ป˜ ์“ฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค
> ํŠน์ •ํ•œ ๊ฐ’ ๋งŒ ๊ฒ€์ƒ‰ํ•ด์„œ ๊ฐ€์ ธ์˜ค๊ธฐ(WHERE ๋ช…๋ น์–ด)
```sql
SELECT rowid, name FROM classmates WHERE address='๊ตฌ๋ฏธ'; -- ์ „์ฒด ๋ฐ์ดํ„ฐ์ค‘ ์ฃผ์†Œ๊ฐ€ '๊ตฌ๋ฏธ'์ธ ์‚ฌ๋žŒ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
-- ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
sqlite> .read 02_CRUD.sql
examples
rowid       name
----------  ----------
4           ์ด์š”์…‰

  • ๊ฒ€์ƒ‰ ๋“ฑ์— ํ™œ์šฉ

    ํŠน์ • table ์ค‘๋ณต ์ œ๊ฑฐ๊ฒฐ๊ณผ ์ถœ๋ ฅ
    ```sql
    โ€“ ์ „์ฒด ๋‚˜์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋˜, ์ค‘๋ณต์€ ์—†์•ฐ
    SELECT DISTINCT age FROM classmates;
    โ€“ ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ, ๋งˆ์ง€๋ง‰ 27์‚ด์ด ์—†์–ด์ง
    name age address
    โ€”โ€”โ€”- โ€”โ€”โ€”- โ€”โ€”โ€”-
    ์œค์ค€์„ 27 ๊ด‘์ฃผ
    ์˜ค์ฐฝํฌ 11 ๊ด‘์ฃผ
    ๋ฐ•๋‚˜๋ž˜ 25 ์„œ์šธ
    ์ด์š”์…‰ 29 ๊ตฌ๋ฏธ
    ๊น€์ฒ ์ˆ˜ 27 ๋Œ€์ „
    age
    โ€”โ€”โ€”-
    27
    11
    25
    29

#### ๋ฐ์ดํ„ฐ ์‚ญ์ œ(DELETE)
```sql
DELETE FROM classmates WHERE address='๊ด‘์ฃผ'; -- ์ฃผ์†Œ๊ฐ€ ๊ด‘์ฃผ์ธ ์‚ฌ๋žŒ๋งŒ ์ง€์šฐ๊ธฐ
-- ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
name        age         address
----------  ----------  ----------
๋ฐ•๋‚˜๋ž˜         25          ์„œ์šธ
์ด์š”์…‰         29          ๊ตฌ๋ฏธ
๊น€์ฒ ์ˆ˜         27          ๋Œ€์ „

  • ์ค‘๋ณต์ด ๋ถˆ๊ฐ€๋Šฅํ•œ(UNIQUEํ•œ) id๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง€์šฐ๋ฉด ํŠน์ • ์—ด๋งŒ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค.
  • ์ง€์›Œ์ง„ id๋Š” ๋‹ค์Œ์— ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ์—ด๊ฐ’์— ํ• ๋‹น๋œ๋‹ค ์ด๋ฅผ ๋ง‰์œผ๋ ค๋ฉด id๊ฐ’์„ ์„ ์–ธํ•  ๋•ŒAUTOINCREMENT๋ฅผ ์จ์•ผํ•จ(SQLite์—์„œ๋Š” ๋น„์ถ”์ฒœ)

๋ฐ์ดํ„ฐ ์ˆ˜์ •(UPDATE)

UPDATE์™€ SET, WHERE๋กœ ์ผ๋ถ€๋งŒ ์ˆ˜์ •ํ•˜๊ธฐ

UPDATE classmates SET name='ํ™๊ธธ๋™', address='์ œ์ฃผ' 
WHERE age>=50 AND address="๋งˆํฌ"; -- ๋‚˜์ด๊ฐ€ 50์‚ด ์ด์ƒ์ด๊ณ  ๊ณ ํ–ฅ์ด ๋งˆํฌ์ธ ์‚ฌ๋žŒ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„๊ณผ ์ฃผ์†Œ ๋ฐ”๊พธ๊ธฐ
-- ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ
rowid       name        age         address
----------  ----------  ----------  ----------
1           ์œค์ค€์„         27          ๊ด‘์ฃผ
2           ์˜ค์ฐฝํฌ         11          ๊ด‘์ฃผ
3           ๋ฐ•๋‚˜๋ž˜         25          ์„œ์šธ
4           ์ด์š”์…‰         29          ๊ตฌ๋ฏธ
5           ํ™๊ธธ๋™         54          ์ œ์ฃผ -- ๋ฐ•์ฒ ์šฉ, ๋งˆํฌ๊ฐ€ ํ™๊ธธ๋™, ์ œ์ฃผ๋กœ ๋ฐ”๋€œ

  • address==โ€๋งˆํฌโ€ ์—ฌ๋„ ๊ดœ์ฐฎ์Œ, ๊ด„ํ˜ธ ์ฒ˜๋ฆฌ๋„ ๊ดœ์ฐฎ์Œ

CONUT()๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆซ์ž์„ธ๊ธฐ
```sql
SELECT COUNT(*) FROM users WHERE age >= 30 AND last_name = '๊น€';
โ€“ ๋‚˜์ด๊ฐ€ 30์ด์ƒ์ด๊ณ  ์„ฑ์ด ๊น€์”จ์ธ ์œ ์ €์˜ ์ˆ˜๋Š”?

> AVG()๋ฅผ ์ด์šฉํ•œ ํ‰๊ท ๋‚ด๊ธฐ(INT ํ˜•๋งŒ ๊ฐ€๋Šฅ)
```sql
SELECT AVG(age) FROM users WHERE age >= 30 AND last_name = '๊น€';
-- ๋‚˜์ด๊ฐ€ 30์ด์ƒ์ด๊ณ  ์„ฑ์ด ๊น€์”จ์ธ ์œ ์ €๋“ค์˜ ํ‰๊ท  ๋‚˜์ด๋Š”?

MAX()๋ฅผ ์ด์šฉํ•œ ์ตœ๋Œ€๊ฐ’๊ตฌํ•˜๊ธฐ
```sql
SELECT MAX(balance), first_name FROM users;
โ€“ ๊ฐ€์žฅ ์ž”์•ก์ด ๋งŽ์€ ์‚ฌ๋žŒ์˜ ์ž”์•ก๊ณผ ์ด๋ฆ„์€?

- MIN()์€ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉ
> ์™€์ผ๋“œ์นด๋“œ ํŒจํ„ด (LIKE)
```sql
SELECT * FROM users WHERE age LIKE '2_';
-- users ์ค‘ 20๋Œ€์ธ ์‚ฌ๋žŒ ์ถœ๋ ฅ 2%๋กœํ•˜๋ฉด 2์‚ด, 200์‚ด๋„ ๋‚˜์˜ด

  • ํ•ด๋‹น ์ •๋ณด์—์„œ ํฌํ•จ์ด ๋˜์–ด์žˆ๋Š”๊ฐ€? ๋“ฑ์„ ์ฐพ์Œ

  • ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰ ๋“ฑ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค (?? : ํŠน์ •ํ•œ ๋ฌธ์ž ๊ฐฏ์ˆ˜ ๋žœ๋ค ๋ฌธ์ž, *: ๊ฐฏ์ˆ˜์™€ ์ƒ๊ด€์—†์ด ์•„๋ฌด ๋ฌธ์ž๋‚˜)

    ํ‘œ์‹œ ์˜ˆ์‹œ ์˜๋ฏธ
    % 2% 2๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ’
      %2 2๋กœ ๋๋‚˜๋Š” ๊ฐ’
      %2% 2๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’
    - _2% ์•„๋ฌด๊ฐ’์ด๋‚˜ ๋“ค์–ด๊ฐ€๊ณ  ๋‘๋ฒˆ์งธ๊ฐ€ 2๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ’
      1____ 1๋กœ ์‹œ์ž‘ํ•˜๊ณ  4์ž๋ฆฌ์ธ ๊ฐ’
      2_%_% 2๋กœ ์‹œ์ž‘ํ•˜๊ณ  ์ ์–ด๋„ 3์ž๋ฆฌ์ธ ๊ฐ’

๋ฐ์ดํ„ฐ ์ •๋ ฌ(ORDER)

ORDER
```sql
SELECT * FROM users ORDER BY age DESC, last_name ASC LIMIT 10;
โ€“ ๋‚˜์ด๊ฐ€ ๋งŽ์€ ์ƒ์œ„ 10๋ช…์˜ ์„ฑ์ˆœ์œผ๋กœ 10๋ช… ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ณด

- ORDER BY column1, column2 [ASC|DESC], ASC = ์˜ฌ๋ฆผ์ฐจ์ˆœ (๊ธฐ๋ณธ๊ฐ’)  ์ž‘์€๊ฒƒ ๋ถ€ํ„ฐ, DESC= ๋‚ด๋ฆผ์ฐจ์ˆœ, ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€๊ฒƒ
1. ํ…Œ์ด๋ธ”๋ช… ์ˆ˜์ •
> 04_DDL_a.sql ํŒŒ์ผ ๋‚ด์šฉ
```sql
DROP TABLE articles;
DROP TABLE news;
CREATE TABLE articles(
    title TEXT NOT NULL,
    content TEXT NOT NULL
);

INSERT INTO articles VALUES('์œค์ค€์„ ๊ตถ์–ด์ฃฝ๋‹ค', '๋ฐฑ์ˆ˜ ์ƒํ™œ ๊ธธ์–ด ์ž”์•ก ๋ถ€์กฑํ•ด... ์ถฉ๊ฒฉ');

SELECT * FROM articles;
ALTER TABLE articles RENAME TO news; -- ํ…Œ์ด๋ธ” ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ

-- created_at์ด๋ผ๋Š” DATETIME ํƒ€์ž…์˜ COLUMN์„ ์ƒˆ๋กœ ๋„ฃ์œผ๋ ค๊ณ  ์‹œ๋„
ALTER TABLE news
-- ADD COLUMN created_at DATETIME NOT NULL; -- ๋น„์–ด์žˆ๋Š” ๊ฐ’์˜ ์ƒˆ๋กœ์šด column์ด ์ƒ๊ธฐ๋ฉด ์•ˆ๋˜๋ฏ€๋กœ ์—๋Ÿฌ ๋ฐœ์ƒ
ADD COLUMN created_at DATETIME NOT NULL DEFAULT 1; -- DEFAULT ๊ฐ’ ์ •ํ•ด์ฃผ์—ˆ์œผ๋ฏ€๋กœ ์—๋Ÿฌ ์•ˆ๋‚จ
.tables

  • ADD COLUMN ๋Œ€์‹  RENAME COLUMN ๋ฐ”๊ฟ€์ปฌ๋Ÿผ TO ๋ฐ”๋€”์ด๋ฆ„ ์œผ๋กœ ์ปฌ๋Ÿผ ์ด๋ฆ„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ
  • ALTER TABLE โ€œtable_nameโ€ Change โ€œcolumn 1โ€ โ€œcolumn 2โ€ [โ€œData Typeโ€]; ํ•œ๊บผ๋ฒˆ์— ํ˜•๋ณ€ํ™˜