CS/Cloud Computing

[Cloud Computing] 5. DB(MySQL) 내 컴퓨터에 설치하기

코코맹 2024. 7. 17. 15:50

  DB를 사용하는 방법

  • 내컴퓨터에 설치(MySQL02 실습) → On Premise 온프레미스
  • 클라우드의 VM(가상머신) 인스턴스에 설치 (MySQL03) → IaaS
  • SQL 인스턴스를 사용 (MySQL01) → PaaS : 운영체제를 몰라도 사용가능
  • 도커를 사용
  • 웹브라우저에서 웹서버로 요청을 보낼때 : https로 전송
  • 웹서버에서 DB로 요청을 보낼 때 : SQL로 전송

 

  Window에서 MariaDB 시작하기

cd \
cd program files
cd maria<tab>
cd bin
mysql -u root -p

 

데이터 베이스 만들기

create database petshop;

 

데이터베이스 사용 선언

use petshop;

 

pet 테이블 만들기

 

CREATE TABLE pet(
name varchar(20) not null primary key,
owner varchar(20),
species varchar(20),
sex varchar(1),
birth DATE,
death DATE);

 

테이블에 내용을 넣기

insert into 테이블명 values(입력데이터 전체); 

insert into pet values('Puffball','Diane','hamster','f','2019-03-30',NULL);
insert into pet values('Fluffy','Harold','cat','f','2013-02-04',NULL);
insert into pet values('Claws','Gwen','cat','m','2014-03-17',NULL);
insert into pet values('Buffy','Harold','dog','f','2019-05-13',NULL);
insert into pet values('Fang','Benny','dog','m','2010-08-27',NULL);
insert into pet values('Bowser','Joey','dog','m','2009-08-31','2015-07-29');
insert into pet values('Chirpy','Gwen','bird','f','2018-09-11',NULL);
insert into pet values('Whistler','Gwen','bird',Null,'2017-12-09',NULL);
insert into pet values('Slim','Benny','snake','m','2016-04-29',NULL);

 

잘못 입력한 데이터 지우기

delete from 테이블명 where name=’이름’;

delete from pet where name=’Fang’;

 

데이터 수정하기

update 테이블명 set 컬럼명=’바꿀값’ where name=’이름’;

update pet set death='2022-01-26' where name='Slim';

 

데이터 조회하기

select * from pet;
#보고싶은 내용은 select 뒤에 컬럼명을 씁니다.
#from 뒤에는 테이블 명을 씁니다.

select name,birth from pet;
#이름과 생년월일만 조회

select name,birth from pet order by birth asc;
#생년월일에 따라 오름차순(asc)으로 정렬 -> 나이가 많은순

select name,birth from pet order by birth desc;
#생년월일에 따라 내림차순(desc)으로 정렬 -> 나이가 어린순

 

데이터를 수정하고 중복을 제거하기 : distinct

 

update pet set owner='Diane' where name='Bowser';
# Bowser의 주인이 Joey가 아니라 Diane이었으므로 수정

select * from pet;
# 전체 테이블 내용 확인

select distinct owner from pet;
# 주인의 이름을 중복제거 하고 보여줌

 

AND 와 OR 사용하기

select * from pet where birth > '2018-01-01';
# 2018년 이후에 태어난 동물 검색

select * from pet where species='dog' and sex='f';
# AND:(species='dog')이면서 (sex='f')인 동물 검색
# 로그인할 때도 ID와 PW 확인

//Ex) 로그인 과정
select * from memeber where id='asd' and pw='asd';
# 둘중 하나라도 틀리면 로그인 안됨 맞다면? 쿠키전달

# OR : (species='snake')이거나 (species='bird')인 동물 검색
# 웹브라우저에서 웹서버로 요청을 보낼때 : https로 전송
# 웹서버에서 DB로 요청을 보낼 때 : SQL로 전송

 

정규표현식(PCRE)을 활용한 검색

PCRE(Perl Compatible Regular Expressions, 정규표현식) : 패턴 검색

select * from pet where name regexp "ffy";
# 정규표현식은 ffy가 들어가는 모든 문자열을 검색

select * from pet where name like "%ffy";
# like로 검색할 때에는 %를 앞 또는 뒤, 앞뒤에 붙여서 사용

 

내림차순, 오름차순 정렬하기

select owner, count(*) from pet group by owner order by owner desc;

select species,count(*) from pet group by species order by species asc;

select species,count(*) from pet group by species order by species desc;
# 종류별로 정렬해서 많은 종류순으로 내림차순 하기

select distinct owner AS customer from pet;
# owner 컬럼명을 customer(별칭)로 바꾸어서 보여주기

select distinct owner AS customer from pet order by customer asc;
# owner의 이름에 따라 알파벳순으로 정렬(오름차순)

 

묶기와 조건 적용하기

group by와 having 조건문
성별을 묶어서 개수 세기

select sex,count(*) from pet group by sex having count(*);

select name,sex from pet group by sex having 1=1;
# having 1=1은 모두 참이 되는 조건, name은 첫줄로만 출력

select sex,count(*) from pet group by sex having 1=1;
# 성별 개수로 표시

select sex,count(*) from pet group by sex;
# having 1=1은 사족이므로 제거 가능