concatselect concat('안녕하세요!','MySQL') as concat;

select concat(address1, ' ' , address2, ' ', address3) as address from member where userid = 'orange';

left, rightselect left('ABCDEFGHIJKLMNOP', 5);

select userid, left(userid, 2) as leftfunc from member;

substringsubstring(문자열, 시작위치) : 시작위치부터 끝까지
substring(문자열, 시작위치, 길이) : 시작위치부터 길이만큼
select substring('ABCDEFGHIJKLMNOP', 5) as sub;
select substring('ABCDEFGHIJKLMNOP', 5, 3) as sub;


char_lengthselect email, char_length(email) as len from member;

lpad, rpadlpad(문자열, 총길이, 채울문자열)
select point, lpad(point, 5, '0') as pad from member;

ltrim, rtrim trimselect ltrim(' ABCDE ') as ltrim;
select ltrim(' ABCDE ') as trim;

replacereplace(문자열, 대상, 바꿀 문자열)
select replace('ABCDEGG', 'CD', ',') as repl;

🖥️예제




현재 00004 플립5가 중복된다.
select code, name, regdate, price from product
union
select code, name, regdate, price from product_new;

플립5가 중복되지만 regdate 값이 다르기 때문에 중복제거가 되지 않는다.(플립5가 2개)
select code, name, price from product
union
select code, name, price from product_new;

select문에 regdate를 빼면 모든 컬럼의 값이 같으므로 중복값이 제거된다.
만약 중복값을 제거하고싶지 않을 경우
union all을 사용하면 된다.
select code, name, price from product
union all
select code, name, price from product_new;

🖥️ 서브쿼리 예제
상품 코드가 '00001'의 가격보다 크거나 같은 가격을 가지고 있는 상품의 모든 정보를 출력
select * from product where price >= (select price from product where code = '00001');

코드번호, 상품명, 가격, 제일 비싼 가격을 출력하고싶다
select code, name, price, (select max(price) from product) as max_price from product;

필드의 identity한 숫자를 자동으로 부여


select userid, name, gender from member
where userid in (select userid from orders group by userid having count(userid) >= 2);

처음에는 문제에서 어떤 부분을 서브쿼리로 만들어야하는지 생각한 후 서브쿼리를 먼저 만들고 그 다음에 전체 쿼리를 작성하면 더 편하게 쿼리를 작성할 수 있다.
select left(m.userid, 2), count(o.userid) as cnt from member as m join orders as o
on m.userid = o.userid
group by m.userid having cnt >= 2;

MySQL는 관계형 데이터베이스 관리 시스템(RDBMS)으로, 데이터를 생성(Create), 읽기(Read), 수정(Update), 삭제(Delete)하는 기본적인 데이터 조작 작업을 수행할 수 있다. 이러한 데이터 조작 작업은 CRUD 작업이라고도 불린다.
MySQL 8.0 COmmand Line Client에서 root로 로그인
접속 가능한 사용자 추가하기
create user '사용자명'@'localhost' identified by '비밀번호';
사용자 목록 조회
use mysql;
select user, host from user;
할당 권한 상세 옵션
create, drop, alter: 테이블에 대한 생성, 삭제, 변경 권한
select, insert, update, delete: 테이블의 데이터를 조회, 삽입, 변경, 삭젱 대한 권한
all: 모든 권한
usage: 권한을 부여하지 않고 계정만 생성
grant select on 데이터베이스명.테이블명 to '사용자'@'localhost';
grant all on *,* to '사용자'@'localhost';
flush privileges;
grant select on 데이터베이스명.테이블명 to '사용자'@'%'; -- 모든 IP에서 접근이 가능
🖥️ 예제
create database apple;create table apple.member(select * from kdt.member);grant select on apple.member to 'apple'@'localhost';drop user '계정명'@'localhost';
revoke 권한명 privileges on 데이터베이스명.테이블명 from '계정명'@'localhost';
show grants for '계정명'@'localhost';
🖥️ 예제
1. apple 데이터베이스에모든 권한을 가진 사용자 'orange'를 생성
create user 'orange'@'localhost' identified by '2222';
grant all on apple.* to 'orange'@'localhost';
show grants for 'orange'@'localhost';
revoke all privileges on apple.* from 'orange'@'localhost';
from 부분에서 오류가 나는데 해당 부분은 무시해도 된다.
drop user 'orange'@'localhost';
use mysql;
select user, host from user;
create view 뷰이름 as 쿼리.....
🖥️ 예제
member의 userid, name, hp와 profile의 mbtl를 출력하는 뷰(vw_member_profile)를 만들어보자


create view vw_member_profile as select m.userid, m.name, m.hp, p.mbti from member as m join profile as p
on m.userid = p.userid;
select * from vw_member_profile;
모든 작업들을 정상 처리하겠따고 확정하는 명령어. 해당 처리 과정을 DB에 영구적으로 저장
작업 중 문제가 발생되어 트랜젝션의 처리 과정에서 발생한 변경사항을 모두 취소하는 명령어
srart transaction
블록안의 명령어들은 하나의 명령어 처럼 처리됨
...
성공하던지, 실패하던지 둘 중 하나의 결과가 발생
문제가 발생하면 rollback;
정상적인 처리가 완료되면 commit;
show variables like '%commit%';
🖥️ 예제
start transaction;
insert into word values('car', '자동차', 1);
select * from word;
rollback;
select * from word;
insert into word values('car', '자동차', 1);
commit;
select * from word;
insert문으로 롤백이 가능하다.
DDL문(create, drop, alter, rename, truncate)에 e대해 예외를 적용: rollback 대상이 아니다.
truncate table 테이블명;
🖥️ truncate 및 트렌젝션 예제
select * from product_new;
start transaction;
delete from product_new;
rollback;
select * from product_new;
start transaction;
truncate table product_new;
select * from product_new;
rollback;
select * from product_new;
# autocommit 활성화
set autocommit = 1;
delete문은 롤백이 가능하지만 truncate는 롤백이 불가능하다.
오타가 많네요?