DATABASE/MySQL Workbench

MySQL Workbench : 테이블 생성 및 데이터 CRUD (insert, select, update, delete) 키워드 정리

신강희 2024. 5. 14. 11:38
728x90

< MySQL 테이블 생성 및 데이터 CRUD (insert, select, update, delete) 키워드 정리 >

# 우선 실습을 위해 새로운 db를 생성하여 shirts라는 테이블 생성

 

# 새로운 db 사용을 하고 전체 데이터를 입력해주자 하단 명령어 사용

-- 데이터 1000개짜리를 실행시킬 예정이라 사전에 적힌 파일을 가지고, sql 파일로 열어서 실행시킴
use shirts_db; 

into shirts (article, color, shirt_size, last_worn) values ('polo shirt', 'Khaki', 'S', 96);
insert into shirts (article, color, shirt_size, last_worn) values ('tank top', 'Maroon', 'S', 96);
insert into shirts (article, color, shirt_size, last_worn) values ('t-shirt', 'Yellow', 'S', 178);
insert into shirts (article, color, shirt_size, last_worn) values ('polo shirt', 'Blue', 'M', 157);
insert into shirts (article, color, shirt_size, last_worn) values ('t-shirt', 'Yellow', 'S', 86);
insert into shirts (article, color, shirt_size, last_worn) values ('tank top', 'Blue', 'M', 96);
insert into shirts (article, color, shirt_size, last_worn) values ('tank top', 'Aquamarine', 'S', 76);
insert into shirts (article, color, shirt_size, last_worn) values ('tank top', 'Aquamarine', 'M', 189);
insert into shirts (article, color, shirt_size, last_worn) values ('polo shirt', 'Pink', 'S', 162);
insert into shirts (article, color, shirt_size, last_worn) values ('polo shirt', 'Purple', 'S', 154);

 

# 1000개 짜리 파일을 MySQL Workbench에 끌고와서 연다음에 실행시켰다.

# 이렇게 코드문이 많을경우 실행을 시작할곳에 마우스 커서를 두고 상단에 아이콘을 클릭하면 전체 데이터가 한번에 실행됨

 

-- 전체 데이터 개수 확인
select count(*)
from shirts;

 

-- 1) article 과 color 컬럼만 가져오기
select article, color
from shirts;

 

-- 2) M 사이즈 셔초에서, 셔츠 id만 빼고 전체 컬럼 가져오기.
select article, color, shirt_size, last_worn
from shirts
where shirt_size = 'M';

 

-- 3) polo 셔츠의 사이즈를 L로 변경.
update shirts
set shirt_size = 'L'
where article = 'polo shirt';

-- 변경된 컬럼만 불러와서 확인
select *
from shirts
where article = 'polo shirt';

 

-- 4) last worn 이 15인 데이터를 0으로 바꾸세요.
-- 15인 데이터만 확인해보고
select *
from shirts
where last_worn = 15;

-- 그 값을 0으로 변경
update shirts
set last_worn = 0
where last_worn = 15;

 

-- 5) blut 셔츠의, 사이즈는 XS로, 컬러는 off white로 변경.
-- 컬러가 블루인 것들만 확인해보고,
select *
from shirts
where color = 'blue';

-- 그값을 변경해준다.
update shirts
set shirt_size = 'XS' , color = 'Off white'
where color = 'Blue';

 

-- 6) 입은지 200일이 지난 셔츠는 삭제
-- 200일이 지난 데이터 확인
select *
from shirts
where last_worn >= 200;

-- 확인된 데이터들은 삭제
delete from shirts
where last_worn >= 200;

 

-- 7) 유행이 지난, tank tops는 삭제
-- 확인
select *
from shirts
where article = 'tank top';

-- 삭제
delete from shirts
where article = 'tank top';

 

# 데이터 삭제delete / 테이블 삭제는 drop!

-- 8) 셔츠 테이블의 모든 데이터를 삭제
delete from shirts;

-- 9) 셔츠 테이블을 삭제
drop table shirts;

 

### 한번더 복습!! 데이터 넣기insert / 선택select / 수정update / 삭제delete => CRUD

 

다음 게시글로 계속!

반응형