본문 바로가기

리눅스(Linux)/DB

[리눅스] MariaDB 기본 명령어 - insert, select, update, delete / create, drop (한글깨짐 방지 설정)

반응형

 

# mariadb 다운로드

# yum update / yum upgrade
yum install mariadb-server -y =>y 눌러서 설치
systemctl start mariadb  / systemctl enable --now mariadb
mysql    # 확인

 

# 한글 설정

### 한글이 깨지지 않도록 설정

cd /etc/my.cnf.d
1. vi client.cnf
##############################
[client]
default-character-set=utf8
##############################저장

2. vi mysql-client.cnf
##############################
[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8
##############################저장

3. vi mariadb-server.cnf
##############################
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
init_connect = set names utf8
##############################저장

systemctl restart mariadb

mysql => status 
Server characterset:	utf8
Db     characterset:	utf8
Client characterset:	utf8
Conn.  characterset:	utf8

 

# table 생성(create)

mysql
show databases;
create database shop;
show databases;
=> 삭제 drop database shop;
use shop;
show tables;

create table customer(
id varchar(10) not null primary key,
name varchar(20) null,
age int,
address varchar(20)
);

show tables;
desc customer;

## 만약, primary key생성을 까먹었다면?
# alter table 테이블명 add constraint primary key(컬럼명);
alter table customer add constraint primary key(id);

 

# 데이터(레코드) 생성(insert)

# customer 테이블
aa 홍길동 10 서울
bb 홍길순 20 춘천
cc 강감찬 30 합천
dd 유관순 40 홍천
ee null null 강남


# 데이터 입력 (레코드 구성)
# insert into 테이블명 values(값1, 값2, 값n);
insert into customer values('aa','홍길동',10,'서울');
insert into customer values('bb','홍길순',20,'춘천');
insert into customer values('cc','강감찬',30,'합천');
insert into customer values('dd','유관순',40,'홍천');
insert into customer values('ee','유관순2',50, null);

 

# 데이터 조회(select)

# 데이터 조회
#select * from 테이블명;
#select 필드명1,필드명2 from 테이블명;
select * from customer;
select id,name from customer

 

## 데이터 조회 - 특정 조건 조회

# 특정 조건 조회
#select * from 테이블명 where 조건;

-- 나이가 10살인 레코드
select * from customer where age=10;

-- id가 bb인 레코드
select * from customer where id='bb';

-- 나이가 20-40 사이의 레코드
# Between AND, OR / 부등호
select * from customer where age >=20 and age <= 40;   # age >== and age <= 40; age를 두번 써줘야한다!
select * from customer where age between 20 and 40;

-- 나이가 30살 이상인 레코드
select * from customer where age >= 30;

-- 이름이 '홍'으로 시작하는 레코드
#like ~와 비슷한
#like '홍%'   /  not like '홍%' 
#like '%순'   /  like '%관%' 
select * from customer where name like '홍%';

-- 이름이 '홍'으로 시작하지 않는 레코드
select * from customer where name not like '홍%';

-- 이름이 '순'으로 끝나는 레코드
select * from customer where name like '%순';

-- 이름에 '관'이 들어간 레코드
select * from customer where name like '%관%';

-- 주소가 null 값인 레코드
#is null / is not null
select * from customer where address is null;

-- 주소가 null 값이 아닌 레코드
select * from customer where address is not null;

# 이메일
--이메일이 있는 레코드
select * from member where email is not null;
select * from member where email like '%@%';

--이메일이 없는 레코드
select * from member where email is null;

 

 

# 데이터 수정(update)

# 데이터 수정
update 테이블명 set 필드명1=바꿀값, 필드명2=바꿀값2 where 조건
id가 aa 인 사용자의 주소를 제주도 변경
update customer set address='제주도' where id='aa';

id각 bb인 사용자의 이름을 이순신변경 나이 100 변경
update customer set name='이순신', age=100 where id='bb';

##
id가 aa 인 이름 '고길동' 나이 20살 변경
update member set name='고길동', age=20 where id='aa';

id각 bb인 사용자의 이메일 bbb@aa.com 변경
update member set email='bbb@aa.com', age=100 where id='bb';

 

 

# 데이터 삭제(delete)

# 데이터 삭제
#delete from 테이블명 where 조건

id가 aa인 레코드 삭제
delete from customer where id='aa';

★중요!
# delete from 테이블명 
delete from customer;
> 테이블이 삭제되는 것이 아니라 테이블내 모든 '레코드'(데이터)가 삭제!

> 테이블 삭제는 drop 
drop table customer; 테이블 삭제

delete로 안에 '데이터값' 모두 삭제
drop으로 customer 'table'을 삭제

 

drop으로 shop 'db'를 삭제

 


 

오늘은 기본적인 DB의 명령어와 한글깨짐 방지 설정에 대해 알아보았습니다. 아주 기본적인 명령어들이기 때문에 잘 습득하여 관심이 생긴다면 추후 DB에 대해 더 깊게 공부하셔도 좋을 것 같습니다. 감사합니다. 

반응형