๐Ÿ“‚๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค/๐Ÿ“ŠDBMS

๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ

๐Ÿ‘ฉ‍๐ŸŽ“์ธํ…”๋ฆฌ๊ฐ์ž๐Ÿฅ” 2023. 5. 15. 17:52

๋ฐ์ดํ„ฐ ์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ

-- insert

-- ๋ฐ์ดํ„ฐ ์ž…๋ ฅ

 

-- insert ๋ฌธ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

-- insert into ํ…Œ์ด๋ธ” (์—ด1, ์—ด2, ์—ด3, ...) values (๊ฐ’1, ๊ฐ’2, ๊ฐ’3, ...)

 

-- ์ฃผ์˜์ 

-- ํ…Œ์ด๋ธ” ์ด๋ฆ„ ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ์—ด์€ ์ƒ๋žต ๊ฐ€๋Šฅํ•จ

-- ๋‹ค๋งŒ, ์ƒ๋žตํ•  ์‹œ์—๋Š” values ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ๊ฐ’๋“ค์˜ ์ˆœ์„œ์™€ ๊ฐœ์ˆ˜๋Š” ์—ด์˜ ์ˆœ์„œ ๋ฐ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•ด์•ผํ•จ

 

create table toy_shop (toy_id int, toy_name char(4), age int);

insert into toy_shop values (1, '์šฐ๋””', 25)

 

select * from toy_shop;

 

-- ์•„์ด๋””์™€ ์ด๋ฆ„๋งŒ ์ž…๋ ฅํ•˜๊ณ  ๋‚˜์ด๋Š” ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค๋ฉด

insert into toy_shop (toy_id, toy_name) values (2, '๋ฒ„์ฆˆ');

select * from toy_shop;

 

-- ์—ด์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์„œ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•จ

insert into toy_shop (toy_name, age, toy_id) values ('์ œ์‹œ', 20, 3);

select * from toy_shop;

 

 

-- ํ…Œ์ด๋ธ” ์‚ญ์ œ

drop table toy_shop;

 

์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ’ ์ž…๋ ฅ

-- auto_increment

-- ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ’ ์ž…๋ ฅ

-- insert๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•ด๋‹น ์—ด์€ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ’์ด ์ž…๋ ฅ๋จ

 

-- ์ฃผ์˜์ 

-- auto_increment๋กœ ์ง€์ •ํ•˜๋Š” ์—ด์€ ๋ฐ˜๋“œ์‹œ primary key๋กœ ์ง€์ •ํ•ด์ค˜์•ผ ํ•จ

-- primary key: ๊ฐ ํ–‰์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์œ ์ผํ•œ ๊ฐ’

create table toy_shop2 (

toy_id int auto_increment primary key,

toy_name char(4),

age int);

 

-- auto_increment ์—ด์€ ๋น„์›Œ๋‘๊ณ  ๋ฐ์ดํ„ฐ ์ž…๋ ฅ ํ•ด๋ณด๊ธฐ

insert into toy_shop2 values (null, '๋ณดํ•', 25);

insert into toy_shop2 values (null, '์Šฌ๋งํ‚ค', 22);

insert into toy_shop2 values (null, '๋ ‰์Šค', 21);

select * from toy_shop2;

 

-- ์–ด๋Š ์ˆซ์ž๊นŒ์ง€ ์ฆ๊ฐ€๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

select last_insert_id();

-- auto_increment๋กœ ์ž…๋ ฅ๋˜๋Š” ๋‹ค์Œ ๊ฐ’์„ 100๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

alter table toy_shop2 auto_increment = 100;

insert into toy_shop2 values (null, 'ํ–„', 35);

select * from toy_shop2;

 

-- auto_increment๋กœ ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์„ 1000์œผ๋กœ ์ง€์ •ํ•˜๊ณ , 3์”ฉ ์ฆ๊ฐ€ํ•˜๋„๋ก ์„ค์ •

truncate table toy_shop2; -- toy shop2 table ์ดˆ๊ธฐํ™”

alter table toy_shop2 auto_increment = 1000;

set @@auto_increment_increment = 3;

 

insert into toy_shop2 values (null, 'ํฌํ…Œ์ดํ† ', 20);

insert into toy_shop2 values (null, '์•ค๋””', 23);

insert into toy_shop2 values (null, '์•Œ๋ฆฐ', 25);

select * from toy_shop2;

 

 

-- world.ciry ํ…Œ์ด๋ธ” ํ”„๋กœํผํ‹ฐ ๋ณด๊ธฐ

desc world.city;

 

