Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[50] 230310 SQL Ch. 6 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ SQL: 25๊ฐ• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด, 26๊ฐ• ํ…Œ์ด๋ธ” ์ž‘์„ฑ, ์‚ญ์ œ, ๋ณ€๊ฒฝ, 27๊ฐ• ์ œ์•ฝ, 28๊ฐ• ์ธ๋ฑ์Šค ๊ตฌ์กฐ, 29๊ฐ• ์ธ๋ฑ์Šค ์ž‘์„ฑ๊ณผ ์‚ญ์ œ, 30๊ฐ• ๋ทฐ ์ž‘์„ฑ๊ณผ ์‚ญ์ œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 50์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Python, Flask, SQL

[50] 230310 SQL Ch. 6 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ SQL: 25๊ฐ• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด, 26๊ฐ• ํ…Œ์ด๋ธ” ์ž‘์„ฑ, ์‚ญ์ œ, ๋ณ€๊ฒฝ, 27๊ฐ• ์ œ์•ฝ, 28๊ฐ• ์ธ๋ฑ์Šค ๊ตฌ์กฐ, 29๊ฐ• ์ธ๋ฑ์Šค ์ž‘์„ฑ๊ณผ ์‚ญ์ œ, 30๊ฐ• ๋ทฐ ์ž‘์„ฑ๊ณผ ์‚ญ์ œ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 50์ผ]

yjyuwisely 2023. 3. 10. 12:50

230310 Fri 50th class

Ch. 6 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ SQL

์ง„๋„: p. 245 (27.3 ์ œ์•ฝ ์‚ญ์ œ) ~ (๊ต์žฌ: SQL ์ฒซ๊ฑธ์Œ, ์ €์ž: ์•„์‚ฌ์ด ์•„์ธ ์‹œ)
MySQL Workbench Manual: https://dev.mysql.com/doc/workbench/en/
MySQL ํŠœํ† ๋ฆฌ์–ผ: https://www.tutorialspoint.com/mysql/

SQLD ์š”์•ฝ pdf: https://yurimac.tistory.com/40
์ฑ… ์š”์•ฝ (9p) pdf: https://brunch.co.kr/@ydy702/10

 

SQL ์ฒซ๊ฑธ์Œ | ์•„์‚ฌ์ด ์•„์ธ ์‹œ - ๊ต๋ณด๋ฌธ๊ณ 

SQL ์ฒซ๊ฑธ์Œ | ์™•์ดˆ๋ณด๋ฅผ ์œ„ํ•œ ์ •๋ง ์‰ฌ์šด SQL ์ž…๋ฌธ์„œ๋ฅผ ๋งŒ๋‚˜๋ณด์ž!๋ณธ๊ฒฉ์ ์ธ ๋น…๋ฐ์ดํ„ฐ ์‹œ๋Œ€์— ์ ‘์–ด๋“ค๋ฉด์„œ ๊ธฐ์กด ๋Œ€๋น„ ๋” ํšจ์œจ์ ์ด๊ณ  ํญ๋„“์€ ๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ ์ˆ˜์š”๊ฐ€ ์ปค์งˆ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋˜๋Š” ๊ฐ€์šด๋ฐ, SQ

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


๋ชฐ๋ž๋˜ ๊ฒƒ

VARCHAR Datatype

The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
๊ฒฐ๋ก : varchar2๋กœ ์“ฐ๋Š” ๊ฒŒ ๋‚ซ๋‹ค. 
์ฐธ๊ณ : https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1824

A query in a database is a request for information from a database management system (DBMS), which is the software program that maintains data. Users can make a query to retrieve data or change information in a database, such as adding or removing data.

์ฟผ๋ฆฌ๋Š” ์›น ์„œ๋ฒ„์— ํŠน์ •ํ•œ ์ •๋ณด๋ฅผ ๋ณด์—ฌ๋‹ฌ๋ผ๋Š” ์›น ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ(์ฃผ๋กœ ๋ฌธ์ž์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์š”์ฒญ)์— ์˜ํ•œ ์ฒ˜๋ฆฌ์ด๋‹ค. 


Ch. 6 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด ์ž‘์„ฑ๊ณผ ์‚ญ์ œ

์ฑ…์˜ ๋ชฉ์ฐจ
__25๊ฐ•__๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด
1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด

2. ์Šคํ‚ค๋งˆ

__26๊ฐ•__ํ…Œ์ด๋ธ” ์ž‘์„ฑ, ์‚ญ์ œ, ๋ณ€๊ฒฝ
1. ํ…Œ์ด๋ธ” ์ž‘์„ฑ

2. ํ…Œ์ด๋ธ” ์‚ญ์ œ
3. ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ
4. ALTER TABLE๋กœ ํ…Œ์ด๋ธ” ๊ด€๋ฆฌ

__27๊ฐ•__์ œ์•ฝ
1. ํ…Œ์ด๋ธ” ์ž‘์„ฑ์‹œ ์ œ์•ฝ ์ •์˜

2. ์ œ์•ฝ ์ถ”๊ฐ€
3. ์ œ์•ฝ ์‚ญ์ œ
4. ๊ธฐ๋ณธํ‚ค

__28๊ฐ•__์ธ๋ฑ์Šค ๊ตฌ์กฐ
1. ์ธ๋ฑ์Šค

2. ๊ฒ€์ƒ‰์— ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
3. ์œ ์ผ์„ฑ

__29๊ฐ•__์ธ๋ฑ์Šค ์ž‘์„ฑ๊ณผ ์‚ญ์ œ
1. ์ธ๋ฑ์Šค ์ž‘์„ฑ

2. ์ธ๋ฑ์Šค ์‚ญ์ œ
3. EXPLAIN
4. ์ตœ์ ํ™”

