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๋ฅผ ์ ์ํ๊ฑฐ๋ ๋ณ๊ฒฝ ๋๋ ์ญ์ ํ ๋ ์ฌ์ฉํ๋ ์ธ์ด๋ค.
์ข ๋ฅ
์ญํ
CREATE
๋ฐ์ดํฐ๋ฒ ์ด์ค, ํ ์ด๋ธ ๋ฑ์ ์์ฑํ๋ ์ญํ .
ALTER
ํ ์ด๋ธ์ ์์ ํ๋ ์ญํ
DROP
๋ฐ์ดํฐ๋ฒ ์ด์ค, ํ ์ด๋ธ์ ์ญ์ ํ๋ ์ญํ
TRUNCATE
ํ ์ด๋ธ์ ์ด๊ธฐํ ์ํค๋ ์ญํ
DML
DML(Data Manipulation Language) - ์ ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๋ ฅ๋ ๋ ์ฝ๋๋ฅผ ์กฐํํ๊ฑฐ๋ ์์ ํ๊ฑฐ๋ ์ญ์ ํ๋ ๋ฑ์ ์ญํ ์ ํ๋ ์ธ์ด๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ์๊ฐ ์์ฉ ํ๋ก๊ทธ๋จ์ด๋ ์ง์์ด๋ฅผ ํตํ์ฌ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ์ค์ง์ ์ผ๋ก ์ฒ๋ฆฌํ๋๋ฐ ์ฌ์ฉํ๋ ์ธ์ด๋ค.
์ข ๋ฅ
์ญํ
SELECT
ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ์ญํ
INSERT
ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ๋ ์ญํ
UPDATE
ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ์ญํ
DELETE
ํ ์ด๋ธ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ ์ญํ
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 ํ๊ฒฝ์ค์ ์ ์งํํ ๊ฒ์ด๋ค.
AUTO COMMIT ํด์
์์ ๋ชจ๋ ํด์
EDIT ์ ๋๋ฅด๊ณ PREFRENCE ๋ฒ์ ๋๋ฅธ๋ค.

SQL Execution (SQL ์คํ) ์์ New connections use auto commit mode ๋ฒํผ์ ๋๋ฌ ํด์ ํ๋ค.

๋ค์ ์์ ๋ชจ๋๋ฅผ ํด์ ํ ๊ฒ์ด๋ค.
SQL Editor์ ๋ค์ด๊ฐ์ Safe Updates (rejects UPDATES and DELETs with restrictions) ๋ฒํผ์ ํด์ ํ๋ค.

INSERT , UPDATE , DELETE
ํ ์ด๋ธ ์์ฑ
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].
๋ทฐ์ ํน์ง
์ฅ์ :
ํน์ ์ฌ์ฉ์์๊ฒ ํ ์ด๋ธ ์ ์ฒด๊ฐ ์๋ ํ์ํ ํ๋๋ง์ ๋ณด์ฌ์ค๋ค.
๋ณต์กํ ์ฟผ๋ฆฌ๋ฅผ ๋จ์ํํด์ ์ฌ์ฉํ๋ค.
์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋จ์ :
ํ ๋ฒ ์ ์๋ ๋ทฐ๋ ๋ณ๊ฒฝํ ์ ์๋ค.
์ฝ์ , ์ญ์ , ๊ฐฑ์ ์์ ์ ๋ง์ ์ ํ ์ฌํญ์ ๊ฐ์ง๋ค.
์์ ๋ง์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง ์ ์๋ค.
๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
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๋ฌธ์ฅ์ผ๋ก ๊ฐํธํ๊ฒ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๊ฒ ํด์ค๋ค.
์๋ธ์ฟผ๋ฆฌ๋ ์์น์ ๋ฐ๋ผ ๋ช ์นญ์ด ๋ค๋ฅด๋ค.
FROM์ ์ ์ฌ์ฉํ๋ ์ธ๋ผ์ธ ๋ทฐ(Inline view),
SELECT๋ฌธ์ ์ฌ์ฉํ๋ ์ค์นผ๋ผ ์๋ธ์ฟผ๋ฆฌ(Scala Subquery)
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) - ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ๊ฑฐ๋ ๊ฐ์ฒด์ ๊ถํ์ ์ฃผ๋๋ฑ์ ์ญํ ์ ํ๋ ์ธ์ด๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ดํ๋ฉฐ ๋ฐ์ดํฐ์ ๋ณด์, ๋ฌด๊ฒฐ์ฑ, ํ๋ณต, ๋ณํ ์ํ์ ์ด ๋ฑ์ ์ ์ํ๋๋ฐ ์ฌ์ฉํ๋ค.
์ข ๋ฅ
์ญํ
GRANT
ํน์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ์์๊ฒ ํน์ ์์ ์ ๋ํ ์ํ๊ถํ ๋ถ
REVOKE
ํน์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ์์๊ฒ ํน์ ์์ ์ ๋ํ ์ํ ๊ถํ์ ๋ฐํ, ํ์
COMMIT
ํธ๋์ญ์ ์ ์์ ์ ์ทจ์ ๋ฐ ์๋๋ ๋ณต๊ตฌํ๋ ์ญํ
ROLLBACK
ํธ๋์ญ์ ์ ์์ ์ ์ทจ์ ๋ฐ ์๋๋๋ก ๋ณต๊ตฌํ๋ ์ญ
select ๋ฌธ ์คํ์์
SELECT ์ฟผ๋ฆฌ๋ฌธ์ ์คํํ์๋ FROM - WHERE GROUP BY - HAVING - SELECT - ORDER BY -LIMIT ์์๋๋ก ์คํ์ด ๋๋ค.
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
LIMIT

๊ตฌ์ฒด์ ์ผ๋ก SQL์ ์คํ์์๋ฅผ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
FROM : DBMS์ ๊ฐ ํ ์ด๋ธ ์ค์์ ์ด๋ค ํ ์ด๋ธ์ ์กฐํํ ์ง ๋จผ์ ๊ฒฐ์ ํ๋ค.
WHERE : FROM๋ถ๋ถ์์ ํด๋น๋๋ ํ ์ด๋ธ์์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ row๋ค์ ์ ๋ณํ๋ค.
GROUP BY : WHERE ์กฐ๊ฑด์ผ๋ก ์ถ์ถ๋ ๋ฐ์ดํฐ๋ฅผ ์ ํํ ์นผ๋ผ์ ๊ธฐ์ค์ผ๋ก row๋ค์ ๊ทธ๋ฃจํํ๋ค. ํ๋์ ๊ทธ๋ฃน์ ํ๋์ row๋ก ํํ๋๋ค.
HAVING : ๊ทธ๋ฃจํ ์์ ํ ์์ฑ๋ ์ฌ๋ฌ ๊ทธ๋ฃน ์ค ํน์ ์กฐ๊ฑด(๋ค)์ ๋ง์กฑํ๋ ๊ทธ๋ฃน๋ค๋ง ์ ๋ณํ๋ค.
SELECT : ์ฌ๋ฌ ์กฐ๊ฑด๋ค์ ์ฒ๋ฆฌํ ํ ๋จ์ ๋ฐ์ดํฐ์์ ์ด๋ค ์ปฌ๋ผ์ ์ถ๋ ฅํ ์ง ์ ํํ๋ค.
ORDER BY : ํ์ ์์๋ฅผ ์ด๋ป๊ฒ ๋ณด์ฌ์ค์ง ์ ๋ ฌํ๋ค.
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
Last updated
Was this helpful?