BootCamp 5day

GyeongNamΒ·2023λ…„ 11μ›” 20일
0

BootCamp

λͺ©λ‘ 보기
5/49
post-thumbnail

πŸ“… 2023λ…„ 11μ›” 20일

μ˜€λŠ˜λ„ DMLκ΄€λ ¨ν•΄μ„œ 더 λ§Žμ€ μ‹€μŠ΅μ„ μ§„ν–‰ν–ˆμœΌλ©°, μ œμ•½ 쑰건과 νŠΈλžœμž­μ…˜μ— κ΄€ν•œ κ³΅λΆ€ν–ˆλ‹€. [DB 3일차]


5일차 : DDL 볡슡, μ œμ•½μ‘°κ±΄, νλ¦„μ œμ–΄ , νŠΈλžœμž­μ…˜, DBλ™μ‹œμ„± 이슈, DB격리 μˆ˜μ€€

DDL 볡슡

select * from posts;

insert into posts(id) values(11);

select * from posts where date_format(created_tile1, '%Y') = '2023';

select * from posts where created_tile1 like '2023%';

select * from posts where created_tile1 between '2023-01-01' and '2023-12-31';

select * from posts where created_tile1 >= '2023-01-01' and created_tile1 <= '2023-12-31';

select * from posts where date_format(created_tile1, '%Y') = date_format(now(), '%Y');

ALTER TABLE posts modify column title varchar(255) not null;

auto_increment

alter table author modify column id int auto_increment;

ν”νžˆ λ§ν•˜λŠ” λ„˜λ²„λ§ 을 μœ„ν•΄ μ‚¬μš©ν•˜λ©° update κ°€ κ°€λŠ₯ν•˜λ‹€ (쀑볡은 μ•ˆλ¨) κ°€μž₯ λ§ˆμ§€λ§‰μ˜ μž…λ ₯ν•œ 값을 κΈ°μ€€μœΌλ‘œ 숫자λ₯Ό μ¦κ°€μ‹œν‚¨λ‹€.


μ œμ•½ 쑰건 : unique , foreign key

-- μ œμ•½ 쑰건 검사
select * from information_schema.key_column_usage where table_name = 'author';

alter table author drop constraint email;

alter table author add constraint email2 unique(email);	-- unique μ œμ•½μ‘°κ±΄ μΆ”κ°€

alter table author modify column email varchar(250);    -- μ œμ•½μ‘°κ±΄μ€ 사라지지 μ•ŠλŠ”λ‹€.

-- on update, delete μ„€μ •

-- μ œμ•½ 쑰건 검사
select * from information_schema.key_column_usage where table_name = 'posts'; 

alter table posts drop foreign key post_auth_fk;	-- μ™Έλž˜ν‚€ μ‚­μ œ

alter table posts drop index post_auth_fk;	-- 인덱슀 μ‚­μ œ

-- μ™Έλž˜ν‚€ μΆ”κ°€
alter table posts add constraint post_auth_fk foreign key(auth_id) references author(id) on update cascade; 

-- on update cascade : author ν…Œμ΄λΈ” μˆ˜μ •μ‹œ posts ν…Œμ΄λΈ”λ„ μžλ™ μˆ˜μ •
update author set id = 29 where id = 26;

-- set null : μ°Έμ‘° μˆ˜μ •μ‹œ null μž…λ ₯
alter table posts add constraint post_auth_fk foreign key(auth_id) references author(id) on delete set null on update set null;

show create table posts;