-- ๋ฐ์ดํ„ฐ ์‚ดํŽด๋ณด๊ธฐ

select * from world.city limit 5;

 

-- ๋„์‹œ ์ด๋ฆ„๊ณผ ์ธ๊ตฌ๋ฅผ ๊ฐ€์ ธ์™€ ํ…Œ์ด๋ธ” ๊ตฌ์„ฑํ•˜๊ธฐ

create table city_popul (city_name char(35), population int);

 

๋งˆ์ผ“๋””๋น„ ์šฐํด๋ฆญ ํ›„ ์ƒˆ๋กœ๊ณ ์นจ

 

-- ๋„์‹œ ์ด๋ฆ„๊ณผ ์ธ๊ตฌ๋ฅผ ๊ฐ€์ ธ์™€ ํ…Œ์ด๋ธ” ๊ตฌ์„ฑํ•˜๊ธฐ

create table city_popul (city_name char(35), population int);

select name, population from world.city;

insert into city_popul select name, population from world.city;

 

select * from city_popul;

 

-- ๋ฐ์ดํ„ฐ ์ˆ˜์ •

-- update

 

-- update๋ฌธ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

update ํ…Œ์ด๋ธ” ์ด๋ฆ„ set ์—ด1 = ๊ฐ’1, ์—ด2 = ๊ฐ’2.... where ์กฐ๊ฑด;

 

-- city popul ํ…Œ์ด๋ธ”์˜ ๋„์‹œ ์ด๋ฆ„ ์ค‘์—์„œ 'Seoul'์„ '์„œ์šธ'๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

select * from city_popul where city_name = '์„œ์šธ';

select * from city_popul where city_name = 'seoul';

 

update city_popul set city_name = '์„œ์šธ' where city_name = 'Seoul';

select * from city_popul where city_name = '์„œ์šธ';

 

where ์ ˆ์˜ ์ƒ๋žต

-- ์ฃผ์˜์ 

-- update๋ฌธ์—์„œ where ์ ˆ์€ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, where์ ˆ์„ ์ƒ๋žตํ•˜๋ฉด ๋ชจ๋“  ํ–‰์˜ ๊ฐ’์ด ๋ฐ”๋€œ

-- city_popul ํ…Œ์ด๋ธ”์˜ ์ธ๊ตฌ๋ฅผ 10000๋ช… ๋‹จ์œ„๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋“ฑ์˜ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ฃผ์˜ํ•ด์•ผ ํ•จ

update city_popul set population = population / 10000;

select * from city_popul limit 5;

 

 

๋ฐ์ดํ„ฐ ์‚ญ์ œ

 

-- delete

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

-- delete ๋ฌธ์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

delete from ํ…Œ์ด๋ธ”์ด๋ฆ„ where ์กฐ๊ฑด;

 

-- city_popul ํ…Œ์ด๋ธ”์—์„œ 'New'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋„์‹œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

select * from city_popul cp where city_name like 'New%';

delete from city_popul where city_name like 'New%';

 

-- ๋งŒ์•ฝ์— 'New'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋„์‹œ ์ค‘ ์ƒ์œ„ ๋ช‡ ๊ฑด๋งŒ ์‚ญ์ œํ•˜๋ ค๋ฉด limit์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

delete from city_popul where city_name like 'New%' limit 5;

 

 

JOIN

 

 

-- ์กฐ์ธ

-- ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์„œ๋กœ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ด๋Š” ๊ฒƒ

-- ์˜ˆ) ์ธํ„ฐ๋„ท ๋งˆ์ผ“ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์˜ ํšŒ์› ํ…Œ์ด๋ธ”๊ณผ ๊ตฌ๋งค ํ…Œ์ด๋ธ”

-- -- ํšŒ์› ํ…Œ์ด๋ธ”์—๋Š” ํšŒ์›์˜ ์ด๋ฆ„, ์—ฐ๋ฝ์ฒ˜

-- -- ๊ตฌ๋งค ํ…Œ์ด๋ธ”์—๋Š” ๊ตฌ๋งคํ•œ ๋ฌผ๊ฑด์— ๋Œ€ํ•œ ์ •๋ณด

 

-- ๊ด€๊ณ„ํ˜• db์—์„œ ๋ฐ์ดํ„ฐ๋Š” ์ฃผ์ œ์— ๋”ฐ๋ผ ๋ถ„๋ฆฌํ•ด์„œ ์ €์žฅํ•˜๊ณ  ์žˆ๊ณ  ์ด ๋ถ„๋ฆฌ๋œ ํ…Œ์ด๋ธ”์€ ์„œ๋กœ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ์žˆ์Œ

