์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์ปฌ๋ ์ ํ๋ ์์ํฌ
- fibonacci
- testing
- ์๋ฃ๊ตฌ์กฐ
- ์ฒซ๊ธ์๋๋ฌธ์
- ๋ฌธ์์ด๋ค์ง๊ธฐ
- ํ๊ณ
- ์๊ณ ๋ฆฌ์ฆ
- ํ์ดํ๋ก๊ทธ๋๋ฐ
- HTML
- ๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ
- ๋ถํธ์บ ํ
- ๊ณ์ฐ๊ธฐ๋ง๋ค๊ธฐ
- spring data jpa
- CLI๋ช ๋ น์ด
- ๊ทธ๋ฆฌ๋
- ๋ฐฑ์๋
- ์คํ๋ง
- ์ธํ ๋ฆฌ์ ์ด
- Spring Data JDBC
- CSS
- java
- ๋ฐ์ผ๋ฆฌ์ฝ๋ฉ
- ์๋ฐ
- ๊นํ๋ธ
- Spring Security
- ๊ฑฐ๋ญ์ ๊ณฑ
- Publishing
- FilterChain
- ์ ๋ค๋ฆญ์ค
Archives
- Today
- Total
๋์ ๋ชจ์
[DB] SQL - DB ๋ช ๋ น์ด ์์, ๊ธฐ๋ณธ ์ฟผ๋ฆฌ๋ฌธ ์์, ๋ด์ฅํจ์ ๋ณธ๋ฌธ
WILT/DB
[DB] SQL - DB ๋ช ๋ น์ด ์์, ๊ธฐ๋ณธ ์ฟผ๋ฆฌ๋ฌธ ์์, ๋ด์ฅํจ์
kexon 2022. 8. 7. 16:10๐ DB ๋ช ๋ น์ด ์์
๐ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
create database test;
๐ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ
use test;
๐ฌ ํ ์ด๋ธ ์์ฑ
- use๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ํ → ํ ์ด๋ธ ์์ฑ ๊ฐ๋ฅ
- ํ๋(ํ์ ์ด)์ ํจ๊ป ๋ง๋ค์ด์ผ๋จ
create table user (
id int primary key auto_increment,
name varchar(255),
email varchar(255)
);
๐ฌ ํ ์ด๋ธ ์ ๋ณด ํ์ธ
describe user;
๐ SQL ๊ธฐ๋ณธ ์ฟผ๋ฆฌ๋ฌธ ์์
select
- ์ปฌ๋ผ ๋จ์ ์ถ๋ ฅ
-- ํ
์ด๋ธ๋ช
์์ ์ ํ๋ ๋ชจ๋ ์ปฌ๋ผ ๊ฐ์ ธ์ค๊ธฐ
select ์ปฌ๋ผ๋ช
*(๋ชจ๋ ์ปฌ๋ผ)
from ํ
์ด๋ธ๋ช
-- ์ ์ฒด ๋ฐ์ดํฐ ๋ณด๊ธฐ
select * from ํ
์ด๋ธ๋ช
;
-- ํน์ ์ปฌ๋ผ ๋ณด๊ธฐ
select ํน์ ์ปฌ๋ผ from ํ
์ด๋ธ๋ช
;
from
- from + ๊ฒฐ๊ณผ๊ฐ ๋ฐ์ ๋ฐ์ดํฐํ ์ด๋ธ์ ๋ช ์ํ๋ ๊ฒ์ผ๋ก, ํ ์ด๋ธ ๊ด๋ จ ์์ ์ ๋ฐ๋์ ์ ๋ ฅ
-- ์ฝ๋ ํน์ฑ์ ํ
์ด๋ธ์์ ์ฌ์ฉ
select ํน์ฑ1
from ํ
์ด๋ธ์ด๋ฆ;
-- ๋ช ๊ฐ์ง์ ํน์ฑ์ ํ
์ด๋ธ์์ ์ฌ์ฉ
select ํน์ฑ1, ํน์ฑ2
from ํ
์ด๋ธ์ด๋ฆ;
-- ํ
์ด๋ธ์ ๋ชจ๋ ํน์ฑ ์ ํ
select * -- * ๋ ์์ผ๋์นด๋๋ก, ์ ๋ถ ์ ํํ ๋ ์ฌ์ฉ
from ํ
์ด๋ธ์ด๋ฆ;
where
- ํํฐ ์ญํ ์ ํ๋ ์ฟผ๋ฆฌ๋ฌธ์ผ๋ก, ์ ํ์ ์ผ๋ก ์ฌ์ฉ
- ํน์ ๊ฐ๊ณผ ๋์ผํ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
select ํน์ฑ1, ํน์ฑ2 from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ1 = "ํน์ ๊ฐ";
- ํน์ ๊ฐ์ ์ ์ธํ ๋ฐ์ดํฐ ์ฐพ๊ธฐ
select ํน์ฑ1, ํน์ฑ2 from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ2 <> "ํน์ ๊ฐ";
- ํน์ ๊ฐ๊ณผ ๋น๊ต์ฐ์ฐ์
- ํฌ๊ฑฐ๋ ์์ ๋ฐ์ดํฐ ํํฐ: <, >
- ๋น๊ตํ๋ ๊ฐ์ ํฌํจํ๋ ํํฐ: <=, >=
select ํน์ฑ1, ํน์ฑ2 from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ1 > "ํน์ ๊ฐ";
select ํน์ฑ1, ํน์ฑ2 from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ1 <= "ํน์ ๊ฐ";
- ๋ฌธ์์ด์์ ํน์ ๊ฐ๊ณผ ๋น์ทํ ๊ฐ๋ค ํํฐ: like, \\%, \\*
select ํน์ฑ1, ํน์ฑ2 from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ2 like "%ํน์ ๋ฌธ์์ด%";
- ๋ฆฌ์คํธ ๊ฐ๋ค๊ณผ ์ผ์นํ๋ ๋ฐ์ดํฐ ํํฐ: in
select ํน์ฑ1, ํน์ฑ2 from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ2 in("ํน์ ๊ฐ1","ํน์ ๊ฐ2");
- ๊ฐ์ด ์์ ๋(= null): is
select * from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ1 is null;
- ๊ฐ์ด ์๋ ๊ฒฝ์ฐ ์ ์ธ: not
select * from ํ
์ด๋ธ์ด๋ฆ
where ํน์ฑ1 is not null;
orderby
- ๋ฐ์ดํฐ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ค ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ ์ง ๊ฒฐ์ ํ๋ฉฐ, ์ ํ์ ์ผ๋ก ์ฌ์ฉ
- ๊ธฐ๋ณธ ์ ๋ ฌ: ์ค๋ฆ์ฐจ์
select * from ํ
์ด๋ธ์ด๋ฆ
order by ํน์ฑ1;
- ๋ด๋ฆผ์ฐจ์
select * from ํ
์ด๋ธ์ด๋ฆ
order by ํน์ฑ1 desc;
limit
- ์ฟผ๋ฆฌ๋ฌธ ์ ์ผ ๋ง์ง๋ง์ ์ฌ์ฉํ๋ฉฐ, ์ถ๋ ฅํ ๋ฐ์ดํฐ ๊ฐ์ ์ ํจ
- ์ ํ์ ์ผ๋ก ์ฌ์ฉ
- ๋ฐ์ดํฐ ๊ฐ์(๊ฒฐ๊ณผ) 200๊ฐ๋ง ์ถ๋ ฅ
select * from ํ
์ด๋ธ์ด๋ฆ limit 200;
distinct
- ์ค๋ณต๊ฐ ์ ๊ฑฐ
- ํน์ฑ1์ ๊ธฐ์ค์ผ๋ก
select distinct ํน์ฑ1 from ํ
์ด๋ธ์ด๋ฆ;
- ํน์ฑ1, ํน์ฑ2, ํน์ฑ3์ ์ ๋ํฌํ ‘์กฐํฉ’ ๊ฐ๋ค ์ ํ
select distinct ํน์ฑ1, ํน์ฑ2, ํน์ฑ3 from ํ
์ด๋ธ์ด๋ฆ;
inner join
- ์๋ก ๊ณตํต๋ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ ์ด์์ ํ ์ด๋ธ ์ฐ๊ฒฐ(join)
select *
from ํ
์ด๋ธ1
join ํ
์ด๋ธ2 on ํ
์ด๋ธ1.ํน์ฑA = ํ
์ด๋ธ2.ํน์ฑB;
outer join
- left outer join - left inclusive ์คํ
select *
from ํ
์ด๋ธ1
left outer join ํ
์ด๋ธ2 on ํ
์ด๋ธ1.ํน์ฑA = ํ
์ด๋ธ2.ํน์ฑB;
- right outer join - right inclusive ์คํ
select *
from ํ
์ด๋ธ1
right outer join ํ
์ด๋ธ2 on ํ
์ด๋ธ1.ํน์ฑA = ํ
์ด๋ธ2.ํน์ฑB;
์ฌ๋ฌ ์ฟผ๋ฆฌ๋ฌธ ํ ๋ฒ์ ์จ๋ณด๊ธฐ
- ์ ๋ฝ์์ ์จ ๊ณ ๊ฐ์ ๋์๋ณ๋ก ๋ฌถ์ ๋ค, ๊ฐ ๋์ ์์ ๋ฐ๋ผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ , CustomerId์ ๋ฐ๋ผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ 3๊ฐ์ ๊ฒฐ๊ณผ๋ง ์์ฒญ
select c.CustomerId, c.FirstName, count(c.City) as 'City Count'
from customers as c
join employees as e on c.SuportRepId = e.EmployeeId
where c.Country = 'Europe'
group by c.city
order by 3 desc, c.CustomerId asc
limit 3;
๐ SQL ๋ด์ฅํจ์
๐ค ์งํฉ์ฐ์ฐ: ๋ ์ฝ๋ ์กฐํ → ๋ถ๋ฅ → ํน์ ์์
group by
- ๊ทธ๋ฃน์ผ๋ก ๋ฌถ์ด์ ๋ฐ์ดํฐ ์กฐํ
select * from customers
group by state;
having
- group by๋ก ๊ทธ๋ฃนํํ ๊ฒฐ๊ณผ์ ๋ํ ํํฐ๋ง
- where์ ์ฐจ์ด์ ์ where๋ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ํํฐ๋ง
select customerId, avg(total) from invoices
group by customerId
having avg(total) > 6;
count()
- ๋ฐ์ดํฐ์ ๊ฐ์๋ฅผ ์ ๋ ์ฌ์ฉ
select state, count(*) from customers
group by states;
sum()
- ๋ฐ์ดํฐ ํฉ ๊ณ์ฐ
select invoiceId, sum(unitPrice)
from invoice_items
group by invoiceId;
-- invoice_items ํ
์ด๋ธ์์ invoiceId ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํํ๊ณ
-- unitPrice ํ๋ ํฉ์ ๊ตฌํจ
avg()
- ๋ฐ์ดํฐ ํ๊ท ๊ฐ ๊ณ์ฐ
select trackId, avg(unitPrice)
from invoice_items
group by trackId;
max(), min()
- ๊ฐ ๋ฐ์ดํฐ์ ์ต๋๊ฐ, ์ต์๊ฐ ๊ณ์ฐ
select customerId, min(total)
from invoices
group by customerId;
select ์คํ ์์
- from → where → group by → having → select → order by
select customerid, avg(total) -- ์กฐํ๋ ๊ฒฐ๊ณผ์์ customerId ํ๋์ total ํ๋ ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ
from invoices -- invoices ํ
์ด๋ธ์์
where customerId >= 10 -- customerId ํ๋๊ฐ 10 ์ด์์ธ ๋ ์ฝ๋ ์กฐํ
group by customerId -- customerId ๊ธฐ์ค์ผ๋ก ๊ทธ๋ฃนํ
having sum(total) >= 30 -- total ํ๋์ ์ดํฉ์ด 30 ์ด์์ธ ๊ฒฐ๊ณผ๋ค๋ง ํํฐ๋ง
order by 2; -- ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ๊ฒฐ๊ณผ ๋ฆฌํด
'WILT > DB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DB] SQL vs NoSQL (0) | 2022.08.07 |
---|
Comments