SQL

SQL ์ž‘์„ฑํ˜•์‹

1. SQL ๋ฌธ ๋์—๋Š” ํ•ญ์ƒ ์„ธ๋ฏธ์ฝœ๋ก ์„ ์จ์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

: SQL ๋ฌธ๋ฒ• ์ƒ ์„ธ๋ฏธ์ฝœ๋ก ์ด ํ•˜๋‚˜์˜ SQL ๋ฌธ์„ ์ข…๊ฒฐํ•˜๋Š” ๋‹จ์œ„๋‹ค.

2.SQL ๋ฌธ ์•ˆ์—๋Š” ๊ณต๋ฐฑ์ด๋‚˜ ๊ฐœํ–‰ ๋“ฑ์„ ์ž์œ ๋กญ๊ฒŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

:์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์“ฐ๋“ , ๊ตฌ๋ถ„๋˜์–ด์•ผํ•  ํ‚ค์›Œ๋“œ๋“ค์ด ์ตœ์†Œํ•œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๊ณ , ์„ธ๋ฏธ์ฝœ๋ก ์œผ๋กœ ๋งˆ๋ฌด๋ฆฌ๋˜์–ด ์žˆ์œผ๋ฉด ์‹คํ–‰์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

3.SQL ๋ฌธ์˜ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ๋ฌธ์ž

:SQL ๋ฌธ์„ ๋ณด๋ฉด, MySQL์— ๊ธฐ๋ณธ์œผ๋กœ ๋‚ด์žฅ๋œ ํ‚ค์›Œ๋“œ๋“ค(์ด๋Ÿฐ ๊ฒƒ๋“ค์„ ๋ณดํ†ต โ€˜์˜ˆ์•ฝ์–ดโ€™๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.)์€ ๋Œ€๋ฌธ์ž๋กœ ์จ์ฃผ๊ณ , ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ์†Œ๋ฌธ์ž๋กœ ์“ด๋‹ค.

4.๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„๊ณผ ํ…Œ์ด๋ธ” ์ด๋ฆ„

: ์‹ค๋ฌด์—์„œ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—, ๊ฐ™์€ ์ด๋ฆ„์˜ ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…'. 'ํ…Œ์ด๋ธ”๋ช…' ํ˜•์‹์œผ๋กœ ์ ๋Š”๊ฒŒ ์ข‹๋‹ค.

 --select * from '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…'.'ํ…Œ์ด๋ธ”๋ช…' ;
 select * from employees.departments ;

DDL

DDL(Data Definition Language) - ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ์–ธ์–ด๋กœ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๋“ฑ์˜ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒด์˜ ๊ณจ๊ฒฉ์„ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์–ธ์–ด๋‹ค.

์ฃผ๋กœ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๊ด€๋ฆฌ์ž๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„์ž๊ฐ€ ์‚ฌ์šฉํ•˜๋ฉฐ, CHEMA, DOMAIN, TABLE, VIEW, INDEX๋ฅผ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ ๋˜๋Š” ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋‹ค.

DML

DML(Data Manipulation Language) - ์ •์˜๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž…๋ ฅ๋œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋Š” ๋“ฑ์˜ ์—ญํ• ์„ ํ•˜๋Š” ์–ธ์–ด๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž๊ฐ€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ์งˆ์˜์–ด๋ฅผ ํ†ตํ•˜์—ฌ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์งˆ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋‹ค.

Select

๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ

select *  from employees.employees; -- ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ 
select emp_no , birth_date, gender from employees.employees ; 
-- ํŠน์ •์ปฌ๋Ÿผ์„ ๋Œ€์ƒ์œผ๋กœ ์กฐํšŒ 

Dummy ํ…Œ์ด๋ธ” ํ™œ์šฉ

select count() from dual ; 
-- dual ์€ ๋”๋ฏธํ…Œ์ด๋ธ” 
select (1+1) / 2 as val from dual ; 
-- select์ ˆ์—์„œ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ฒ˜๋ฆฌ๋ฅผ ํ• ๋–„ ๋ฏธ๋ฆฌ ์‚ฌ์šฉ

๋ฐ์ดํ„ฐ ์ •๋ ฌ : ์˜ค๋ฆ„์ฐจ์ˆœ(ASC) ๋‚ด๋ฆผ์ฐจ์ˆœ (DESC)