-- ํ…Œ์ด๋ธ”์˜ ์กฐ์ธ์„ ์œ„ํ•ด์„œ๋Š” ํ…Œ์ด๋ธ”์ด ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋กœ ์—ฐ๊ฒฐ๋˜์–ด์•ผ ํ•จ

-- -- ์ผ๋Œ€๋‹ค ๊ด€๊ณ„ : ํšŒ์› ํ…Œ์ด๋ธ”์˜ ์•„์ด๋””์—๋Š” ํ•˜๋‚˜์˜ ๊ฐ’์ด ํ•œ ๋ฒˆ๋งŒ ๋“ฑ์žฅํ•ด์•ผ ํ•˜์ง€๋งŒ

-- -- ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์—๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ด€๊ณ„

-- -- ์˜ˆ) ํšŒ์› ํ…Œ์ด๋ธ”์—์„œ ํšŒ์› ์•„์ด๋””๋Š” ํ•œ ๋ฒˆ๋งŒ ๋“ฑ์žฅํ•˜์ง€๋งŒ ๊ตฌ๋งค ํ…Œ์ด๋ธ”์—์„œ๋Š” ํ•œ ์•„์ด๋””๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ

-- -- ์ด๋•Œ, ํšŒ์› ํ…Œ์ด๋ธ”์˜ ์•„์ด๋””๋ฅผ ๊ธฐ๋ณธํ‚ค(Primary Key), ๊ตฌ๋งค ํ…Œ์ด๋ธ”์˜ ์•„์ด๋””๋ฅผ ์™ธ๋ž˜ํ‚ค(Foreign Key)๋กœ ์ง€์ •

-- -- -- ๊ธฐ๋ณธํ‚ค : ํ…Œ์ด๋ธ”์—์„œ ์œ ์ผํ•œ ๊ฐ’์œผ๋กœ, ๊ฐ ํ–‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ผํ•˜๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’

-- -- -- ์™ธ๋ž˜ํ‚ค : ๋‘ ํ…Œ์ด๋ธ”์„ ์„œ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐ์— ์‚ฌ์šฉํ•˜๋Š” ํ‚ค.

-- -- -- ์™ธ๋ž˜ํ‚ค๊ฐ€ ํฌํ•จ๋œ ํ…Œ์ด๋ธ”์„ ์ž์‹ํ…Œ์ด๋ธ”, ์™ธ๋ž˜ํ‚ค ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ๋ถ€๋ชจํ…Œ์ด๋ธ”์ด๋ผ๊ณ  ํ•จ

 -- -- -- ์œ ์‚ฌ ์‚ฌ๋ก€) ํšŒ์‚ฌ์› ํ…Œ์ด๋ธ”๊ณผ ๊ธ‰์—ฌ ํ…Œ์ด๋ธ”, ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ์„ฑ์  ํ…Œ์ด๋ธ”

 

-- ๋‚ด๋ถ€ ์กฐ์ธ์˜ ํ˜•์‹

select ์—ด๋ชฉ๋ก

from ์ฒซ๋ฒˆ์งธ ํ…Œ์ด๋ธ”

inner join ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”

on ์กฐ์ธ๋  ์กฐ๊ฑด

where ๊ฒ€์ƒ‰ ์กฐ๊ฑด;

 

-- ๊ตฌ๋งค ํ…Œ์ด๋ธ”์—์„œ GRL์ด๋ผ๋Š” ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ์‚ฌ๋žŒ์ด ๊ตฌ๋งคํ•œ ๋ฌผ๊ฑด์„ ๋ฐœ์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ

-- ๊ตฌ๋งค์ž ์ด๋ฆ„๊ณผ ์ฃผ์†Œ, ์—ฐ๋ฝ์ฒ˜๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค๋ฉด

select *

from buy

inner join member

on buy.mem_id = member.mem_id

where buy.mem_id = 'GRL';

 

-- where ์กฐ๊ฑด์ ˆ์„ ์ƒ๋žตํ•œ๋‹ค๋ฉด

select *

from buy

inner join member

on buy.mem_id = member.mem_id;

 

-- ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ์ถ”์ถœํ•˜๊ธฐ

select mem_id , mem_name, prod_name, addr, concat(phone1, phone2) ์—ฐ๋ฝ์ฒ˜

from buy

inner join member

on buy.mem_id = member.mem_id ;

-- mem_id๊ฐ€ ์–‘์ชฝ ํ…Œ์ด๋ธ”์— ๋‘˜ ๋‹ค ์กด์žฌํ•ด์„œ ์—๋Ÿฌ

 

