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

์ž์ฒด ์กฐ์ธ, MySQL ๋ฐ์ดํ„ฐ ํ˜•์‹

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

์ž์ฒด ์กฐ์ธ(self join)

-- ์ž์‹ ๊ณผ ์ž์‹ ์ด ์กฐ์ธ

-- ํ…Œ์ด๋ธ”์€ 1๊ฐœ๋งŒ ์‚ฌ์šฉ

-- ์˜ˆ) ํšŒ์‚ฌ์˜ ์กฐ์ง ๊ด€๊ณ„

 

-- ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

create table emp_table (emp char(4), manager char(4), phone varchar(8));

 

insert into emp_table values('๋Œ€ํ‘œ', null, '0000');

insert into emp_table values('์˜์—…์ด์‚ฌ', null, '1111');

insert into emp_table values('๊ด€๋ฆฌ์ด์‚ฌ', '๋Œ€ํ‘œ', '2222');

insert into emp_table values('์ •๋ณด์ด์‚ฌ', '๋Œ€ํ‘œ', '3333');

insert into emp_table values('์˜์—…๊ณผ์žฅ', '์˜์—…์ด์‚ฌ', '1111-1');

insert into emp_table values('๊ฒฝ๋ฆฌ๋ถ€์žฅ', '๊ด€๋ฆฌ์ด์‚ฌ', '2222-1');

insert into emp_table values('์ธ์‚ฌ๋ถ€์žฅ', '๊ด€๋ฆฌ์ด์‚ฌ', '2222-2');

insert into emp_table values('๊ฐœ๋ฐœํŒ€์žฅ', '์ •๋ณด์ด์‚ฌ', '3333-1');

insert into emp_table values('๊ฐœ๋ฐœ์ฃผ์ž„', '์ •๋ณด์ด์‚ฌ', '3333-2');

 

select * from emp_table;

 

-- ์ž์ฒด ์กฐ์ธ์˜ ๊ธฐ๋ณธ ํ˜•์‹

 

select ์—ด๋ชฉ๋ก

from ํ…Œ์ด๋ธ” ๋ณ„์นญA

inner join ํ…Œ์ด๋ธ” ๋ณ„์นญb

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

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

 

-- ๊ฒฝ๋ฆฌ ๋ถ€์žฅ์˜ ์ง์† ์ƒ๊ด€ ์—ฐ๋ฝ์ฒ˜๋ฅผ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด

select a.emp ์ง์›, b.emp ์ง์†์ƒ๊ด€, b.phone ์ง์†์ƒ๊ด€ ์—ฐ๋ฝ์ฒ˜,

from emp_table a

inner join emp_table b

on a.manager = b.emp

where a.emp = '๊ฒฝ๋ฆฌ๋ถ€์žฅ';

 

<<< ๋ญ”๊ฐ€ ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ๋Š”์ง€ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค................... ์ˆ˜์ •ํ•ด์„œ ์—…๋กœ๋“œ ํ•ด์•ผ๊ฒ ๋‹ค!!!!!

 

 

-- ์—ฐ์Šต๋ฌธ์ œ

-- 1. buy ํ…Œ์ด๋ธ”๊ณผ member ํ…Œ์ด๋ธ”์„ ์ด์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์ด๋ผ๋„ ๊ตฌ๋งค๊ธฐ๋ก์ด ์žˆ๋Š” ํšŒ์›๋“ค ๋ฆฌ์ŠคํŠธ ์ถ”์ถœํ•˜๊ธฐ

-- ์กฐํšŒํ•  ์—ด : mem_id, addr.์ค‘๋ณต์ด ์—†๋„๋ก.

 

select distinct m.mem_id, m.addr

from buy b

inner join member m

on b.mem_id = m.mem_id

order by mem_id;

 

 

์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” aac outcomes.csv ์ž๋ฃŒ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€๋กœ

 