__30๊ฐ•__๋ทฐ ์ž‘์„ฑ๊ณผ ์‚ญ์ œ
1. ๋ทฐ

2. ๋ทฐ ์ž‘์„ฑ๊ณผ ์‚ญ์ œ
3. ๋ทฐ์˜ ์•ฝ์ 

25๊ฐ• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด

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

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด: ํ…Œ์ด๋ธ”์ด๋‚˜ ๋ทฐ, ์ธ๋ฑ์Šค ๋“ฑ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์— ์ •์˜ํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ
์—ฌ๊ธฐ์„œ ๊ฐ์ฒด = ๋ฐ์ดํŠธ๋ฒ ์ด์Šค ๊ฐ์ฒด 
๊ฐ์ฒด: ์‹ค์ฒด๋ฅผ ๊ฐ€์ง€๋Š” ์–ด๋–ค ๊ฒƒ ex) ํ…Œ์ด๋ธ” 

์ด๋ฆ„ ์ œ์•ฝ ์‚ฌํ•ญ(๋ช…๋ช…๊ทœ์น™)
- [ํฌ์ธํŠธ] ์˜๋ฏธ์—†๋Š” ์ด๋ฆ„X
- ๊ธฐ์กด ์ด๋ฆ„์ด๋‚˜ ์˜ˆ์•ฝ์–ด์™€ ์ค‘๋ณตX
- ์ˆซ์ž๋กœ ์‹œ์ž‘X
- ์–ธ๋”์Šค์ฝ”์–ด(_) ์™ธ์˜ ๊ธฐํ˜ธ ํ—ˆ์šฉX
- ํ•œ๊ธ€์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” " "๋”๋ธ”์ฟผํŠธ๋กœ ๋‘˜๋Ÿฌ์‹ผ๋‹ค. 
- ์‹œ์Šคํ…œ์ด ํ—ˆ์šฉํ•˜๋Š” ๊ธธ์ด ์ดˆ๊ณผX


25.2 ์Šคํ‚ค๋งˆ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋Š” ์Šคํ‚ค๋งˆ๋ผ๋Š” ๊ทธ๋ฆ‡์—์„œ ๋งŒ๋“ค์–ด์ง„๋‹ค.
๊ฐ์ฒด์˜ ์ด๋ฆ„์ด ๊ฐ™์•„๋„ ์Šคํ‚ค๋งˆ๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๋ฉด ์ƒ๊ด€ ์—†๋‹ค. (๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด = ์Šคํ‚ค๋งˆ ๊ฐ์ฒด)

The database schema is the structure of a database described in a formal language supported by the database management system. 

์Šคํ‚ค๋งˆ ์„ค๊ณ„: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ…Œ์ด๋ธ”์„ ์ž‘์„ฑํ•ด์„œ ๊ตฌ์ถ•ํ•ด๋‚˜๊ฐ€๋Š” ์ž‘์—… 

์Šคํ‚ค๋งˆ ์˜ˆ์‹œ
ex) MySQL: CREATE DATABASE ๋ช…๋ น์œผ๋กœ ์ž‘์„ฑํ•œ '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค'(์ €์žฅ์žฅ์น˜ ๋‚ด์— ์ •๋ฆฌ๋˜์–ด ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ) 
Oracle: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž๊ฐ€ ๊ณ„์ธต์  ์Šคํ‚ค๋งˆ

๋„ค์ž„์ŠคํŽ˜์ด์Šค(namespace): ์ด๋ฆ„์ด ์ถฉ๋Œํ•˜์ง€ ์•Š๋„๋ก ๊ธฐ๋Šฅํ•˜๋Š” ๊ฒƒ
ex) ์Šคํ‚ค๋งˆ, ํ…Œ์ด๋ธ” 


26๊ฐ• ํ…Œ์ด๋ธ” ์ž‘์„ฑ, ์‚ญ์ œ, ๋ณ€๊ฒฝ

Ctrl + Enter๋กœ ์‹คํ–‰ => SCHEMAS์—์„œ ์ƒˆ๋กœ๊ณ ์นจ ๋ˆ„๋ฆ„ 


๋ฐ์ดํ„ฐ ์œ ํ˜•

CHAR(s): ๊ณ ์ • ๊ธธ์ด ๋ฌธ์ž์—ด ์ •๋ณด, ์ตœ๋Œ€ ๊ธธ์ด๋งŒํผ ๊ณต๊ฐ„ ์ฑ„์›€ ‘AA’ = ‘AA ’
VARCHAR(s) : ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž์—ด ์ •๋ณด, ํ• ๋‹น๋œ ๋ณ€์ˆ˜ ๊ฐ’์˜ ๋ฐ”์ดํŠธ๋งŒ ์ ์šฉ ‘AA’ != ‘AA’
NUMBER: ์ •์ˆ˜, ์‹ค์ˆ˜ ๋“ฑ ์ˆซ์ž ์ •๋ณด
DATE๋‚ ์งœ์™€ ์‹œ๊ฐ ์ •๋ณด

no a b newcol
1 10ABC 2014-01-01 NULL
2 20ABC 2014-01-02 NULL

26.1 ํ…Œ์ด๋ธ” ์ž‘์„ฑ CREATE TABLE ํ…Œ์ด๋ธ”๋ช…

ํ…Œ์ด๋ธ” ์ƒ์„ฑ
create table ํ…Œ์ด๋ธ”๋ช…(
	์—ด ์ •์˜1,
	์—ด ์ •์˜2,
	.....
)

create table sample62(
    no,
    a,
    b,
    newcol
)

์—ด ์ •์˜