select buy.mem_id, mem_name, prod_name, addr, concat(phone1, phone2) ์—ฐ๋ฝ์ฒ˜

from buy

inner join member

on buy.mem_id = member.mem_id;

 

-- sql๋ฌธ์„ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ…Œ์ด๋ธ” ์ด๋ฆ„.์—ด์ด๋ฆ„ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ

 

select buy.mem_id,

member.mem_name,

buy.prod_name,

member.addr,

concat(member.phone1, member.phone2) ์—ฐ๋ฝ์ฒ˜

from buy

inner join member

on buy.mem_id = member.mem_id;

 

๋ณ„์นญ

-- ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์ง€๋Š” ๋ฌธ์ œ๋ฅผ ๋ณ„์นญ์œผ๋กœ ํ•ด๊ฒฐ

-- ๊ฐ€์žฅ ๊ถŒ์žฅ๋˜๋Š” ํ˜•ํƒœ์˜ sql ๋ฌธ!!

select b.mem_id, m.mem_name, b.prod_name, m.addr, concat(m.phone1, m.phone2) ์—ฐ๋ฝ์ฒ˜

from buy b

inner join member m

on b.mem_id = m.mem_id;

 

-- ๊ฒฐ๊ณผ๋ฅผ ํšŒ์› ์•„์ด๋”” ์ˆœ์œผ๋กœ ์ •๋ ฌ

select b.mem_id, m.mem_name, b.prod_name, m.addr, concat(m.phone1, m.phone2) ์—ฐ๋ฝ์ฒ˜

from buy b

inner join member m

on b.mem_id = m.mem_id

order by m.mem_id asc;

 

-- ํ˜„์žฌ๋Š” ๊ตฌ๋งคํ•œ ๊ธฐ๋ก์ด ์žˆ๋Š” ํšŒ์›๋“ค์˜ ๋ชฉ๋ก์ž„

-- ๋‚ด๋ถ€ ์กฐ์ธ : ๋‘ ํ…Œ์ด๋ธ”์— ๋ชจ๋‘ ์žˆ๋Š” ๋‚ด์šฉ๋งŒ ์กฐ์ธ๋˜๋Š” ๋ฐฉ์‹

-- ์™ธ๋ถ€ ์กฐ์ธ : ์–‘์ชฝ ์ค‘ ํ•œ ๊ณณ์ด๋ผ๋„ ๋‚ด์šฉ์ด ์žˆ์„ ๋•Œ ์กฐ์ธ

 

-- ์™ธ๋ถ€ ์กฐ์ธ์˜ ํ˜•์‹

select ์—ด ๋ชฉ๋ก

from ์ฒซ ๋ฒˆ์งธ ํ…Œ์ด๋ธ”(left)

<left|right> outer join ๋‘ ๋ฒˆ์งธ ํ…Œ์ด๋ธ” (right)

on ์กฐ์ธ๋  ์กฐ๊ฑด

where ๊ฒ€์ƒ‰ ์กฐ๊ฑด;

-- ๊ตฌ๋งค ๊ธฐ๋ก์ด ์—†๋Š” ํšŒ์›์„ ํฌํ•จํ•œ ์ „์ฒด ํšŒ์›์˜ ๊ตฌ๋งค ๊ธฐ๋ก ์ถœ๋ ฅ

select m.mem_id, m.mem_name, b.prod_name, m.addr

from member m

left outer join buy b

on m.mem_id = b.mem_id

order by m.mem_id asc;

 

-- ํšŒ์› ๊ฐ€์ž…๋งŒ ํ•˜๊ณ  ํ•œ ๋ฒˆ๋„ ๊ตฌ๋งคํ•œ ์ ์ด ์—†๋Š” ํšŒ์›์˜ ๋ชฉ๋ก ์ถ”์ถœ

select distinct m.mem_id, b.prod_name, m.mem_name, m.addr

from member m

left outer join buy b

on m.mem_id = b.mem_id

where b.prod_name is null (= null๋กœ ํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ!!) 

order by m.mem_id asc;

 

-- ์ƒํ˜ธ ์กฐ์ธ(cross join)

-- ํ•œ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธ

-- ์ƒํ˜ธ ์กฐ์ธ ๊ฒฐ๊ณผ์˜ ์ „์ฒด ํ–‰ ์ˆ˜๋Š” ๋‘ ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰ ์ˆ˜์˜ ๊ณฑ

select * from buy;

select * from member;

select * from buy cross join member;