ex04_operator
์ฐ์ฐ์, Operator
1. ์ฐ์ ์ฐ์ฐ์
- +, -, *, /
- %(์์) > ํจ์๋ก ์ ๊ณต(mod())
2. ๋ฌธ์์ด ์ฐ์ฐ์
- ||(java์์ + ์ญํ )
3. ๋น๊ต ์ฐ์ฐ์
- >, >=, <, <=
- =(==), <>(!=)
- ๋
ผ๋ฆฌ๊ฐ ๋ฐํ X > ๋ช
์์ ์ผ๋ก ํํ ๋ถ๊ฐ๋ฅํ ์๋ฃํ > ์กฐ๊ฑด์ด ํ์ํ ์ํฉ์์๋ง ์ฌ์ฉ
- ์ปฌ๋ผ ๋ฆฌ์คํธ์์ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
- ์กฐ๊ฑด์ ์์ ์ฌ์ฉ ๊ฐ๋ฅ
4. ๋
ผ๋ฆฌ ์ฐ์ฐ์
- and(&&), or(||), not(!)
- ์ปฌ๋ผ ๋ฆฌ์คํธ์์ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
- ์กฐ๊ฑด์ ์์ ์ฌ์ฉ ๊ฐ๋ฅ
5. ๋์
์ฐ์ฐ์
- =
- ์ปฌ๋ผ = ๊ฐ
- update๋ฌธ
8. SQL ์ฐ์ฐ์
- ์๋ฐ: instanceof, typeof ๋ฑ..
- in, between, like, is ๋ฑ..(OO๊ตฌ, OO์ )
(3ํญ์ฐ์ฐ์, ์ฆ๊ฐ์ฐ์ฐ์ ์์)
desc tblCountry;
select * from tblCountry;
select population, area, population + area
from tblCountry;
-- ORA-01722: invalid number
select name + capital
from tblCountry;
select name || capital
from tblCountry;
-- ์ปฌ๋ผ๋ช
> ๊ฐ๊ณต๋ ์ปฌ๋ผ๋ช
> ์ฌ๋ฐ๋ฅธ ์ด๋ฆ์ผ๋ก ์์ > ์ปฌ๋ผ๋ช
๋ฐ๊พธ๊ธฐ > ๋ณ์นญ(Alias)
-- ์๋ณ์ > "name || '๋'"
select name, name || '๋'
from tblInsa;
----์๋ณ์์ ๊ณต๋ฐฑ ์๋๊ฒ ์ข์.. ๊ณต๋ฐฑ ๋ถ๊ฐํผํ๊ฒ ํฌํจํ ๋ or ์์ฝ์ด ์๋ฐ์ดํ(๋ก ๊ฐ๋ฅํ์ง๋ง ์ฌ์ฉ ๊ธ์ง)
ex05_where
select ์ปฌ๋ผ๋ฆฌ์คํธ --3. ์ปฌ๋ผ ์ง์
from ํ
์ด๋ธ --1. ํ
์ด๋ธ ์ง์
where ์กฐ๊ฑด; --2. ์กฐ๊ฑด ์ง์
where์
- ๋ ์ฝ๋๋ฅผ ๊ฒ์ํ๋ค.
- ์ํ๋ ํ๋ง ์ถ์ถํ๋ ์ญํ > ๊ฒฐ๊ณผ์
๋ฐํ
select name --3.
from tblCountry --1.
where continent = 'AS'; --2.
select *
from tblInsa
where basicpay > 2000000;
select *
from tblInsa
where buseo <> '๊ฐ๋ฐ๋ถ';
select *
from tblInsa
where buseo = '๊ฐ๋ฐ๋ถ' and jikwi = '๋ถ์ฅ';
select *
from tblInsa
where not buseo = '๊ฐ๋ฐ๋ถ';
-- SQL ๊ตฌ๋ฌธ > ๋์๋ฌธ์ ๊ตฌ๋ถ ์ํจ
-- ๋ฐ์ดํฐ > ๋์๋ฌธ์ ๊ตฌ๋ถํจ(***)
between
- where์ ์์ ์ฌ์ฉ > ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉ
- ์ปฌ๋ผ๋ช
between ์ต์๊ฐ and ์ต๋๊ฐ (๋ ๋ค ํฌํจ)
- ๋ฒ์ ์กฐ๊ฑด
- ๊ฐ๋
์ฑ(***)
select * from tblComedian
where height >= 172 and height <= 178;
select * from tblComedian
where height between 172 and 178;
-- ๋น๊ต์ฐ์ฐ
-- 1. ์ซ์ํ
select * from tblInsa where basicpay >= 1500000 and basicpay <= 2000000;
select * from tblInsa where basicpay between 1500000 and 2000000;
-- 2. ๋ฌธ์ํ
select * from tblInsa where name >= '๋ฐ';
select * from tblInsa where name >= '๋ฐ' and name <= '์ ';
select * from tblInsa where name between '๋ฐ' and '์ ';
-- 3. ๋ ์ง์๊ฐํ
select * from tblInsa where ibsadate >= '2000-01-01'; --2000๋
์ดํ์ ์
์ฌํ ์ง์๋ค
select * from tblInsa where ibsadate >= '2000-01-01' and ibsadate <= '2000-12-31';
select * from tblInsa where ibsadate between '2000-01-01' and '2000-12-31';
in
- where์ ์์ ์ฌ์ฉ > ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉ
- ์ด๊ฑฐํ ์กฐ๊ฑด
- ์ปฌ๋ผ๋ช
in (๊ฐ, ๊ฐ, ๊ฐ..)
- ๊ฐ๋
์ฑ
select * from tblInsa where buseo = 'ํ๋ณด๋ถ' or buseo = '๊ฐ๋ฐ๋ถ' or buseo = '์ด๋ฌด๋ถ';
select * from tblInsa where buseo in ('ํ๋ณด๋ถ', '๊ฐ๋ฐ๋ถ', '์ด๋ฌด๋ถ');
select * from tblInsa
where jikwi in ('๊ณผ์ฅ', '๋ถ์ฅ') and city in ('์์ธ', '์ธ์ฒ')
and basicpay between 2500000 and 3000000;
like
- where์ ์์ ์ฌ์ฉ > ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉ
- ํจํด ๋น๊ต
- ์ปฌ๋ผ๋ช
like 'ํจํด ๋ฌธ์์ด'
- ์ ๊ท ํํ์ ์ด๊ฐ๋จ ๋ฒ์
ํจํด ๋ฌธ์์ด ๊ตฌ์ฑ์์
1. _: ์์์ ๋ฌธ์ 1๊ฐ (.)
2. %: ์์์ ๋ฌธ์ N๊ฐ 0~๋ฌดํ๋ (.*)
-- ๊นOO
select name from tblInsa where name like '๊น__';
select name from tblInsa where name like '__์';
select name from tblInsa where name like '_๊ธธ_';
select name from tblInsa where name like '๊น%';
select * from tblAddressBook where name like '์ด%';
select * from tblAddressBook where name like '%์ด';
select * from tblAddressBook where name like '%์ด%';
-- ์ฌ์ง์๋ง(์ฃผ๋ฏผ๋ฑ๋ก๋ฒํธ ์ฌ์ฉ)
select * from tblInsa where ssn like '______-2______';
select * from tblInsa where ssn like '%-2%';
null ์กฐ๊ฑด
- where์ ์์ ์ฌ์ฉ
- ์ปฌ๋ผ๋ช
is null
- null์ ์ฐ์ฐ์ ๋์์ด ๋ ์ ์๋ค.
-- ์ธ๊ตฌ์๊ฐ ๋ฏธ๊ธฐ์ฌ๋ ๋๋ผ?
select * from tblCountry where population = null; -- ํ๋ฆฐ ๋ฌธ์ฅ
select * from tblCountry where population is null;
select * from tblCountry where not population is null; -- null์ด ์๋ ๊ฒฝ์ฐ
select * from tblCountry where population is not null; -- null์ด ์๋ ๊ฒฝ์ฐ(*์ถ์ฒ)
-- ์์ง ์คํํ์ง ์์ ์ผ?
select * from tblTodo where completedate is null;
--์๋ฃํ ์ผ?
select * from tblTodo where completedate is not null;
-- ๋์๊ด > ๋์ฌ ํ
์ด๋ธ(์์ฑ: ๋์ฌ๋ ์ง, ๋ฐ๋ฉ๋ ์ง)
-- ์์ง ๋ฐ๋ฉ์ ์ํ ์ฌ๋์?
select * from ๋์๋์ฌ where ๋ฐ๋ฉ๋ ์ง is null;
-- ๋ฐ๋ฉ์ด ์๋ฃ๋ ์ฌ๋์?
select * from ๋์๋์ฌ where ๋ฐ๋ฉ๋ ์ง is not null;
ex06_column
์ปฌ๋ผ ๋ฆฌ์คํธ์์ ํ ์ ์๋ ํ๋
- select ์ปฌ๋ผ๋ฆฌ์คํธ
--์ปฌ๋ผ ๋ช
์
select name, buseo
from tblInsa;
-- ์ฐ์ฐ
select name, basicpay, basicpay * 2 as basicpay2
from tblInsa;
-- ์์(๋ชจ๋ ๋ ์ฝ๋์ ๋์ผํ ๋ฐ์ดํฐ)
select name, 'ํ๊ธธ๋', 100
from tblInsa;
distinct
- ์ปฌ๋ผ ๋ฆฌ์คํธ์์ ์ฌ์ฉ
- ์ค๋ณต๊ฐ ์ ๊ฑฐ
- distinct ์ปฌ๋ผ๋ช
> distinct ์ปฌ๋ผ๋ฆฌ์คํธ
-- 14๊ฐ ๊ตญ๊ฐ๊ฐ ๊ฐ๊ฐ ์ํ ๋๋ฅ์ ๊ฐ์ ธ์ค์์ค.
select continent from tblCountry;
-- tblCountry์๋ ์ด๋ค ์ด๋ค ๋๋ฅ์ด ์๋์? > ์ข
๋ฅ?
select distinct continent from tblCountry;
-- tblInsa > ์ด ํ์ฌ์๋ ์ด๋ค ๋ถ์๋ค์ด?
select distinct buseo from tblInsa;
-- tblInsa > ์ด ํ์ฌ์๋ ์ด๋ค ์ง์๊ฐ?
select distinct jikwi from tblInsa;
-- ************ DB์ ํ
์ด๋ธ์๋ ์
๋ณํฉ์ด๋ผ๋๊ฒ ์๋ค.
-- distinct ๋ค์ ์ปฌ๋ผ์ด ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ ๋ชจ๋ ์ค๋ณต๋์ด์ผ ์ค๋ณต๊ฐ์ฒ๋ฆฌ
select distinct continent, name from tblCountry;
select distinct age, height from tblAddressBook where age = 36; --53 > 29
case
- ๋๋ถ๋ถ ์ ์์ ์ฌ์ฉ
- ์กฐ๊ฑด๋ฌธ ์ญํ > ์ปฌ๋ผ๊ฐ ์กฐ์
select
last || first as name,
gender,
case
when gender = 'm' then '๋จ์'
when gender = 'f' then '์ฌ์'
end as genderName
from tblComedian;
select
name,
continent,
case continent
when 'AS' then '์์์'
when 'EU' then '์ ๋ฝ'
when 'AF' then '์ํ๋ฆฌ์นด'
end as continentName
from tblCountry;
select
name, jikwi,
case
when jikwi = '๋ถ์ฅ' or jikwi = '๊ณผ์ฅ' then '๊ฐ๋ถ๊ธ'
else 'ํ์ฌ์๊ธ'
end state,
case
when jikwi in ('๋ถ์ฅ', '๊ณผ์ฅ') then '๊ฐ๋ถ๊ธ'
else 'ํ์ฌ์๊ธ'
end state2
from tblInsa;
select
title,
case
when completedate is null then '๋ฏธ์๋ฃ'
when completedate is not null then '์๋ฃ'
end as state
from tblTodo;
ex07_order
select ์ปฌ๋ผ๋ฆฌ์คํธ --3. ์ํ๋ ์ปฌ๋ผ๋ค์
from ํ
์ด๋ธ --1. ํ
์ด๋ธ๋ก๋ถํฐ
where ์กฐ๊ฑด --2. ์ํ๋ ํ๋ค์
order by ์ ๋ ฌ๊ธฐ์ค; --4. ์์๋๋ก
order by ์
- ๊ฒฐ๊ณผ์
์ ์ ๋ ฌ(O)
- ์๋ณธ ํ
์ด๋ธ์ ์ ๋ ฌ(์ฌ์ฉ์๊ฐ ๊ด์ฌ ๋ถ๊ฐ๋ฅ > ์ค๋ผํด ์ค์ค๋ก)
- order by ์ ๋ ฌ์ปฌ๋ผ [asc|desc] ์๋ต์ asc(์ค๋ฆ์ฐจ์)
select * from tblCountry order by name asc;
select * from tblCountry order by population desc; -- null ์ปฌ๋ผ์ ๋์์ผ๋ก ์ ๋ ฌ
select * from tblCountry where population is not null order by population desc;
select * from tblInsa order by name asc; --๋ฌธ์์ด + ์ค๋ฆ์ฐจ์
select * from tblInsa order by basicpay; --์ซ์ + ์ค๋ฆ์ฐจ์
select * from tblInsa order by ibsadate; --๋ ์ง + ์ค๋ฆ์ฐจ์
-- ๋ค์ค ์ ๋ ฌ
select * from tblInsa order by buseo asc, city asc, name asc;
select
name, buseo, jikwi
from tblInsa
order by buseo, jikwi, name;
select
name, buseo, jikwi
from tblInsa
order by 2, 3, 1; --๋น๊ถ์ฅ > ๊ฐ๋
์ฑ ๋ฎ์, ์ ์ง๋ณด์์ ์ทจ์ฝ
-์ง์์์ผ๋ก ์ ๋ ฌ: ๋ถ์ฅ > ๊ณผ์ฅ > ๋๋ฆฌ > ์ฌ์
- ์ฑ๋ณ์์ผ๋ก ์ ๋ ฌ
select
name, jikwi,
case
when jikwi = '๋ถ์ฅ' then 1
when jikwi = '๊ณผ์ฅ' then 2
when jikwi = '๋๋ฆฌ' then 3
when jikwi = '์ฌ์' then 4
end as jikwiSeq
from tblInsa
order by jikwiSeq asc;
select
name, jikwi,
case
when jikwi = '๋ถ์ฅ' then 1
when jikwi = '๊ณผ์ฅ' then 2
when jikwi = '๋๋ฆฌ' then 3
when jikwi = '์ฌ์' then 4
end
from tblInsa
order by 3 asc; --์ธ๋ฑ์ค์ฒ๋ฆฌ๋ ๊ฐ๋ฅ
-- case ํ ์๋ณด์ด๊ฒ > order by ๋ค์ case
select
name, jikwi
from tblInsa
order by case
when jikwi = '๋ถ์ฅ' then 1
when jikwi = '๊ณผ์ฅ' then 2
when jikwi = '๋๋ฆฌ' then 3
when jikwi = '์ฌ์' then 4
end asc;
-- where์ ์ case ๋ฃ์์ ์์
select
name, jikwi
from tblInsa
where case
when jikwi = '๋ถ์ฅ' then 1
when jikwi = '๊ณผ์ฅ' then 2
when jikwi = '๋๋ฆฌ' then 3
when jikwi = '์ฌ์' then 4
end = 1
order by case
when jikwi = '๋ถ์ฅ' then 1
when jikwi = '๊ณผ์ฅ' then 2
when jikwi = '๋๋ฆฌ' then 3
when jikwi = '์ฌ์' then 4
end asc;
-- ์ฑ๋ณ์์ผ๋ก ์ ๋ ฌ: ๋จ์ > ์ฌ์
-- 771212-1022432
select * from tblInsa;
select * from tblInsa
order by case
when ssn like '%-1%' then '๋จ์'
when ssn like '%-2%' then '์ฌ์'
end asc;
'ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ > Oracle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ex13] DDL(Data Definition Language) (0) | 2023.03.19 |
---|---|
[ex11~12] ๋ ์ง ์๊ฐ ํจ์(date time function), ํ๋ณํ ํจ์(casting function) (0) | 2023.03.19 |
[ex10] string function (0) | 2023.03.19 |
[ex08~09] ์ง๊ณํจ์(aggregation function), ์ํ ํจ์(numerical_function) (0) | 2023.03.19 |
[ex01~03] sql, datatype, select (0) | 2023.03.19 |