์—ด๋ช… ์ž๋ฃŒํ˜• [DEFAULT ๊ธฐ๋ณธ๊ฐ’] [NULL|NOT NULL]
ํ…Œ์ด๋ธ” ์ƒ์„ฑ
create table ํ…Œ์ด๋ธ”๋ช…(
	์—ด์ด๋ฆ„1 ์ž๋ฃŒํ˜• [๊ธฐ๋ณธ๊ฐ’] [NULL|NOT NULL],
	์—ด์ด๋ฆ„2 ์ž๋ฃŒํ˜• [๊ธฐ๋ณธ๊ฐ’] [NULL|NOT NULL],
	.....
)

create table sample62(
    no INTEGER NOT NULL,
    a varchar(30),
    b,
    newcol
)

26.2 ํ…Œ์ด๋ธ” ์‚ญ์ œ DROP TABLE ํ…Œ์ด๋ธ”๋ช… 

ํ…Œ์ด๋ธ”๊ณผ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋„ ํ•จ๊ป˜ ์‚ญ์ œํ•œ๋‹ค. 

DROP TABLE ํ…Œ์ด๋ธ”๋ช…

SQL ๋ช…๋ น์˜ ๊ฒฝ์šฐ ์žฌํ™•์ธ ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์œผ๋‹ˆ ์‹ ์ค‘ํ•˜๊ฒŒ DROP TABLE์„ ์‹คํ–‰ํ•œ๋‹ค. 

- ๋ฐ์ดํ„ฐ ํ–‰ ์‚ญ์ œ

TRUNCATE TABLE ํ…Œ์ด๋ธ”๋ช…

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.


26.3 ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… ADD ์—ด ์ •์˜ (INTERGER)
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… MODIFY ์—ด ์ •์˜ (VARCHAR(20))
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… CHANGE ๊ธฐ์กด์—ด์ด๋ฆ„ ์‹ ๊ทœ์—ด๋ช… ์‹ ๊ทœ์ž๋ฃŒํ˜•
ALTER TABLE ํ…Œ์ด๋ธ”๋ช… DROP ์—ด๋ช…

์—ด ๊ตฌ์„ฑ์„ ๋ณ€๊ฒฝํ•œ๋‹ค. (CREATE TABLE ์‚ฌ์šฉ์ด ์•„๋‹ˆ๋‹ค.)
ex) ์—ด์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ ์ตœ๋Œ€๊ธธ์ด๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค. 

- ์—ด ์ถ”๊ฐ€, ์‚ญ์ œ, ๋ณ€๊ฒฝ
- ์ œ์•ฝ ์ถ”๊ฐ€, ์‚ญ์ œ

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… ๋ณ€๊ฒฝ๋ช…๋ น

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… ADD ์—ด ์ •์˜ -- ์—ด ์ถ”๊ฐ€

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… MODIFY ์—ด ์ •์˜ -- ์—ด ์†์„ฑ ๋ณ€๊ฒฝ

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… CHANGE [๊ธฐ์กด ์—ด ์ด๋ฆ„][์‹ ๊ทœ ์—ด ์ •์˜] -- ์—ด ์ด๋ฆ„ ๋ณ€๊ฒฝ

ALTER TABLE ํ…Œ์ด๋ธ”๋ช… DROP ์—ด๋ช… -- ์—ด ์‚ญ์ œ
create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;				-- ์ˆ˜๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ค‘์—์„œ sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉ
create table sample62(	-- sample62ํ…Œ์ด๋ธ” ์ƒ์„ฑ
	no integer not null,
    a varchar(30),
    b date
);
-- drop table sample62 -- sample62ํ…Œ์ด๋ธ”์„ ์‚ญ์ œ
-- ํ…Œ์ด๋ธ” ๋ณ€๊ฒฝ
alter table sample62 add newcol int; -- newcol์—ด์„ ์ถ”๊ฐ€(add)
alter table sample62 modify a varchar(20); -- a์—ด์˜ ๋ฌธ์ž ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ 30->20์œผ๋กœ ๋ณ€๊ฒฝ(modify) 
alter table sample62 change newcol c varchar(20); -- newcol์—ด์˜ ์ด๋ฆ„์„ c์—ด๋กœ ๋ฐ”๊พธ๊ณ , ํƒ€์ž…์„ int->varchar(20) (change)sample62sample62
desc sample62; -- descending order

insert into sample62(no, a,c) values(1, 'abc','def')

26.4 ALTER TABLE๋กœ ํ…Œ์ด๋ธ” ๊ด€๋ฆฌ p. 240

- ์šฉ๋Ÿ‰์ด ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ ALTER TABLE๋กœ ์—ด์˜ ์ž๋ฃŒํ˜•๋งŒ ๋ณ€๊ฒฝํ•ด ์ตœ๋Œ€ ๊ธธ์ด ์—ฐ์žฅ

๋ฐ์ดํ„ฐ ํ•˜๋‚˜์˜ ํฌ๊ธฐ๋ฅผ ์ตœ์ ํ™”ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ์ €์žฅ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

ex) ๋ฌธ์ž์—ดํ˜•์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜๋Š”๋ฐ, ์ด ์ตœ๋Œ€๊ธธ์ด๋ฅผ ALTER TABLE๋กœ ๋Š˜๋ฆด ์ˆ˜ ์žˆ๋‹ค.

ALTER TABLE sample MODIFY col VARCHAR(30)

์ตœ๋Œ€๊ธธ์ด๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒฝ์šฐ๋Š” ๋งŽ์ง€๋งŒ ์ค„์ด๋Š” ๊ฒฝ์šฐ๋Š” ๋ณ„๋กœ ์—†๋‹ค.

- ํ…Œ์ด๋ธ”์— ALTER TABLE๋กœ ์—ด ์ถ”๊ฐ€

ALTER TABLE sample ADD new_col INTEGER

๋ฌธ์ œ ํ’€์ด)

create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;
create table sample631(
	a int not null,
    b int primary key,
    c varchar(30)
    );