CREATE TABLE aac_outcomes (

age_upon_outcome varchar(15) COLLATE utf8mb4_general_ci NOT NULL,

animal_id char(7) COLLATE utf8mb4_general_ci NOT NULL,

animal_type varchar(10) COLLATE utf8mb4_general_ci NOT NULL,

breed varchar(70) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,

color varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,

date_of_birth datetime NOT NULL,

datetime datetime NOT NULL,

monthyear varchar(90) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,

name varchar(20) COLLATE utf8mb4_general_ci NOT NULL,

outcome_subtype varchar(25) COLLATE utf8mb4_general_ci NOT NULL,

outcome_type varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,

sex_upon_outcome varchar(20) COLLATE utf8mb4_general_ci NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

 

์œ„์˜ ์ฝ”๋“œ ์‹คํ–‰ํ•˜๊ธฐ

 

-- 2. ์ฒœ์žฌ์ง€๋ณ€์œผ๋กœ ์ธํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์‹ค๋˜์—ˆ์Œ.

-- ์ž…์–‘์„ ๊ฐ„ ๊ธฐ๋ก์€ ์žˆ๋Š”๋ฐ, ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๊ธฐ๋ก์ด ์—†๋Š” ๋™๋ฌผ์˜ id์™€ ์ด๋ฆ„์„ id์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒ

-- ์กฐํšŒํ•  ์—ด : animal_id, name, outs.datetime, ins.datetime

 

select ins.animal_id, ins.name, outs.datetime, ins.datetime

from outs.datetime outs

inner join ins.datetime ins

on outs.animal_id = ins.animal_id

where ins.datetime is null

order by animal_id;

 

select outs.animal_id, outs.name, outs.datetime, ins.datetime

from aac_outcomes outs

left outer join aac_intakes ins

on outs.animal_id = ins.animal_id

where ins.animal_id is null

order by outs.animal_id asc;

 

 

-- 3. ๊ด€๋ฆฌ์ž์˜ ์‹ค์ˆ˜๋กœ ์ผ๋ถ€ ๋™๋ฌผ์˜ ์ž…์–‘์ผ์ด ์ž˜๋ชป ์ž…๋ ฅ๋˜์—ˆ์Œ

-- ๋ณดํ˜ธ ์‹œ์ž‘์ผ๋ณด๋‹ค ์ž…์–‘์ผ์ด ๋” ๋น ๋ฅธ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” sql๋ฌธ์„ ์ž‘์„ฑํ•˜๊ธฐ

-- ๋‹จ, ๋ณดํ˜ธ์‹œ์ž‘์ผ์ด ๋น ๋ฅธ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผํ•จ

-- ์กฐํšŒํ•  ์—ด : animal_id, name, outs.datetime, ins.datetime

 

select ins.animal_id, ins.name, outs.datetime outtime, ins.datetime intime

from aac_intakes ins

inner join aac_outcomes outs

on ins.animal_id = outs.animal_id

where outs.datetime <= ins.datetime

order by ins.datetime asc;

 

-- 4. ์•„์ง ์ž…์–‘์„ ๋ชป ๊ฐ„ ๋™๋ฌผ ์ค‘, ๊ฐ€์žฅ ์˜ค๋ž˜ ๋ณดํ˜ธ์†Œ์— ์žˆ์—ˆ๋˜ ๋™๋ฌผ 3๋งˆ๋ฆฌ์˜ ์ด๋ฆ„๊ณผ ๋ณดํ˜ธ ์‹œ์ž‘์ผ์„ ์กฐํšŒํ•˜๋Š” sql๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ

-- ์ด ๋•Œ, ๊ฒฐ๊ณผ๋Š” ๋ณดํ˜ธ์‹œ์ž‘์ผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์กฐํšŒ

-- ์กฐํšŒํ•  ์—ด : name, datetime

 

select ins.name, ins.datetime, outs.datetime

from aac_intakes ins

left outer join aac_outcomes outs

on ins.name = outs.name

where outs.datetime is null

order by outs.datetime desc

limit 3;

 

select ins.name, ins.datetime

from aac_intakes ins

left outer join aac_outcomes outs

on ins.animal_id = outs.animal_id

where outs.animal_id is null

and ins.name != '' -- ์ด๋ฆ„์ด ๋นˆ์นธ์ธ ๊ฒฝ์šฐ ์ œ์™ธ(์„ ํƒ)

and ins.name not like '*%' -- ์ด๋ฆ„์ด *๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ์ œ์™ธ(์„ ํƒ)

order by ins.datetime asc

limit 3;

 

-- 5. ๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™” ์ˆ˜์ˆ ์„ ๊ฑฐ์นœ ๋™๋ฌผ ์ •๋ณด๋ฅผ ์•Œ์•„๋ณด๋ ค ํ•จ

-- ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜ฌ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋˜์ง€ ์•Š์•˜์ง€๋งŒ

-- ๋ณดํ˜ธ์†Œ๋ฅผ ๋‚˜๊ฐˆ ๋‹น์‹œ์—๋Š” ์ค‘์„ฑํ™”๋œ ๋™๋ฌผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ

-- ์กฐํšŒํ•  ์—ด : animal_id, animal_type, name, sex_upon_intake, sex_upon_outcome

-- ์ •๋ ฌ ์ˆœ์„œ : animal_id ์˜ค๋ฆ„์ฐจ์ˆœ

 

select * from aac_intakes;

select ins.animal_id, ins.animal_type, ins.name, ins.sex_upon_intake, outs.sex_upon_outcome

from aac_intakes ins

inner join aac_outcomes outs

on ins.animal_id = outs.animal_id

where ins.sex_upon_intake != outs.sex_upon_outcome

order by ins.animal_id asc;

 

-- MySQL ๋ฐ์ดํ„ฐ ํ˜•์‹

-- ๋ฐ์ดํ„ฐ ํ˜•์‹์€ ํฌ๊ฒŒ ์ˆซ์žํ˜•,๋ฌธ์žํ˜•, ๋‚ ์งœํ˜•์ด ์žˆ์Œ

-- ์‹ค์ œ๋กœ ์ €์žฅ๋  ๋ฐ์ดํ„ฐ์˜ ํ˜•ํƒœ๊ฐ€ ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

-- ์ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์œ„์˜ ๋ฐ์ดํ„ฐ ํ˜•์‹์—์„œ ์„ธ๋ถ€์ ์œผ๋กœ ๋‹ค์‹œ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋‚˜๋‰จ

-- ์˜ˆ) ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด 100๊ธ€์ž๋ฅผ ์ €์žฅํ•  ์นธ์„ ์ค€๋น„ํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ž„

 

