Sunday_August_23
.
.
.
๋ฒŒ์จ ๊ธฐ์—… ํ˜‘์—…์„ ํ•œ์ง€ ์ผ์ฃผ์ผ์ด ์ง€๋‚ฌ๋‹ค.

์›๋ž˜ ์œ„์ฝ”๋“œ์—์„œ๋Š” Django๋ผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ,
๊ธฐ์—… ํ˜‘์—…์„ ์ง„ํ–‰์ค‘์ธ '๋ธŒ๋žœ๋””'๋ผ๋Š” ํšŒ์‚ฌ์—์„œ ๋ฐฑ์—”๋“œ๋Š”
Flask๋ผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๊ณ  ์žˆ์–ด์„œ ์ด๋ฒˆ์— ์ƒˆ๋กญ๊ฒŒ Flask๋ฅผ ๊ณต๋ถ€ํ•˜๊ฒŒ ๋๋‹ค.

๊ทธ๋ž˜์„œ ์›”ํด ์€์šฐ๋‹˜์ด ์“ฐ์‹  ๊น”๋”ํ•œ ํŒŒ์ด์ฌ ํƒ„ํƒ„ํ•œ ๋ฐฑ์—”๋“œ, ์ผ๋ช… '๊น”ํŒŒํƒ„๋ฐฑ'

์ด ์ฑ…์„ ๋ณด๋ฉด์„œ Flask๋ฅผ ๊ณต๋ถ€์ค‘์ธ๋ฐ ์ด ์ฑ…์—์„œ๋Š” ORM์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
์ฒ˜์Œ๋ถ€ํ„ฐ ORM์„ ์‚ฌ์šฉํ•˜๋ฉด SQL์„ ๋ฐฐ์šธ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์™œ๋ƒํ•˜๋ฉด ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž์—๊ฒŒ SQL์€ ์ง„์งœ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

(ORM์ด๋ž€ ์‰ฝ๊ฒŒ ๋งํ•ด, ์ฟผ๋ฆฌ๋ฌธ์ด ์•„๋‹Œ ํŒŒ์ด์ฌ์˜ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด DB์™€ ๋ฐ์ดํ„ฐ๋ฅผ๋ฅผ ์‰ฝ๊ฒŒ ๊ฑด๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์ข‹์€ ์นœ๊ตฌ์ž…๋‹ˆ๋‹ค ๐Ÿ˜€ )

๊ทธ๋ฆฌ๊ณ  ์ƒํ™ฉ ๋•Œ๋ฌธ์— Table์„ ๋จผ์ € ์กฐ์ž‘ํ•˜๋Š” DDL์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ๋‹ค๋ฃจ๋Š”
DML, ๊ทธ ์ค‘์—์„œ ๋จผ์ € ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” insert์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ํ˜„์žฌ 1๊ฐœ๊ฐ€ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค.

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | ์ƒํ’ˆ1    | 23200.00 |            20 |
+----+-----------------+---------+----------+---------------+
1 row in set (0.00 sec)

โž• ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ Insert

๋จผ์ € ํ˜„์žฌ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

mysql> desc products;
+-----------------+---------------+------+-----+---------+----------------+
| Field           | Type          | Null | Key | Default | Extra          |
+-----------------+---------------+------+-----+---------+----------------+
| id              | int           | NO   | PRI | NULL    | auto_increment |
| thumbnail_image | varchar(2000) | NO   |     | NULL    |                |
| name            | varchar(500)  | NO   |     | NULL    |                |
| price           | decimal(10,2) | NO   |     | NULL    |                |
| discount_rate   | int           | YES  |     | NULL    |                |
+-----------------+---------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
  • pk์ธ id๋Š” auto_increment์ด๋ฏ€๋กœ ๊ตณ์ด ์•ˆ ๋„ฃ์–ด์ค˜๋„ ๋ฉ๋‹ˆ๋‹ค.
  • thumbnail_image๋Š” varchar(string)
  • name๋„ varchar(string)
  • price๋Š” decimal์ด๋ผ๋Š” ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
  • discount_rate๋Š” int ํƒ€์ž…์ด๊ณ  null=True ์ž…๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ์˜ ํ–‰์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ insert๋ผ๋Š” ๋ช…๋ น์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.

  1. ์ปฌ๋Ÿผ์˜ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋“ค์„ ๋„ฃ์–ด์ฃผ๊ธฐ
  2. ์ปฌ๋Ÿผa=๋ฐ์ดํ„ฐ1, ์ปฌ๋Ÿผb=๋ฐ์ดํ„ฐ2 ์ด๋Ÿฐ์‹.
INSERT INTO products VALUES (๊ฐ’1, ๊ฐ’2, ..., ๊ฐ’N)

๋จผ์ € 1๋ฒˆ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ ๋ฐฉ๋ฒ•

mysql> insert into products values (2, 'image_url2', '์ƒํ’ˆ2', '17200.00', 10);
Query OK, 1 row affected (0.02 sec)

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | ์ƒํ’ˆ1    | 23200.00 |            20 |
|  2 | image_url2      | ์ƒํ’ˆ2    | 17200.00 |            10 |
+----+-----------------+---------+----------+---------------+
2 rows in set (0.00 sec)