select emp_no, birth_date from employees.employees order by birth_date asc ; 
-- ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ๋œ ๊ฒฐ๊ณผ๋ฅผ ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ(ASC) ํ˜น์€ ๋‚ด๋ฆผ์ฐจ์ˆœ(desc)์œผ๋กœ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •.

select emp_no, birth_date from employees.employees order by emp_no desc ; 
select * from employees.employees order by first_name desc , gender ; 
-- order by๋Š” deafault๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ

์ค‘๋ณต์„ ์ œ์™ธํ•˜๊ณ  ๋ณด๊ณ  ์‹ถ์„ ๋•Œ

select distinct first_name from employees.employees ; 
-- ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ  ์œ ์ผํ•œ ๊ฐ’๋“ค๋งŒ ๋ณด์—ฌ์ค„ ๋•Œ ์”€ 
select distinct first_name , gender from employees.employees ;

ํ•œ์ •๋œ ์ง‘ํ•ฉ์„ ์กฐํšŒ (where)

select * from employees.employees where first_name = 'Georgi' ; 
-- ์กฐ๊ฑด์— ๋งž๋Š” ํ•œ์ •๋œ ์ง‘ํ•ฉ๋งŒ์„ ์กฐํšŒํ•  ๋•Œ ์“ฐ๋Š” where
select * from employees.employees where hire_date > '1986-04-30' ; 
-- 1986๋…„ 4์›” 30์ผ ์ดํ›„์˜ ์ž…์‚ฌํ•œ ์‚ฌ๋žŒ๋“ค์„ ์กฐํšŒ
select*from employees.employees where first_name = 'Georgi' and hire_date > '1986-04-30'; 
-- ์ž…์‚ฌ์ผ์ด 1986๋…„ 4์›” 30์ผ ์ดํ›„์— ์ž…์‚ฌํ•œ ์ด๋ฆ„์ด Gerogi์ธ ์‚ฌ๋žŒ ์กฐํšŒ 
select  from employees.employees where hire_date between '1986-03-11' and '1986-04-30'; 
-- 1986๋…„ 3์›”11์ผ๊ณผ 4์›”30์ผ ์‚ฌ์ด์˜ ์ž…์‚ฌํ•œ ์‚ฌ๋žŒ๋“ค์„ ์กฐํšŒ

select * from employees.employees where first_name in ('Percy' , 'Weiye'); 
-- ์„ฑ์ด Percy ์ด๊ฑฐ๋‚˜ Weiye์ธ ์‚ฌ๋žŒ์„ ์กฐํšŒ

select * from employees.employees where first_name like 'W%'; 
-- ์ด๋ฏˆ์˜ ์ฒซ๊ธ€์ž๊ฐ€ W๋กœ ์‹œ์ž‘๋˜๋Š” ์‚ฌ๋žŒ๋“ค์„ ๋ชจ๋‘ ์กฐํšŒ

์‹ค์Šตํ•˜๊ธฐ ์•ž์„œ SQL EDITOR ํ™˜๊ฒฝ์„ค์ •์„ ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

  1. AUTO COMMIT ํ•ด์ œ

  2. ์•ˆ์ „๋ชจ๋“œ ํ•ด์ œ

EDIT ์„ ๋ˆ„๋ฅด๊ณ  PREFRENCE ๋ฒ„์„ ๋ˆ„๋ฅธ๋‹ค.

SQL Execution (SQL ์‹คํ–‰) ์—์„œ New connections use auto commit mode ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ํ•ด์ œํ•œ๋‹ค.

๋‹ค์Œ ์•ˆ์ „๋ชจ๋“œ๋ฅผ ํ•ด์ œํ•  ๊ฒƒ์ด๋‹ค.

SQL Editor์— ๋“ค์–ด๊ฐ€์„œ Safe Updates (rejects UPDATES and DELETs with restrictions) ๋ฒ„ํŠผ์„ ํ•ด์ œํ•œ๋‹ค.

INSERT , UPDATE , DELETE

  1. ํ…Œ์ด๋ธ” ์ƒ์„ฑ

seq : ์ž๋™ ์ธ๋ฑ์Šค ์ฆ๊ฐ€,

seq๋กœ ์ปฌ๋Ÿผ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ INT NOT NULL ๋กœ ์ค‘๋ณต์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์ˆซ์žํ˜•ํ•˜๊ณ  AUTO_INCREMENT ๋กœ ์ž๋™ ์ฆ๊ฐ€๋ฅผ ์„ค์ •