-- ์ •์ˆ˜ํ˜•

-- -- tinyint (์ˆซ์ž ๋ฒ”์œ„ : -128 ~ 127)

-- -- smallint (์ˆซ์ž ๋ฒ”์œ„ : -32768 ~ 32767)

-- -- int (์ˆซ์ž ๋ฒ”์œ„ : ์•ฝ -21์–ต ~ 21์–ต)

-- -- bright (์ˆซ์ž ๋ฒ”์œ„ : ์•ฝ -900๊ฒฝ ~ 900๊ฒฝ)

 

create table tbl_type (

tinyint_col tinyint,

smallint_col smallint,

int_col int,

bigint_col bigint); # ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค ๋•Œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ ํ˜•์‹ ์ง€์ •

 

-- ๊ฐ ์—ด์˜ ์ตœ๋Œ“๊ฐ’๊นŒ์ง€๋Š” ์ด์ƒ์—†์ด ์ž…๋ ฅ ๊ฐ€๋Šฅ

insert into tbl_type values(127, 32767, 2147483647, 900000000000000000);

select * from tbl_type;

 

-- ์ตœ๋Œ“๊ฐ’์— 0์„ ๋” ๋ถ™์—ฌ์„œ ์ž…๋ ฅํ•ด๋ณด๊ธฐ