1. ์•„๋ž˜ sql๋ฌธ์žฅ์€ ์‹คํ–‰์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋ฅผ ์ ์–ด๋ณด์„ธ์š”.

create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;
create table sample631(
	a int not null,
    b int primary key,
    c varchar(30)
    );
    
insert into sample631(b,c) values(3,'๊ทธ๋ฆฐ์ด');
select * from sample631;


ํ•ด๊ฒฐ: a๋ฅผ ๋„ฃ์–ด์„œ ์–ด๋–ค ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค. 

2. ์•„๋ž˜ sql๋ฌธ์žฅ์€ ์‹คํ–‰์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋ฅผ ์ ์–ด๋ณด์„ธ์š”.

create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;
create table sample631(
	a int not null,
    b int primary key,
    c varchar(30)
    );
    
insert into sample631(a,b) values(2,2);
select * from sample631;

primary key ํ•ด๋‹น ํ•„๋“œ๊ฐ€ NOT NULL๊ณผ UNIQUE ์ œ์•ฝ ์กฐ๊ฑด์˜ ํŠน์ง•์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ex) ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ

2, 2 ์ค‘๋ณต์ด์–ด์„œ ์•ˆ ๋œ๋‹ค. 

3. ์•„๋ž˜ sql ๋ฌธ์žฅ์ด ์‹คํ–‰๋˜๋Š”์ง€ ์•ˆ ๋˜๋Š”์ง€ ์˜ˆ์ƒํ•ด ๋ณด๊ณ , ๊ทธ ์ด์œ ๋ฅผ ์ ์–ด๋ณด์„ธ์š”

insert into sample631 values(1,3,'๊ทธ๋ฆฐ์ด'); -- ๋œ๋‹ค.
insert into sample631 values(1,3); -- ์•ˆ ๋œ๋‹ค.

๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์—ด์— ๋‹ค ๋„ฃ๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค. 


27๊ฐ• ์ œ์•ฝ

null ์ค‘๋ณต๊ฐ’    
X O not null  ํ•ด๋‹น ํ•„๋“œ๋Š” NULL ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (ํ•„์ˆ˜ ์ž…๋ ฅ) 
O X unique
(๊ณ ์œ ํ‚ค)
ํ•ด๋‹น ํ•„๋“œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.
X X primary key
(๊ธฐ๋ณธํ‚ค)
ํ•ด๋‹น ํ•„๋“œ๊ฐ€ NOT NULL๊ณผ UNIQUE ์ œ์•ฝ ์กฐ๊ฑด์˜ ํŠน์ง•์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
ex) ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ, ํšŒ์›๊ฐ€์ž…ํ•  ๋•Œ ID

์ œ์•ฝ์กฐ๊ฑด: ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ ์œ ์ง€
1. PRIMARY KEY(๊ธฐ๋ณธํ‚ค) : UNIQUE & NOT NULL 
2. UNIQUE KEY(๊ณ ์œ ํ‚ค) : ๊ณ ์œ ํ‚ค ์ •์˜
3. NOT NULL : NULL ๊ฐ’ ์ž…๋ ฅ๊ธˆ์ง€
4. CHECK : ์ž…๋ ฅ ๊ฐ’ ๋ฒ”์œ„ ์ œํ•œ
5. FOREIGN KEY(์™ธ๋ž˜ํ‚ค) : NULL ๊ฐ€๋Šฅ ์—ฌ๋Ÿฌ ์†์„ฑ๊ฐ€๋Šฅ
์ฐธ๊ณ : https://www.w3schools.com/sql/sql_foreignkey.asp

์ œ์•ฝ ์กฐ๊ฑด(constraint): ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ๊ฒ€์‚ฌ ๊ทœ์น™์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์ œ์•ฝ ์กฐ๊ฑด์€ CREATE ๋ฌธ์œผ๋กœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•  ๋•Œ๋‚˜, ALTER ๋ฌธ์œผ๋กœ ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
 
CREATE TABLE ๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ œ์•ฝ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
 
1. NOT NULL : ํ•ด๋‹น ํ•„๋“œ๋Š” NULL ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
2. UNIQUE : ํ•ด๋‹น ํ•„๋“œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.
3. PRIMARY KEY : ํ•ด๋‹น ํ•„๋“œ๊ฐ€ NOT NULL๊ณผ UNIQUE ์ œ์•ฝ ์กฐ๊ฑด์˜ ํŠน์ง•์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
4. FOREIGN KEY : ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์„ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์˜์กดํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
5. DEFAULT : ํ•ด๋‹น ํ•„๋“œ์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

27.1 ํ…Œ์ด๋ธ” ์ž‘์„ฑ ์‹œ ์ œ์•ฝ ์ •์˜


27.2 ์ œ์•ฝ ์ถ”๊ฐ€

create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;
create table sample631(
	a integer not null,
    b integer not null unique,
    c varchar(30)
    );
insert into sample631 (a,b,c) values(1,1,'์ •์ž๋ฐ”');
select * from sample631;
insert into sample631 (a,b,c) values(1,1,'์ •์ž๋ฐ”'); -- Error Code: 1062. Duplicate entry '1' for key 'sample631.b'
create table sample631(
	a int not null,
    b int primary key
    c varchar(30)
    );
    
create table sample631(
	a int not null,
    b int,
    c varchar(30)
    primary key(b) -- ๋‚˜์ค‘์— ์‚ญ์ œ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์ด ํ˜•ํƒœ๋กœ ์“ด๋‹ค.
    );
    
create table sample631(
	a int not null,
    b int,
    c varchar(30)
    constraint ์ œ์•ฝ์กฐ๊ฑด๋ช… primary key(b) -- ์ œ์•ฝ ์ด๋ฆ„์€ constraint ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง€์ •ํ•œ๋‹ค.
    );