seq ๋ฅผ ๊ธฐ๋ณธํ‚ค๋กœ ์ง€์ •ํ•˜์—ฌ ์ค‘๋ณต์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ์ง€์ •

CREATE TABLE TEST_DB.customer ( 
seq INT NOT NULL AUTO_INCREMENT, 
ct_id VARCHAR(20) NOT NULL, 
ct_pw VARCHAR(100) NOT NULL, 
address VARCHAR(100) NOT NULL, 
ct_tell VARCHAR(20) NOT NULL, 
REG_DATE DATE NOT NULL, 
EXPIRED_DATE DATE NOT NULL, 
PRIMARY KEY(seq) ) ENGINE= MYISAM CHARSET=utf8 ;
-- show character set ; ์บ๋ฆญํ„ฐ ์…‹ ์ข…๋ฅ˜ ํ™•์ธ 

2. ๋ฐ์ดํ„ฐ ์‚ฝ์ž…

INSERT๋ฌธ

INSERT INTO test_db.customer (ct_id, ct_pw, address, ct_tell, REG_DATE, EXPIRED_DATE) 
values ('yunpha123','1234','์ธ์ฒœ์‹œ ๋ถ€ํ‰๊ตฌ ๊ฐˆ์‚ฐ๋™ ๋ฌด์ง€๊ฐœ์•„ํŒŒํŠธ 101ํ˜ธ','010-1234-5678','20180920','2020-02-18') ;

3. ๋ฐ์ดํ„ฐ ํ™•์ธ

select * from test_db.customer ;

UPDATE๋ฌธ

select * from employees.departments; 
insert into employees.departments (dept_no, dept_name) values('d010', 'android developteam') ; 
insert into employees.departments (dept_no, dept_name) values('d011', 'IOS developteam') ; 
insert into employees.departments (dept_no, dept_name) values('d012', 'Infra team') ; 
insert into employees.departments (dept_no, dept_name) values('d013', 'operation team') ;
-- ๊ทผ๋ฐ ๋‚ด๊ฐ€ ๋ถ€์„œ์ด๋ฆ„์„ ์ž˜๋ชป ๋„ฃ์—ˆ์–ด. 
update employees.departments set dept_name = 'andorid team' where dept_no = 'd010'; 
select * from employees.departments where dept_no= 'd010';

select from employees.departments ; 

update employees.departments set dept_name = 'IOS team' 
where dept_name = 'IOS developteam'; 

select  from employees.departments where dept_no = 'd011';
-- ์œ„ํ—˜ update์— ์กฐ๊ฑด์ ˆ์ด ์—†์„๊ฒฝ์šฐ db์—์„œ๋Š” ๋งค์šฐ ์œ„ํ—˜ํ•œ ์ž‘์—…์œผ๋กœ ์ธ์‹ํ•จ ์™œ๋ƒํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์ „์ฒด๋ฅผ ๋ฐ”๊พธ๊ฒŒ ๋˜๋ฏ€๋กœ 
--update employees.departments set dept_no = 'd100' ;

DELETE ๋ฌธ

select  from employees.employees ; 
select  from employees.dept_emp ;
select * from employees.employees a , employees.dept_emp b where a.emp_no = b.emp_no ; 
create table employees.emp_dept as (select a.emp_no , b.dept_no from employees.employees a , employees.dept_emp b where a.emp_no = b.emp_no) ;
select * from employees.emp_dept ;
delete from employees.emp_dept where emp_no = 10001 and dept_no='d005'; 
delete from employees.emp_dept where emp_no = 10002 ;
delete from employees.emp_dept ; -- ๋ฐ์ดํ„ฐ๋งŒ ์‚ญ์ œ๋จ

View

๋ทฐ(view)๋Š” ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์–ธ์–ด SQL์—์„œ ํ•˜๋‚˜ ์ด์ƒ์˜ ํ…Œ์ด๋ธ” (๋˜๋Š” ๋‹ค๋ฅธ ๋ทฐ)์—์„œ ์›ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜์—ฌ, ๊ทธ๋“ค์„ ์‚ฌ์šฉ์ž ์ •์˜ํ•˜์—ฌ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค. ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ด€๊ณ„ ๋ชจ๋ธ์˜ ๊ด€๊ณ„์˜ ์ผ์ข…์ธ ๋„์ถœ ๊ด€๊ณ„์— ํ•ด๋‹นํ•œ๋‹ค. ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”(๊ธฐ๋ณธ ๊ด€๊ณ„) ๋˜๋Š” ๋ทฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์กฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณด๊ธฐ์— ํ‘œ์‹œ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์„ ํƒ ๊ธฐ์ค€์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค[1].