insert into tbl_type values(1270, 327670, 21474836470, 9000000000000000000);

 

-- ๋ฌธ์žํ˜•

-- ๋ฌธ์žํ˜•์€ ๊ธ€์ž๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

-- ์ž…๋ ฅํ•  ์ตœ๋Œ€ ๊ธ€์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•จ

-- -- char(๊ฐœ์ˆ˜)

-- -- varchar(๊ฐœ์ˆ˜)

 

-- char๋Š” Character์˜ ์•ฝ์ž๋กœ, ๊ณ ์ •๊ธธ์ด ๋ฌธ์žํ˜•์ด๋ผ๊ณ  ๋ถ€๋ฆ„

-- ์ž๋ฆฟ์ˆ˜๊ฐ€ ๊ณ ์ •๋จ

-- ์˜ˆ) char(10)์— "๊ฐ€๋‚˜๋‹ค" 3๊ธ€์ž๋งŒ ์ €์žฅํ•ด๋„ 10์ž๋ฆฌ๋ฅผ ๋ชจ๋‘ ํ™•๋ณดํ•œ ํ›„์— ์•ž์˜ 3์ž๋ฆฌ๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ๋’ค์˜ 7์ž๋ฆฌ๋Š” ๋‚ญ๋น„ํ•˜๊ฒŒ ๋จ

 

-- varchar๋Š” ๊ฐ€๋ณ€๊ธธ์ด ๋ฌธ์žํ˜•์œผ๋กœ, "๊ฐ€๋‚˜๋‹ค" 3๊ธ€์ž๋ฅผ ์ €์žฅํ•˜๋ฉด 3์ž๋ฆฌ๋งŒ ์‚ฌ์šฉํ•จ(Variable Character)

 

-- vachar๋Š” char๋ณด๋‹ค ๊ณต๊ฐ„์„ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ MySQL์˜ ์ฒ˜๋ฆฌ ์†๋„๋Š” CHAR๊ฐ€ ๋” ๋น ๋ฆ„

-- ์˜ˆ) ๊ฑฐ์ฃผ ์ง€์—ญ ์ปฌ๋Ÿผ์— ์„œ์šธ/๋ถ€์‚ฐ/๊ฒฝ๋ถ/์ „๋‚จ๊ณผ ๊ฐ™์ด ๋ชจ๋‘ 2๊ธ€์ž๋กœ ์ผ์ •ํ•œ ๊ฒฝ์šฐ์—๋Š” char(2)๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์Œ

-- ๋ฐ˜๋ฉด์— ๊ฐ€์ˆ˜ ์ด๋ฆ„์€ ๊ธ€์ž ์ˆ˜๊ฐ€ ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— varchar๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์Œ

 

-- ์œ„์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ member ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑ

create table member -- ํšŒ์› ํ…Œ์ด๋ธ”

( mem id char(8) not null primary key, -- ์‚ฌ์šฉ์ž ์•„์ด๋””(pk)

mem_name varchar(10) not null, -- ์ด๋ฆ„

mem_number tinyint not null, -- ์ธ์›์ˆ˜

addr char(2) not null, -- ์ง€์—ญ(๊ฒฝ๊ธฐ, ์„œ์šธ, ๊ฒฝ๋‚จ ์‹์œผ๋กœ 2๊ธ€์ž๋งŒ ์ž…๋ ฅ)

phone1 char(3), -- ์—ฐ๋ฝ์ฒ˜์˜ ๊ตญ๋ฒˆ(02, 031, 055 ๋“ฑ)

phone2 char(8), -- ์—ฐ๋ฝ์ฒ˜์˜ ๋‚˜๋จธ์ง€ ์ „ํ™”๋ฒˆํ˜ธ(ํ•˜์ดํ”ˆ ์ œ์™ธ)

height tinyint unsigned, -- ํ‰๊ท  ํ‚ค # ๋ถ€ํ˜ธ๋ฅผ ๋—€๋‹ค

debut_date date); -- ๋ฐ๋ท” ์ผ์ž