- ์—ด ์ œ์•ฝ ์กฐ๊ฑด ์ถ”๊ฐ€
c ์—ด์— NOT NULL ์ œ์•ฝ์„ ์„ค์ •ํ•˜๋Š” ์˜ˆ

ALTER TABLE sample631 MODIFY c VARCHAR (30) NOT NULL;

- ํ…Œ์ด๋ธ” ์ œ์•ฝ ์ถ”๊ฐ€ 
๊ธฐ๋ณธํ‚ค ์ œ์•ฝ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์˜ˆ

ALTER TABLE sample631 ADD CONSTRAINT pkey_sample631 PRIMARY KEY(a);

27.3 ์ œ์•ฝ ์‚ญ์ œ p. 245

create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;
CREATE TABLE sample631 (
    a INT NOT NULL,
    b INT PRIMARY KEY,
    c VARCHAR(30)
);
insert into sample631 values(1,3,'๊ทธ๋ฆฐ์ด');
SELECT * FROM sample631;

-- c์—ด์— not null ์ œ์•ฝ ์ถ”๊ฐ€
alter table sample631 modify c varchar(30) not null;
-- c์—ด์— not null ์ œ์•ฝ ์‚ญ์ œ
alter table sample631 modify c varchar(30);
desc sample631;
-- a์—ด์— primary key(๊ธฐ๋ณธํ‚ค) ์ œ์•ฝ ์ถ”๊ฐ€
alter table sample631 add constraint pkey_sample631 primary key(a);
-- a์—ด์— primary key(๊ธฐ๋ณธํ‚ค) ์ œ์•ฝ ์‚ญ์ œ (์ œ์•ฝ์กฐ๊ฑด์ด๋ฆ„์ด ์žˆ์„ ๊ฒฝ์šฐ: pkey_sample631)
alter table sample631 drop constraint pkey_sample631;
-- a์—ด์— primary key(๊ธฐ๋ณธํ‚ค) ์ œ์•ฝ ์‚ญ์ œ (์ œ์•ฝ์กฐ๊ฑด์ด๋ฆ„์ด ์—†์„ ๊ฒฝ์šฐ)
alter table sample631 drop primary key;
desc sample631;
select * from sample631;

delete from sample631;

27.4 ๊ธฐ๋ณธํ‚ค p. 246

๊ธฐ๋ณธํ‚ค(primary key): ํ•ด๋‹น ํ•„๋“œ๊ฐ€ NOT NULL๊ณผ UNIQUE(์ค‘๋ณต๊ฐ’X) ์ œ์•ฝ ์กฐ๊ฑด์˜ ํŠน์ง•์„ ๋ชจ๋‘ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.
ex) ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ, ํšŒ์›๊ฐ€์ž…ํ•  ๋•Œ ID

๊ธฐ๋ณธํ‚ค ์ œ์•ฝ: ์—ด์„ ๊ธฐ๋ณธํ‚ค๋กœ ์ง€์ •ํ•ด ์œ ์ผํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก ํ•˜๋Š” ๊ตฌ์กฐ, ์œ ์ผ์„ฑ ์ œ์•ฝ์œผ๋กœ๋„ ๋ถˆ๋ฆฐ๋‹ค.

[ํฌ์ธํŠธ] ๊ธฐ๋ณธํ‚ค ์ œ์•ฝ์ด ์„ค์ •๋œ ์—ด์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค. 

create database sample; -- sample ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ
use sample;
-- sample634 ํ…Œ์ด๋ธ” ์ƒ์„ฑ
create table sample634(
	-- p๋ผ๋Š” ์ด๋ฆ„์˜ ์—ด์— null๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ
    p integer not null,
    -- a๋ผ๋Š” ์ด๋ฆ„์€ ์ตœ๋Œ€ ๋ฌธ์ž์—ด์„ 30์œผ๋กœ ์„ค์ •
    a varchar(30),
    -- p์—ด์— ๊ธฐ๋ณธํ‚ค(์ œ์•ฝ์กฐ๊ฑด)๋ฅผ ์„ค์ •ํ•˜๊ณ  ๊ทธ ์ œ์•ฝ์กฐ๊ฑด์˜ ์ด๋ฆ„์„ pkey_smaple634๋กœ ์„ค์ •
    constraint pkey_sample634 primary key(p)
    );
    
insert into sample634 values (1,'์ฒซ์งธ์ค„');
insert into sample634 values (2,'๋‘˜์งธ์ค„');
insert into sample634 values (3,'์…‹์งธ์ค„');

insert into sample634 values (2, '๋„ท์งธ์ค„');
-- Error Code: 1062. Duplicate entry '2' for key 'sample634.PRIMARY' : p์—ด์˜ ๊ฐ’์ด ์ค‘๋ณต์ด๋ฏ€๋กœ

update sample634 set p=2 where p=3; -- where: ํ•„ํ„ฐ๋ง ์กฐ๊ฑด, ์ด ํ•„ํ„ฐ๋ง์ด ์—†์œผ๋ฉด ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ 2๋กœ ๋œ๋‹ค. 
-- Error Code: 1062. Duplicate entry '2' for key 'sample634.PRIMARY' : p = 2์ธ ์—ด์ด ๋‘ ๊ฐœ๋‚˜ ์กด์žฌํ•˜๋ฏ€๋กœ 

select * from sample634;

- ๋ณต์ˆ˜์˜ ์—ด๋กœ ๊ธฐ๋ณธํ‚ค ๊ตฌ์„ฑํ•˜๊ธฐ p. 248
= p์—ด, a์—ด์„ ํ•ฉ์ณ์„œ ๊ธฐ๋ณธํ‚ค ๊ตฌ์„ฑํ•˜๊ธฐ (์Šˆํผํ‚ค)