์‰ฝ๊ฒŒ ๋งํ•ด์„œ ๋ทฐ(view)๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์กด์žฌํ•˜๋Š” ์ผ์ข…์˜ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์„ ์˜๋ฏธํ•œ๋‹ค.

์ด๋Ÿฌํ•œ ๋ทฐ๋Š” ์‹ค์ œ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ํ–‰๊ณผ ์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ์‹ค์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์ง€๋Š” ์•Š๊ณ , ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์ด๋‚˜ ๋‹ค๋ฅธ ๋ทฐ์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์—ญํ• ๋งŒ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์ด๋‚˜ ๋ทฐ๋ฅผ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค[2].

๋ทฐ์˜ ํŠน์ง•

์žฅ์ :

  1. ํŠน์ • ์‚ฌ์šฉ์ž์—๊ฒŒ ํ…Œ์ด๋ธ” ์ „์ฒด๊ฐ€ ์•„๋‹Œ ํ•„์š”ํ•œ ํ•„๋“œ๋งŒ์„ ๋ณด์—ฌ์ค€๋‹ค.

  2. ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ˆœํ™”ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

  3. ์ฟผ๋ฆฌ๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹จ์ :

  1. ํ•œ ๋ฒˆ ์ •์˜๋œ ๋ทฐ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

  2. ์‚ฝ์ž…, ์‚ญ์ œ, ๊ฐฑ์‹  ์ž‘์—…์— ๋งŽ์€ ์ œํ•œ ์‚ฌํ•ญ์„ ๊ฐ€์ง„๋‹ค.

  3. ์ž์‹ ๋งŒ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.

๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 CREATE VIEW ๋ทฐ์ด๋ฆ„ AS ( SELECT ํ•„๋“œ์ด๋ฆ„1, ํ•„๋“œ์ด๋ฆ„2, ... FROM ํ…Œ์ด๋ธ”์ด๋ฆ„ WHERE ์กฐ๊ฑด)

๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ๋”ฐ๋ผํ•ด๋ณด๊ธธ ๊ถŒํ•œ๋‹ค.

CREATE VIEW employees.emp_dept22 as (
select a.emp_no , b.dept_no from employees.employees a , employees.dept_emp b 
where a.emp_no = b.emp_no) ; 
select * from employees.emp_dept22 ;

์„œ๋ธŒ์ฟผ๋ฆฌ(Sub Query)

์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€ SQL ๋ฌธ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ SQL ๋ฌธ์„ ๋งํ•œ๋‹ค.

์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์—ฌ๋Ÿฌ๋ฒˆ์˜ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰์„ ํ†ตํ•ด ์–ป๋Š” ๊ฒฐ๊ณผ๋ฅผ ํ•˜๋‚˜์˜ ์ค‘์ฒฉ๋œ SQL๋ฌธ์žฅ์œผ๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

์„œ๋ธŒ์ฟผ๋ฆฌ๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ๋ช…์นญ์ด ๋‹ค๋ฅด๋‹ค.

  1. FROM์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์ธ๋ผ์ธ ๋ทฐ(Inline view),

  2. SELECT๋ฌธ์— ์‚ฌ์šฉํ•˜๋Š” ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ(Scala Subquery)

  3. WHERE์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ(Subquery)

3๊ฐ€์ง€ ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๊ฐ„๋‹จํ•œ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ฒ ๋‹ค.

FROM์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์ธ๋ผ์ธ ๋ทฐ(Inline view)

select emp_no , salary from employees.salaries ; 
select a.*, b.salary from employees.employees a left join 
(select emp_no , salary from employees.salaries) b on a.emp_no = b.emp_no ; 

--ํ…Œ์ด๋ธ” ์ƒ์„ฑ 
create table employees.emp_salary as (select a.* , b.salary from employees.employees a 
left join 
(select emp_no , salary from employees.salaries) b on a.emp_no = b.emp_no ) ; 
select * from employees.emp_salary ;