-- CREATE TABLE `posts` (
--    `id` int(11) NOT NULL,
--    `title` varchar(255) NOT NULL,
--    `content` varchar(3000) DEFAULT NULL,
--    `auth_id` int(11) DEFAULT NULL,
--    `price` decimal(10,3) DEFAULT NULL,
--    `created_tile1` datetime DEFAULT current_timestamp(),
--    `created_tile2` datetime(6) DEFAULT current_timestamp(6),
--    `created_tile3` datetime DEFAULT current_timestamp(),
--    PRIMARY KEY (`id`),
--    KEY `post_auth_fk` (`auth_id`),
--    CONSTRAINT `post_auth_fk` FOREIGN KEY (`auth_id`) REFERENCES `author` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
--  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

primary key : unique + not null
였직 ν•˜λ‚˜μ˜ ν•„λ“œμ—λ§Œ μ„€μ • κ°€λŠ₯

foreign key : μ™Έλž˜ν‚€λΌκ³  λΆ€λ₯΄λ©° ν•œ ν…Œμ΄λΈ”μ„ λ‹€λ₯Έ ν…Œμ΄λΈ”κ³Ό μ—°κ²°μ‹œμΌœμ£ΌλŠ” μ—­ν• 
λ‹€λ₯Έ ν…Œμ΄λΈ”μ˜ ν•„λ“œλŠ” λ°˜λ“œμ‹œ uniqueλ‚˜ primary key이어야 함

cascade : μ°Έμ‘°λ˜λŠ” ν…Œμ΄λΈ”μ—μ„œ 데이터λ₯Ό μ‚­μ œ/μˆ˜μ • μ‹œ 같이 μ‚­μ œ/μˆ˜μ •
setnull : μ°Έμ‘°λ˜λŠ” ν…Œμ΄λΈ”μ—μ„œ 데이터λ₯Ό μ‚­μ œ/μˆ˜μ • μ‹œ null둜 λ³€κ²½
restrict : foreign key둜 μž‘μ€ ν…Œμ΄λΈ”μ˜ 데이터가 남아 있으면, foreign key λŒ€μƒ 데이터 μ‚­μ œ/μˆ˜μ • λΆˆκ°€
λ™μž‘ μ˜΅μ…˜μ„ μ„€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ κΈ°λ³Έ restrict 이닀.


default

alter table author add create_at datetime default current_timestamp;

μ‹œκ°„ μ„€μ • μ‹œ κ°€μž₯ 많이 μ‚¬μš©


νλ¦„μ œμ–΄

case value
when [compart_value1] then [result1]
when [compart_value1] then [result2]
else [result3]
end

-- case μ˜ˆμ‹œ
select id title, content, 
(case auth_id
when 1 then 'first_author'
when 2 then 'second_author'
else 'others'
end) as author_type
from posts

-- if μ˜ˆμ‹œ
if(a, b, c)
-- a = 쑰건
-- b = 참일 경우
-- c = 거짓일 경우
select if(0<1, 'yes', 'no');

-- ifnull μ˜ˆμ‹œ
ifnull (a, b)
select id title, content, ifnull(auth_id, 'anonymous') from posts;
-- λ§Œμ•½ a의 값이 null이 μ•„λ‹ˆλΌλ©΄ aλ₯Ό λ°˜ν™˜ν•˜κ³  null이면 bλ₯Ό λ°˜ν™˜

νŠΈλžœμž­μ…˜

-- νŠΈλžœμž­μ…˜ μ‹€μŠ΅ : auto_commit ν•΄μ œ ν›„ μ‹€μŠ΅
insert into author(name, email) values('test', 'test@naver,com');
insert into posts(id, title, content, auth_id) values('20','hello', 'hello is', 1);
select * from author;
select * from posts;

-- commit;
-- rollback;
select * from author;

-- 정리
insert into author(name, email) values('test', 'test@naver,com');
commit;
insert into author(name, email) values('test', 'test@naver,com');
insert into posts(id, title, content, auth_id) values('20','hello', 'hello is', 1);
rollback;

νŠΈλžœμž­μ…˜ : 논리적인 μž‘μ—… λ‹¨μœ„λ‘œ μ²˜λ¦¬λ˜μ–΄μ•Ό ν•˜λŠ” ν•˜λ‚˜ μ΄μƒμ˜ SQL 문의 집합
commit : νŠΈλžœμž­μ…˜μ˜ λͺ¨λ“  변경사항을 λ°μ΄ν„°λ² μ΄μŠ€μ— 영ꡬ적으둜 μ €μž₯
rollback : νŠΈλžœμž­μ…˜μ˜ 변경사항을 λͺ¨λ‘ μ·¨μ†Œν•˜κ³ , λ°μ΄ν„°λ² μ΄μŠ€λ₯Ό νŠΈλžœμž­μ…˜ μ‹œμž‘ μ΄μ „μ˜ μƒνƒœλ‘œ λ˜λŒλ¦¬λŠ” 것


DB λ™μ‹œμ„± 이슈

  • dirty read
    μˆ˜μ • 쀑인 데이터λ₯Ό 읽을 수 μžˆλŠ” 문제
    ν•΄κ²°μ±… : Read Committed 격리성
  • Non-Repeatable Read
    λ™μΌν•œ 쑰회 쿼리λ₯Ό 두 번 이상 μ‹€ν–‰ν•  λ•Œ, κ·Έ 쀑간에 λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ—μ„œ 데이터λ₯Ό μˆ˜μ •ν•˜μ—¬ ν•œ νŠΈλžœμž­μ…˜μ˜ κ²°κ³Όκ°€ λ‹€λ₯΄κ²Œ λ‚˜νƒ€λ‚˜λŠ” 문제
    ν•΄κ²°μ±… : Repeatable Read 격리성
  • Phantom Read
    ν•œ νŠΈλžœμž­μ…˜μ΄ 같은 쑰회쿼리λ₯Ό μ—¬λŸ¬ 번 μ‹€ν–‰ν–ˆμ„ λ•Œ, κ·Έ 쀑간에 λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ—μ„œ μƒˆλ‘œμš΄ 데이터λ₯Ό μΆ”κ°€/μ‚­μ œν•˜μ—¬ λ‹€λ₯΄κ²Œ λ‚˜νƒ€λ‚˜λŠ” 문제
    ν•΄κ²°μ±… : DBλ§ˆλ‹€ 차이가 μžˆμ–΄ ν•΄κ²°ν•˜κΈ° μœ„ν•΄ Serializable κ²©λ¦¬μˆ˜μ€€μ΄ ν•„μš”ν• μˆ˜λ„ 있음. ν•΄κ²° μ „λž΅μ„ 생각해볼 것.

DB κ²©λ¦¬μˆ˜μ€€

  • Read Uncommitted
    데이터가 λ³€κ²½λ˜μ—ˆλ‹€λ©΄, μ»€λ°‹λ˜μ§€ μ•Šμ•˜λ‹€ ν•˜λ”λΌλ„ 읽을 수 μžˆλ„λ‘ ν•˜λŠ” κ²©λ¦¬μˆ˜μ€€
  • Read Committed
    λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ΄ μ»€λ°‹λœ λ°μ΄ν„°λ§Œ 읽을 수 μžˆλŠ” κ²©λ¦¬μˆ˜μ€€.
  • Repeatable Read
    ν•œ 번 읽은 λ°μ΄ν„°λŠ” 같은 νŠΈλžœμž­μ…˜ λ‚΄μ—μ„œλŠ” 항상 같은 값을 갖도둝 ν•˜λŠ” κ²©λ¦¬μˆ˜μ€€
  • Serializable
    λ°μ΄ν„°λ² μ΄μŠ€ μ°¨μ›μ—μ„œ λ™μ‹œμ— νŠΉμ • 데이터에 μ ‘κ·Όν•˜λŠ” 것을 차단

github λ°μ΄ν„°λ² μ΄μŠ€ μ‹€μŠ΅ λ‚΄μš©

profile
503 Service Unavailable Error

0개의 λŒ“κΈ€