ํ‚ค์˜ ์ข…๋ฅ˜ (์ •์ฒ˜๊ธฐ์— ๋‚˜์˜ด, ์ฐธ๊ณ : ์‹œ๋‚˜๊ณต ์ •์ฒ˜๊ธฐ ์š”์•ฝ์ •๋ฆฌ 173๋ฒˆ)

ํ›„๋ณดํ‚ค(Candidate Key): ๊ธฐ๋ณธํ‚ค๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์—ด ex) ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ, ํ•™๋ฒˆ
•๋ฆด๋ ˆ์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ์†์„ฑ๋“ค ์ค‘์—์„œ ํŠœํ”Œ์„ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ๋“ค์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ, ์ฆ‰ ๊ธฐ๋ณธํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ๋“ค์„ ๋งํ•จ
•ํ›„๋ณดํ‚ค๋Š” ๋ฆด๋ ˆ์ด์…˜์— ์žˆ๋Š” ๋ชจ๋“  ํŠœํ”Œ์— ๋Œ€ํ•ด์„œ ์œ  ์ผ์„ฑ๊ณผ ์ตœ์†Œ์„ฑ์„ ๋งŒ์กฑ์‹œ์ผœ์•ผ ํ•จ

๋Œ€์ฒดํ‚ค(Alternate Key): ๊ธฐ๋ณธํ‚ค๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ํ‚ค ex) ํ•™๋ฒˆ 
•ํ›„๋ณดํ‚ค๊ฐ€ ๋‘˜ ์ด์ƒ์ผ ๋•Œ ๊ธฐ๋ณธํ‚ค๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ํ›„ ๋ณดํ‚ค๋ฅผ ์˜๋ฏธํ•จ
•๋ณด์กฐํ‚ค๋ผ๊ณ ๋„ํ•จ


๊ธฐ๋ณธํ‚ค: ํ•˜๋‚˜์˜ ์—ด๋กœ ๊ตฌ์„ฑ๋œ ์œ ์ผ์„ฑ์„ ๊ฐ–์ถ˜ ํ‚ค 
•ํ›„๋ณดํ‚ค ์ค‘์—์„œ ํŠน๋ณ„ํžˆ ์„ ์ •๋œ ์ฃผํ‚ค(Main Key)๋กœ ์ค‘ ๋ณต๋œ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†์Œ
•ํ•œ ๋ฆด๋ ˆ์ด์…˜์—์„œ ํŠน์ • ํŠœํ”Œ์„ ์œ ์ผํ•˜๊ฒŒ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ
•๊ธฐ๋ณธํ‚ค๋Š” NULL ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค. ์ฆ‰ ํŠœํ”Œ์—์„œ ๊ธฐ ๋ณธํ‚ค๋กœ ์„ค์ •๋œ ์†์„ฑ์—๋Š” NULL ๊ฐ’์ด ์žˆ์–ด์„œ๋Š” ์•ˆ ๋จ

์Šˆํผํ‚ค(Super Key): ๋‘ ๊ฐœ ์ด์ƒ์˜ ์—ด์„ ํ•ฉ์ณ์„œ ๊ตฌ์„ฑ๋œ ์œ ์ผ์„ฑ์„ ๊ฐ–์ถ˜ ํ‚ค
•ํ•œ ๋ฆด๋ ˆ์ด์…˜ ๋‚ด์— ์žˆ๋Š” ์†์„ฑ๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ํ‚ค๋กœ์„œ ๋ฆด๋ ˆ์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ํŠœํ”Œ๋“ค ์ค‘ ์Šˆํผ ํ‚ค๋กœ ๊ตฌ์„ฑ๋œ ์†์„ฑ์˜ ์ง‘ํ•ฉ๊ณผ ๋™์ผํ•œ ๊ฐ’์€ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์Œ
•์Šˆํผํ‚ค๋Š” ๋ฆด๋ ˆ์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ํŠœํ”Œ์— ๋Œ€ํ•ด ์œ ์ผ์„ฑ์€ ๋งŒ์กฑ์‹œํ‚ค์ง€๋งŒ, ์ตœ์†Œ์„ฑ์€ ๋งŒ์กฑ์‹œํ‚ค์ง€ ๋ชปํ•จ


์™ธ๋ž˜ํ‚ค(Foreign Key)
•๋‹ค๋ฅธ ๋ฆด๋ ˆ์ด์…˜์˜ ๊ธฐ๋ณธํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์†์„ฑ ๋˜๋Š” ์†์„ฑ๋“ค์˜ ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•จ
•ํ•œ ๋ฆด๋ ˆ์ด์…˜์— ์†ํ•œ ์†์„ฑ A์™€ ์ฐธ์กฐ ๋ฆด๋ ˆ์ด์…˜์˜ ๊ธฐ๋ณธ ํ‚ค์ธ B๊ฐ€ ๋™์ผํ•œ ๋„๋ฉ”์ธ ์ƒ์—์„œ ์ •์˜๋˜์—ˆ์„ ๋•Œ์˜ ์†์„ฑ A๋ฅผ ์™ธ๋ž˜ํ‚ค๋ผ๊ณ  ํ•จ

์˜ˆ์ œ) a์—ด๊ณผ b์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธฐ๋ณธํ‚ค

create table sample635(
	a int not null,
    b int not null,
    constraint skey_sample635 primary key(a,b)
    );
    
desc sample635;
insert into sample635 values(1,1);
insert into sample635 values(1,2);
insert into sample635 values(1,3);
insert into sample635 values(2,1);
insert into sample635 values(2,2);

select * from sample635;

๊ฒฐ๊ณผ)

a b
1 1
1 2
1 3
2 1
2 2