1986๋…„ 06 26์ผ์— ์ž…์‚ฌํ•œ ํšŒ์‚ฌ์› ์ค‘ ์—ฐ๋ด‰์ด ๊ฐ€์žฅ ๋†’์€ TOP3๋ฅผ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

select emp_no , first_name, last_name, salary 
from (select * from employees.emp_salary where hire_date ='1986-06-26' ) a 
order by salary limit 3 ;

SELECT๋ฌธ์— ์‚ฌ์šฉํ•˜๋Š” ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ(Scala Subquery)

์‚ฌ์› 10010 ๊ธ‰์—ฌ์™€ ํ…Œ์ด๋ธ” ์ „์ฒด ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ์กฐํšŒํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

select emp_no , first_name,last_name, salary , 
(select round(avg(salary),-1) from employees.emp_salary ) as 'ํ‰๊ท ๊ธ‰์—ฌ' 
from employees.emp_salary where emp_no =10010;

Where ์ ˆ์— ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ

'Peac' ์„ฑ์„ ๊ฐ€์ง„ ์‚ฌ์›๋“ค๋ณด ๋” ๋นจ๋ฆฌ ์ž…์‚ฌํ•œ ์‚ฌ๋žŒ๋“ค์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

select emp_no, first_name , last_name ,hire_date from employees.employees 
where hire_date < (select min(hire_date) from employees.employees where last_name = 'Peac') 
order by hire_date ;

DCL

DCL(Data Control Language) - ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด์— ๊ถŒํ•œ์„ ์ฃผ๋Š”๋“ฑ์˜ ์—ญํ• ์„ ํ•˜๋Š” ์–ธ์–ด๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ์–ดํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ์˜ ๋ณด์•ˆ, ๋ฌด๊ฒฐ์„ฑ, ํšŒ๋ณต, ๋ณ‘ํ–‰ ์ˆ˜ํ–‰์ œ์–ด ๋“ฑ์„ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

select ๋ฌธ ์‹คํ–‰์ˆœ์„œ

SELECT ์ฟผ๋ฆฌ๋ฌธ์„ ์‹คํ–‰ํ–ˆ์„๋•Œ FROM - WHERE GROUP BY - HAVING - SELECT - ORDER BY -LIMIT ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰์ด ๋œ๋‹ค.

  1. FROM

  2. WHERE

  3. GROUP BY

  4. HAVING

  5. SELECT

  6. ORDER BY

  7. LIMIT

๊ตฌ์ฒด์ ์œผ๋กœ SQL์˜ ์‹คํ–‰์ˆœ์„œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. FROM : DBMS์˜ ๊ฐ ํ…Œ์ด๋ธ” ์ค‘์—์„œ ์–ด๋–ค ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ• ์ง€ ๋จผ์ € ๊ฒฐ์ •ํ•œ๋‹ค.

  2. WHERE : FROM๋ถ€๋ถ„์—์„œ ํ•ด๋‹น๋˜๋Š” ํ…Œ์ด๋ธ”์—์„œ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” row๋“ค์„ ์„ ๋ณ„ํ•œ๋‹ค.

  3. GROUP BY : WHERE ์กฐ๊ฑด์œผ๋กœ ์ถ”์ถœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•œ ์นผ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ row๋“ค์„ ๊ทธ๋ฃจํ•‘ํ•œ๋‹ค. ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์€ ํ•˜๋‚˜์˜ row๋กœ ํ‘œํ˜„๋œ๋‹ค.

  4. HAVING : ๊ทธ๋ฃจํ•‘ ์ž‘์—… ํ›„ ์ƒ์„ฑ๋œ ์—ฌ๋Ÿฌ ๊ทธ๋ฃน ์ค‘ ํŠน์ • ์กฐ๊ฑด(๋“ค)์„ ๋งŒ์กฑํ•˜๋Š” ๊ทธ๋ฃน๋“ค๋งŒ ์„ ๋ณ„ํ•œ๋‹ค.

  5. SELECT : ์—ฌ๋Ÿฌ ์กฐ๊ฑด๋“ค์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ๋‚จ์€ ๋ฐ์ดํ„ฐ์—์„œ ์–ด๋–ค ์ปฌ๋Ÿผ์„ ์ถœ๋ ฅํ• ์ง€ ์„ ํƒํ•œ๋‹ค.

  6. ORDER BY : ํ–‰์˜ ์ˆœ์„œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ค„์ง€ ์ •๋ ฌํ•œ๋‹ค.

  7. LIMIT : ์ด์ „ ๋‹จ๊ณ„๊นŒ์ง€ ์กฐํšŒ๋œ row๋“ค ์ค‘ ์ผ๋ถ€ row๋“ค๋งŒ ๋ณด์—ฌ์ค€๋‹ค.