์ž˜ ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ €๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์œผ๋ฉด์„œ ์•Œ๊ฒŒ ๋œ ์‚ฌ์‹ค์ธ๋ฐ, ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค„ ๋•Œ์—๋Š” id๊ฐ’๋„ ๋„ฃ์–ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด positionalํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

mysql> insert into products values ('image_url2', '์ƒํ’ˆ2', 17200.00, 10);
ERROR 1136 (21S01): Column count doesn't match value count at row 1

์ด์ฒ˜๋Ÿผ ํ•˜๋‚˜๊ฐ€ ๋ถ€์กฑํ•˜๋‹ค๊ณ  ๋‚˜์˜ต๋‹ˆ๋‹ค.

์ž ๊ทธ๋Ÿผ 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

2๏ธโƒฃ ๋ฐฉ๋ฒ•

์ผ๋‹จ ๋ฐฉ๋ฒ•์€ 1๋ฒˆ๊ณผ ๋‹ค๋ฅผ๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ปฌ๋Ÿผ(ํ•„๋“œ)๊นŒ์ง€ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

INSERT INTO products (thumbnail_image, name, price, discount_rate) VALUES ('image_url3', '์ƒํ’ˆ3', 13500, 30);
mysql> insert into products (thumbnail_image, name, price, discount_rate) values ('image_url3', '์ƒํ’ˆ3', 13500, 30);
Query OK, 1 row affected (0.01 sec)

ํ™•์ธํ•ด๋ณผ๊นŒ์š”?

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | ์ƒํ’ˆ1    | 23200.00 |            20 |
|  2 | image_url2      | ์ƒํ’ˆ2    | 17200.00 |            10 |
|  3 | image_url3      | ์ƒํ’ˆ3    | 13500.00 |            30 |
+----+-----------------+---------+----------+---------------+
3 rows in set (0.00 sec)

์˜ค ใ…‹ใ…‹ ๋งค์šฐ ์ž˜ ๋“ค์–ด๊ฐ”๊ตฐ์š”.

โ—๏ธ ๊ทธ๋ฆฌ๊ณ  ๋˜ ์ด๋ ‡๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์œผ๋ฉด์„œ ํ•˜๋‚˜๋ฅผ ๊นจ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค.

โ—๏ธ Decimal ํƒ€์ž…์€ '123.00' ์ด๋ ‡๊ฒŒ ์ŠคํŠธ๋งํƒ€์ž… + ๋’ค์— .00 ์ด๋Ÿฐ๊ฒƒ์„ ์•ˆ์ฃผ๊ณ  ๊ทธ๋ƒฅ int ํ˜•์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค˜๋„ ์•Œ์•„์„œ ์ธ์‹์„ ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜€

๋งˆ์ง€๋ง‰์œผ๋กœ 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ํ•œ๋ฒˆ ๋” ์จ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋ด…์‹œ๋‹ค.
๋Œ€์‹ ์— ์ด๋ฒˆ์—” ์ปฌ๋Ÿผ์˜ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋ฐ”๊ฟ”๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

mysql> insert into products (discount_rate, price, name, thumbnail_image) values (15, 19000, '์ƒํ’ˆ4', 'image_url4');
Query OK, 1 row affected (0.01 sec)

mysql> select * from products;
+----+-----------------+---------+----------+---------------+
| id | thumbnail_image | name    | price    | discount_rate |
+----+-----------------+---------+----------+---------------+
|  1 | image_url1      | ์ƒํ’ˆ1    | 23200.00 |            20 |
|  2 | image_url2      | ์ƒํ’ˆ2    | 17200.00 |            10 |
|  3 | image_url3      | ์ƒํ’ˆ3    | 13500.00 |            30 |
|  4 | image_url4      | ์ƒํ’ˆ4    | 19000.00 |            15 |
+----+-----------------+---------+----------+---------------+
4 rows in set (0.00 sec)

๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€, 1๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์˜ ์ˆœ์„œ์— ๋งž๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 2๋ฒˆ ๋ฐฉ๋ฒ•์€ ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋ฌผ๋ก  2๋ฒˆ๋ฐฉ๋ฒ•์—์„œ ์ •ํ•ด์ค€ ์ปฌ๋Ÿผ์˜ ์ˆœ์„œ์— ๋งž๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

โ—๏ธ ๊ทธ๋ฆฌ๊ณ  1๋ฒˆ์˜ ๋ฐฉ๋ฒ•์€ positional ํ•˜๊ธฐ ๋•Œ๋ฌธ์— id์˜ ๊ฐ’๋„ ๋„ฃ์–ด์ค˜์•ผ ํ•˜์ง€๋งŒ, 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ keyword ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์— id์˜ ๊ฐ’์€ auto๊ฐ€ ์ ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค.

profile
๐Ÿ‡ Rabbit can take a rest, but ๐Ÿข turtle can't

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