2๊ฐœ ๋ฌถ์Œ์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค. (1,1), (1,2), (1,3), (2,1), (2,2)


28๊ฐ• ์ธ๋ฑ์Šค ๊ตฌ์กฐ

28.1 ์ธ๋ฑ์Šค

์ธ๋ฑ์Šค: ํ…Œ์ด๋ธ”์— ๋ถ™์—ฌ์ง„ ์ƒ‰์ธ


28.2 ๊ฒ€์ƒ‰์— ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ธ๋ฑ์Šค์— ์“ฐ์ด๋Š” ๋Œ€ํ‘œ์ ์ธ ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜
ex) ์ด์ง„ ํŠธ๋ฆฌ(binary tree), ํ•ด์‹œ

์ด์ง„ํƒ์ƒ‰(binary search): ์ง‘ํ•ฉ์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„์–ด ์กฐ์‚ฌํ•˜๋Š” ๊ฒ€์ƒ‰๋ฐฉ๋ฒ•
[ํฌ์ธํŠธ] ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•  ๋•Œ๋Š” ์ด์ง„ ํƒ์ƒ‰์ด ๋น ๋ฅด๋‹ค!

์ด์ง„ํŠธ๋ฆฌ(binary tree): ํ…Œ์ด๋ธ”์— ์ธ๋ฑ์Šค๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ์™€ ๋ณ„๊ฐœ๋กœ ์ธ๋ฑ์Šค์šฉ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ์žฅ์น˜์— ๋งŒ๋“ค์–ด์ง€๋ฉฐ ์ด์ง„ ํŠธ๋ฆฌ๋ผ๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์ž‘์„ฑ๋œ๋‹ค. 


28.3 ์œ ์ผ์„ฑ 

Point: ์ด์ง„ ํŠธ๋ฆฌ์—๋Š” ์ค‘๋ณตํ•˜๋Š” ๊ฐ’์„ ๋“ฑ๋กํ•  ์ˆ˜ ์—†๋‹ค.


29๊ฐ• ์ธ๋ฑ์Šค ์ž‘์„ฑ๊ณผ ์‚ญ์ œ p. 257

CREATE INDEX
DROP INDEX
-- ์ธ๋ฑ์Šค ์ƒ์„ฑ
-- sample62 ํ…Œ์ด๋ธ”์˜ no ์—ด์— isample65๋ผ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•œ๋‹ค. 
create index isample65 on sample62(no);

select * from sample62; #index๋ฅผ ์ „ํ˜€ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š์Œ.

-- explain ์ธ๋ฑ์Šค ์‚ฌ์šฉ ์—ฌ๋ถ€ ์กฐํšŒ
-- possible_keys์—ด, key์—ด์ด null์ด๋ฉด index๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค๋Š” ๊ฑฐ์ž„. 
explain select * from sample62; 

select * from sample62 where a='abc'; -- index์‚ฌ์šฉํ•ด์„œ ๊ฒ€์ƒ‰

explain select * from sample62 where a='abc';
desc sample62;

29.1 ์ธ๋ฑ์Šค ์ž‘์„ฑ

๋ฌธ๋ฒ•

CREATE INDEX ์ธ๋ฑ์Šค๋ช… ON ํ…Œ์ด๋ธ”๋ช… (์—ด๋ช…1, ์—ด๋ช…2, ...)

์˜ˆ์ œ) ์ธ๋ฑ์Šค ์ž‘์„ฑํ•˜๊ธฐ

-- sample62 ํ…Œ์ด๋ธ”์˜ no ์—ด์— isample65๋ผ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•œ๋‹ค. 
CREATE INDEX isample65 ON sample62(no);

29.2 ์ธ๋ฑ์Šค ์‚ญ์ œ

๋ฌธ๋ฒ•

-- DROP INDEX (์Šคํ‚ค๋งˆ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ)
DROP INDEX ์ธ๋ฑ์Šค๋ช…

-- DROP INDEX(ํ…Œ์ด๋ธ” ๋‚ด ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ)
DROP INDEX ์ธ๋ฑ์Šค๋ช… ON ํ…Œ์ด๋ธ”๋ช…

์˜ˆ์ œ) ์ธ๋ฑ์Šค ์‚ญ์ œํ•˜๊ธฐ

DROP INDEX isample65 ON sample62;

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

CREATE INDEX isample65 ON sample62(a);

WHERE ๊ตฌ์— a ์—ด์— ๋Œ€ํ•œ ์กฐ๊ฑด์‹์„ ์ง€์ •ํ•œ ๊ฒฝ์šฐ SELECT ๋ช…๋ น์€ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ WHERE ๊ตฌ์˜ ์กฐ๊ฑด์‹์— a ์—ด์ด ์ „ํ˜€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฉด SELECT ๋ช…๋ น์€ isample62๋ผ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

SELECT * FROM sample62 WHERE a = 'a';

29.3 EXPLAIN

์‹ค์ œ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒ€์ƒ‰ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค. 

๋ฌธ๋ฒ•

EXPLAIN SQL ๋ช…๋ น

์˜ˆ์‹œ) EXPLAIN์œผ๋กœ ์ธ๋ฑ์Šค ์‚ฌ์šฉ ํ™•์ธํ•˜๊ธฐ1 (MySQL)

EXPLAIN SELECT * FROM sample62 WHERE a = 'a';

29.4 ์ตœ์ ํ™”

์‹คํ–‰๊ณ„ํš์—์„œ๋Š” ์ธ๋ฑ์Šค์˜ ์œ ๋ฌด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์—ฌ๋ถ€์— ๋Œ€ํ•ด์„œ๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด๋ถ€์˜ ์ตœ์ ํ™” ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ํŒ๋‹จ๋œ๋‹ค. 


30๊ฐ• ๋ทฐ ์ž‘์„ฑ๊ณผ ์‚ญ์ œ p. 263