Foreign Key

Foreign Key๋Š” ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ํŠน์ • row๋ฅผ ์‹๋ณ„ํ•˜๋Š” ์ปฌ๋Ÿผ์œผ๋กœ ์šฐ๋ฆฌ๋ง๋กœ ์™ธ๋ž˜ ํ‚ค๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์™ธ๋ž˜๋Š” ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์—์„œ 1๊ฐœ์˜ ํ‚ค(์†์„ฑ ๋˜๋Š” ์†์„ฑ์˜ ์ง‘ํ•ฉ)์— ํ•ด๋‹นํ•˜๊ณ , ์ฐธ์กฐํ•˜๋Š” ์ธก์˜ ๊ด€๊ณ„ ๋ณ€์ˆ˜๋Š” ์ฐธ์กฐ๋˜๋Š” ์ธก์˜ ํ…Œ์ด๋ธ”์˜ ํ‚ค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ์†์„ฑ์˜ ํ–‰ 1๊ฐœ์˜ ๊ฐ’์€, ์ฐธ์กฐ๋˜๋Š” ์ธก ํ…Œ์ด๋ธ”์˜ ํ–‰ ๊ฐ’์— ๋Œ€์‘๋œ๋‹ค. ์ด ๋•Œ๋ฌธ์— ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ํ–‰์—๋Š”, ์ฐธ์กฐ๋˜๋Š” ํ…Œ์ด๋ธ”์— ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š” ๊ฐ’์„ ํฌํ•จํ•  ์ˆ˜ ์—†๋‹ค[3]. ์ฆ‰ ์™œ๋ฆฌํ‚ค๋Š” ๋‘๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ฐ๊ฒฐ ๋‹ค๋ฆฌ ์—ญํ• ์„ ํ•˜๋ฉฐ ์ค‘๋ณต ๋œ ๊ฐ’์„ ๋ฐฉ์ง€ํ•ด์ค€๋‹ค[4].

๊ทธ๋ ‡๋‹ค๋ฉด ์™ธ๋ž˜ํ‚ค๋Š” ์™œ ํ•„์š”ํ• ๊นŒ?

์˜ˆ๋ฅผ๋“ค์–ด , ์—ฐ๋ด‰ ํ…Œ์ด๋ธ”์—์„œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ ์ž ํ• ๋•Œ ์™ธ๋ž˜ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์› ์•„์ด๋””๊ฐ€ ์—†๋Š” ์ •๋ณด๋ฉด DBMS๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์‚ฝ์ž…์„ ๋ง‰๋Š”๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ํ…Œ์ด๋ธ” ๋‚ด์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ํ•ญ์ƒ ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค[4].

์™ธ๋ž˜ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

CREATE TABLE table_name (
   id    INTEGER  PRIMARY KEY,
   col2  CHARACTER VARYING(20),
   col3  INTEGER FOREIGN KEY REFERENCES other_table(column_name),
   ... )

๋‹ค์Œ์€ EMPLOYEES DB์—์„œ salary ํ…Œ์ด๋ธ”์„ employees ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ์‹ค์Šต์„ ๊ฐ–๊ฒ ๋‹ค.

์„ค์ •์„ ๋งˆ์นœ ํ›„ apply๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์™ธ๋ž˜ํ‚ค ๋ฌธ๋ฒ•์ด ๋‚˜์˜จ๋‹ค.

ํ•ด์„ํ•˜๋ฉด ์—ฐ๋ด‰ํ…Œ์ด๋ธ”(salary)์˜ empno๋Š” ์‚ฌ์› ํ…Œ์ด๋ธ”(employees)์˜ emp_no ์ปฌ๋Ÿผ์„ ์ฐธ์กฐํ•˜๋„๋ก ํ•œ๋‹ค.๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

Reference:

[1] https://ko.wikipedia.org/wiki/VIEW_(SQL)

[2] http://tcpschool.com/mysql/mysql_view_createReplace

[3] https://ko.wikipedia.org/wiki/%EC%99%B8%EB%9E%98_%ED%82%A4

[4] https://brunch.co.kr/@dan-kim/26

Last updated