Skip to content

Commit

Permalink
feat: [DB] View ์ •๋ฆฌ (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaeyeon0130 authored Jan 4, 2025
1 parent be81b0b commit 7d14bc2
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions DB/View.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# View

- DB์—์„œ ๋ทฐ๋Š” ํ•˜๋‚˜ ๋˜๋Š” ๊ทธ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์„ ์กฐ์ธํ•˜์—ฌ ๋งŒ๋“  ์ƒˆ๋กœ์šด ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์„ ์˜๋ฏธํ•œ๋‹ค.
- ์‹ค์ œ๋กœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ ๊ฑด ์•„๋‹ˆ์ง€๋งŒ, ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ์‹ค์กด ํ…Œ์ด๋ธ”๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค.
- ๋ทฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒˆ๋กœ์šด ๋ทฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.
- ์ด๋Š” ์ฃผ๋กœ ํŠน์ • ์ •๋ณด๋งŒ ์ œ๊ณตํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๋‚˜ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”๋กœ ๋งŒ๋“ค์–ด ๊ฐ„ํŽธํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์—…๋ฌด์—์„œ๋„ ์ž์ฃผ ์ด์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด,

๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.
`user`, `payment`.
๊ฐœ๋ฐœ์ž๊ฐ€ ํšŒ์›๋ณ„ ๊ฒฐ์ œ ์ด๋ ฅ์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ์€๋ฐ, ํšŒ์‚ฌ ๊ทœ์ •์ƒ ๋ฏผ๊ฐํ•œ ๊ฐœ์ธ์ •๋ณด๋Š” ์ œ๊ณตํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ทฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ œ๊ณตํ•˜๋ฉด ๋œ๋‹ค.
user ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ฏผ๊ฐํ•œ ๊ฐœ์ธ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์ปฌ๋Ÿผ์„ ์ œ์™ธํ•˜๊ณ  payment ํ…Œ์ด๋ธ”์—๋„ ์นด๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ œ์™ธํ•œ ์ปฌ๋Ÿผ๋งŒ ์กฐ์ธํ•ด์„œ ๋ทฐ๋ฅผ ๋งŒ๋“ค์–ด ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•ด๋‹น ๋ทฐ๋ฅผ ์ ‘๊ทผํ•˜๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.

# View ์‚ฌ์šฉ๋ฒ•
์˜ˆ) <p>
`user` : id, name, email, phone_number <p>
`payment` : id, user_id, payment_date, amount, card_number

<br>

```angular2html
# ๋ทฐ ๋„ค์ด๋ฐ ๊ทœ์น™ : ์ ‘๋‘์‚ฌ "v"๋ฅผ ๋ถ™์ธ๋‹ค.
CREATE VIEW v_user_payment AS
SELECT
u.id AS user_id,
u.name,
p.payment_date,
p.amount
FROM
user u
JOIN
payment p
ON
u.id = p.user_id;
```
์œ„์™€ ๊ฐ™์ด, ๋ฏผ๊ฐํ•œ ์ •๋ณด(phone_number, card_number ๋“ฑ)๋ฅผ ์ œ์™ธํ•˜๊ณ ,
`user`์˜ ์ด๋ฆ„๊ณผ `payment`์˜ ๊ฒฐ์ œ ๋‚ด์—ญ๋งŒ ํฌํ•จํ•œ ๋ทฐ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

<br>

```angular2html
DROP VIEW v_user_payment
```
๋ทฐ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ณ ,

<br>


```angular2html
SELECT name, payment_date, amount
FROM v_user_payment
WHERE user_id = 1234;
```
์ผ๋ฐ˜์ ์œผ๋กœ ํ…Œ์ด๋ธ”์„ `SELECT`ํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ทฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

<br>

๋ทฐ๋ฅผ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์ฟผ๋ฆฌ ๋งˆ์ง€๋ง‰์— `with read only`๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด `DML(INSERT, UPDATE, DELETE)`์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ์— ์™ธ๋ถ€์— ์ œ๊ณตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์€ ์˜ต์…˜์ด๋‹ค.
```angular2html
ALTER VIEW v_user_payment READ ONLY;
or
CREATE VIEW v_user_payment AS
/* query */
WITH READ ONLY;
```

# View ์žฅ๋‹จ์ 
๋ทฐ์˜ ์žฅ์ 

- ๋ฐ์ดํ„ฐ ์กฐํšŒ๊ฐ€ ์šฉ์ดํ•˜๋‹ค. (๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ˆœํ™”)
- ์‚ฌ์šฉ์ž๋ณ„ ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ณด์•ˆ ์ด์ )
- ๋ฌผ๋ฆฌ์ ์ธ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ทฐ์˜ ๋‹จ์ 

- ๋ทฐ์— ์ธ๋ฑ์Šค๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.
- ๋ทฐ๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ทฐ๋ฅผ ๋งŒ๋“  ๊ฒฝ์šฐ ์—ฐ๊ด€ ๋ทฐ๋ฅผ ์‚ญ์ œํ•˜๋ฉด ์ƒ์„ฑ๋œ ๋ทฐ๋„ ์‚ญ์ œ๋œ๋‹ค.
- ํ•œ๋ฒˆ ์ •์˜๋œ ๋ทฐ๋Š” ์ˆ˜์ •์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.

0 comments on commit 7d14bc2

Please sign in to comment.