CREATE DATABASE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…
CREATE TABLE ํ…Œ์ด๋ธ”๋ช…
CREATE INDEX ์ธ๋ฑ์Šค๋ช…
CREATE VIEW ๋ทฐ๋ช… AS SELECT์ ˆ 

SELECT * FROM ํ…Œ์ด๋ธ”๋ช…

์„œ๋ธŒ์ฟผ๋ฆฌ = ์ฟผ๋ฆฌ ์•ˆ์— ์ฟผ๋ฆฌ๋ฅผ ๋„ฃ์Œ 
SELECT (SELECT * FROM ํ…Œ์ด๋ธ”๋ช…) FROM ํ…Œ์ด๋ธ”๋ช… - ์„œ๋ธŒ์ฟผ๋ฆฌ (์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ)

SELECT * FROM (SELECT * FROM ํ…Œ์ด๋ธ”๋ช…) A - ์„œ๋ธŒ์ฟผ๋ฆฌ (์ธ๋ผ์ธ ๋ทฐ)

SELECT * FROM ํ…Œ์ด๋ธ”๋ช… WHERE A = (SELECT * FROM ํ…Œ์ด๋ธ”๋ช…) - ์„œ๋ธŒ์ฟผ๋ฆฌ (์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ - WHERE ์”€)

30.1 ๋ทฐ

๋ทฐ : ํ…Œ์ด๋ธ”์€ ์‹ค์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ˜๋ฉด, ๋ทฐ๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋‹ค.
๊ฐ€์ƒํ…Œ์ด๋ธ”์ด๋ผ๊ณ ๋„ ํ•จ.
์‹คํ–‰ ์‹œ์ ์— SQL ์žฌ์ž‘์„ฑํ•˜์—ฌ ์ˆ˜ํ–‰๋จ.
์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋ทฐ๋ฅผ ์“ด๋‹ค. 

๋ฌธ๋ฒ•

CREATE VIEW ๋ทฐ๋ช… AS SELECT ๋ช…๋ น
DROP VIEW ๋ทฐ๋ช…
SELECT * FROM sample635;

-- CREATE VIEW AS
CREATE VIEW sample_view_365 AS SELECT * FROM sample635;
SELECT * FROM V1
#์•„๋ž˜ ์ฒซ ๋ฌธ๋‹จ ์ฝ”๋“œ๋ž‘ ๊ฐ™๋‹ค. SELECT์ ˆ์„ ๋‹จ์ˆœํ™”์‹œํ‚จ๋‹ค.
SELECT *
FROM (SELECT *
		FROM TB1
        WHERE col1 = (SELECT * FROM TB2 WHERE col2 = 'b')
        AND col3 = 'c'
        ORDER BY c1
        GROUP BY c1
        HAVING c4 >= 100
)

CREATE VIEW V1 AS
SELECT *
FROM (SELECT *
		FROM TB1
        WHERE col1 = (SELECT * FROM TB2 WHERE col2 = 'b')
        AND col3 = 'c'
        ORDER BY c1
        GROUP BY c1
        HAVING c4 >= 100
)

Point: ๋ทฐ๋Š” SELECT ๋ช…๋ น์„ ๊ธฐ๋กํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋‹ค.

Point: ๋ทฐ๋Š” ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณต์žกํ•œ SELECT ๋ช…๋ น์„ ๊ฐ„๋žตํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.


30.2 ๋ทฐ ์ž‘์„ฑ๊ณผ ์‚ญ์ œ

- ๋ทฐ์˜ ์ž‘์„ฑ

CREATE VIEW ๋ทฐ๋ช… AS SELECT ๋ช…๋ น

์˜ˆ์ œ) ๋ทฐ ์ž‘์„ฑํ•˜๊ธฐ

CREATE VIEW sample_veiw_67 AS SELECT * FROM sample54;
SELECT * FROM sample_view_67;

CREATE VIEW์—์„œ ์—ด ์ง€์ •ํ•˜๊ธฐ

CREATE VIEW ๋ทฐ๋ช… (์—ด๋ช…1, ์—ด๋ช…2, ...) AS SELECT ๋ช…๋ น

์˜ˆ์ œ) ์—ด์„ ์ง€์ •ํ•ด ๋ทฐ ์ž‘์„ฑํ•˜๊ธฐ

CREATE VIEW sample_view_672(n, v, v2) AS
	SELECT no, a, a*2 FROM sample54;
SELECT * FROM sample_view_672 WHERE n = 1;

- ๋ทฐ ์‚ญ์ œ

DROP VIEW ๋ทฐ๋ช…

์˜ˆ์ œ) ๋ทฐ ์‚ญ์ œํ•˜๊ธฐ

DROP VIEW sample_view_67;

30.3 ๋ทฐ์˜ ์•ฝ์ 

- ๋จธ๋ฆฌํ‹ฐ์–ผ๋ผ์ด์ฆˆ๋“œ ๋ทฐ (Materialized View)
๋ฐ์ดํ„ฐ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์ €์žฅ์žฅ์น˜์— ์ €์žฅํ•ด๋‘๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.
๋‹จ์ : MySQL์—์„œ๋Š” ์‚ฌ์šฉX
Oracle, DB2์—์„œ๋งŒ ์‚ฌ์šฉO

- ํ•จ์ˆ˜ ํ…Œ์ด๋ธ” 
๋ถ€๋ชจ ์ฟผ๋ฆฌ์™€ ์–ด๋–ค ์‹์œผ๋กœ๋“  ์—ฐ๊ด€๋œ ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ทฐ์˜ SELECT ๋ช…๋ น์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋ฅผ ํ•จ์ˆ˜ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ํšŒํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.


728x90
๋ฐ˜์‘ํ˜•
Comments