BootCamp 4day

GyeongNamยท2023๋…„ 11์›” 18์ผ
0

BootCamp

๋ชฉ๋ก ๋ณด๊ธฐ
4/49
post-thumbnail

๐Ÿ“… 2023๋…„ 11์›” 17์ผ

์˜ค๋Š˜์€ ์–ด์ œ ๋ฐฐ์šด DML๊ด€๋ จํ•ด์„œ ๋” ๋งŽ์€ ์‹ค์Šต์„ ์ง„ํ–‰ํ–ˆ๋‹ค. [DB 2์ผ์ฐจ]


4์ผ์ฐจ : DDL ๋ณต์Šต, DML ์ถ”๊ฐ€ ์‹ค์Šต

์˜ค๋Š˜๋„ ๋งŽ์€ ๋‚ด์šฉ์„ ์‹ค์Šตํ–ˆ์ง€๋งŒ ๊ทธ์ค‘ ๊ฐ€์žฅ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„๋งŒ ์„ค๋ช…ํ•˜๊ณ ์ž ํ•œ๋‹ค.

DDL ๋ณต์Šต

drop table posts;
drop table author; -- ๊ธฐ์กด ํ…Œ์ด๋ธ” ์‚ญ์ œ

drop database if exists board; -- ๊ธฐ์กด ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์‚ญ์ œ (if exists : ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ)

create database board; -- ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ƒ์„ฑ  

use board;	-- ํ˜„์žฌ ์‚ฌ์šฉ db ๋ณ€๊ฒฝ

create table author(	-- ํ…Œ์ด๋ธ” ์ƒ์„ฑ 1
    id int not null, 
    name varchar(255),
    email varchar(255),
    password varchar(255),
    test varchar(255),
    primary key(id)		-- ๊ธฐ๋ณธํ‚ค (์ตœ์†Œ์„ฑ, ์œ ์ผ์„ฑ)
);

create table posts(		-- ํ…Œ์ด๋ธ” ์ƒ์„ฑ 2
    id int not null, 
    title varchar(255),
    content varchar(255),
    auth_id int,
    primary key(id),
    foreign key(auth_id) references author(id)	-- ์™ธ๋ž˜ํ‚ค (๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค ์—ฐ๊ฒฐ)
);

create table table_blob(	-- ํ…Œ์ด๋ธ” ์ƒ์„ฑ 3
	id int,
    img longblob
);

-- (alter ๋ฌธ์€ DML์‹ค์Šต ๋•Œ ๊ฐ™์ด ์ง„ํ–‰)

DML ์‹ค์Šต

-- tinyint ํƒ€์ž…์˜ ์ปฌ๋Ÿผ ์ถ”๊ฐ€ (๋ฒ”์œ„ -128~127, unsigned์ผ ๊ฒฝ์šฐ 0~255)
alter table author add column age tinyint unsigned; 

describe author;	-- ํƒ€์ž… ๋ณ€๊ฒฝ ํ™•์ธ

insert into author(id, name, age) values(1, "jgn", 255);
insert into author(id, name, age) values(2, "jgn", 300);	-- ์—๋Ÿฌ(๋ฒ”์œ„์ดˆ๊ณผ)

-- decimal(M,D) : M=์ž๋ฆฌ์ˆ˜, D=์†Œ์ˆ˜๋ถ€, ๊ณ ์ •์†Œ์ˆ˜์ 
alter table posts add column price decimal(10,3); -- posts ํ…Œ์ด๋ธ”์— decimalํƒ€์ž…์˜ price ์ปฌ๋Ÿผ ์ถ”๊ฐ€

insert into table_blob(id,img) values(1,load_file('ํŒŒ์ผ๊ฒฝ๋กœ'));	--blob ์ด๋ฏธ์ง€ ์ถ”๊ฐ€

select hex(img) from table_blob where id = 1;	-- blod ์กฐํšŒ ์‹œ 

-- enum('๋ฐ์ดํ„ฐ ๊ฐ’1', '๋ฐ์ดํ„ฐ ๊ฐ’2',...) , not null default 'user'๋“ฑ์˜ ์˜ต์…˜๋„ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
alter table author modify column role enum('user','admin') not null default 'user';

-- datetime default current_timestamp : ํ˜„์žฌ์‹œ๊ฐ„์„ default๋กœ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ์‹ 
alter table posts add column created_tile1 datetime default current_timestamp;

select * from author where id not in(1,2,4);	-- not + in()
select * from posts where id between 2 and 4;	-- between (min) and (mex) 
select * from posts where id >=2 and id<=4;		-- (true) and (true) = (true)
select * from posts where id = 2 or id = 3 or id = 4;	-- (true) or (false) = (true)
select * from posts where !(id <2 or id>4);		-- not(!) + ro

-- like : ํŠน์ • ๋ฌธ์ž๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ %๋ฅผ ์‚ฌ์šฉ
select * from author where name like 'stirng%'; 	-- stirng์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š”
select * from author where name like '%stirng%'; 	-- stirng์„ ํฌํ•จํ•˜๋Š”
select * from author where name like '%stirng'; 	-- stirng์œผ๋กœ ๋๋‚˜๋Š”

-- regexp : ์ •๊ทœํ‘œํ˜„์‹ ํ† ๋Œ€๋กœ ํŒจํ„ด ์—ฐ์‚ฐ ์ˆ˜ํ–‰
select * from author where name regexp '[a-z]'; 	-- a๋ถ€ํ„ฐ z๊นŒ์ง€
select * from author where name regexp '[๊ฐ€-ํžฃ]'; 	-- '๊ฐ€' ๋ถ€ํ„ฐ 'ํžฃ' ๊นŒ์ง€

-- date_format : ๋‚ ์งœ/์‹œ๊ฐ„ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •๋œ ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
select date_format('2020-01-01 17:12:00', '%y-%m-%d');

-- distinct: ์ค‘๋ณต์ œ๊ฑฐํ•˜๊ณ  ์กฐํšŒ
select distinct name from author;

-- order by : ์ •๋ ฌ (๋จผ์ € ์“ด ์†์„ฑ์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์žˆ์œผ๋ฉฐ, asc/desc ์ƒ๋žต์‹œ asc ์ ์šฉ)
select * from author order by name desc;

-- limit : ๋ฐ˜ํ•œํ•  ํ–‰์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ์ง€์ •
select * from author order by id desc limit 2; 

--๋ณ„์นญ alias (as ์ƒ๋žต ๊ฐ€๋Šฅ)
select name AS '์ปฌ๋Ÿผ๋ณ„์นญ' from author;
select * from author AS 'ํ…Œ์ด๋ธ”์ด๋ฆ„ ๋ณ„์นญ';

์ด๋ ‡๊ฒŒ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ๊ฐ€์ง€๊ณ  SQL์„ ๊ฐ€์ง€๊ณ  programmers์—์„œ ๋ฌธ์ œํ’€์ด๋„ ํ–ˆ๋‹ค.

https://school.programmers.co.kr/learn/courses/30/lessons/59035
https://school.programmers.co.kr/learn/courses/30/lessons/59037
https://school.programmers.co.kr/learn/courses/30/lessons/59404
https://school.programmers.co.kr/learn/courses/30/lessons/59405

์ž์„ธํ•œ ์‹ค์Šต ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


github ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹ค์Šต ๋‚ด์šฉ

profile
503 Service Unavailable Error

0๊ฐœ์˜ ๋Œ“๊ธ€