π 2023λ 11μ 20μΌ
μ€λλ DMLκ΄λ ¨ν΄μ λ λ§μ μ€μ΅μ μ§ννμΌλ©°, μ μ½ μ‘°κ±΄κ³Ό νΈλμμ μ κ΄ν 곡λΆνλ€. [DB 3μΌμ°¨]
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;
alter table author modify column id int auto_increment;
νν λ§νλ λλ²λ§ μ μν΄ μ¬μ©νλ©° update κ° κ°λ₯νλ€ (μ€λ³΅μ μλ¨) κ°μ₯ λ§μ§λ§μ μ λ ₯ν κ°μ κΈ°μ€μΌλ‘ μ«μλ₯Ό μ¦κ°μν¨λ€.
-- μ μ½ μ‘°κ±΄ κ²μ¬ 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 μ΄λ€.
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 : νΈλμμ
μ λ³κ²½μ¬νμ λͺ¨λ μ·¨μνκ³ , λ°μ΄ν°λ² μ΄μ€λ₯Ό νΈλμμ
μμ μ΄μ μ μνλ‘ λλ리λ κ²
- dirty read
μμ μ€μΈ λ°μ΄ν°λ₯Ό μ½μ μ μλ λ¬Έμ
ν΄κ²°μ± : Read Committed 격리μ±
- Non-Repeatable Read
λμΌν μ‘°ν 쿼리λ₯Ό λ λ² μ΄μ μ€νν λ, κ·Έ μ€κ°μ λ€λ₯Έ νΈλμμ μμ λ°μ΄ν°λ₯Ό μμ νμ¬ ν νΈλμμ μ κ²°κ³Όκ° λ€λ₯΄κ² λνλλ λ¬Έμ
ν΄κ²°μ± : Repeatable Read 격리μ±
- Phantom Read
ν νΈλμμ μ΄ κ°μ μ‘°ν쿼리λ₯Ό μ¬λ¬ λ² μ€ννμ λ, κ·Έ μ€κ°μ λ€λ₯Έ νΈλμμ μμ μλ‘μ΄ λ°μ΄ν°λ₯Ό μΆκ°/μμ νμ¬ λ€λ₯΄κ² λνλλ λ¬Έμ
ν΄κ²°μ± : DBλ§λ€ μ°¨μ΄κ° μμ΄ ν΄κ²°νκΈ° μν΄ Serializable 격리μμ€μ΄ νμν μλ μμ. ν΄κ²° μ λ΅μ μκ°ν΄λ³Ό κ².
- Read Uncommitted
λ°μ΄ν°κ° λ³κ²½λμλ€λ©΄, 컀λ°λμ§ μμλ€ νλλΌλ μ½μ μ μλλ‘ νλ 격리μμ€
- Read Committed
λ€λ₯Έ νΈλμμ μ΄ μ»€λ°λ λ°μ΄ν°λ§ μ½μ μ μλ 격리μμ€.
- Repeatable Read
ν λ² μ½μ λ°μ΄ν°λ κ°μ νΈλμμ λ΄μμλ νμ κ°μ κ°μ κ°λλ‘ νλ 격리μμ€
- Serializable
λ°μ΄ν°λ² μ΄μ€ μ°¨μμμ λμμ νΉμ λ°μ΄ν°μ μ κ·Όνλ κ²μ μ°¨λ¨