728x90
1. DML
a. C : insert / into 데이터 추가
⦁ 기본은 백커터(`)를 넣어 사용하며 데이터베이스를 use 상태면 생략 가능
1. insert
/*================<< insert >>================*/
select * from student_mst;
insert into student_mst
(student_id, student_name, mentor_id)
values
(5, '이강용', 10),
(6, '김준경', 10),
(7, '이현수', 10),
(8, '정의현', 10);
select * from university_mst;
insert into university_mst
(university_id, university_name)
values
(4, '카이스트'),
(5, '고려대'),
(6, '연세대'),
(7, '포항공대'),
(8, '성균관대'),
(9, '한양대'),
(10, '경희대');
2. select insert
insert into author_mst
(author_name)
select distinct
저작자
from
library_mst
order by
저작자;
select
*
from
author_mst;
insert into publisher_mst
(publisher_name)
select distinct
출판사
from
library_mst
order by
출판사;
select
*
from
publisher_mst;
b. R : select / from 데이터 조회
⦁ 쿼리 실행 순서는 from 1번, where 2번, select 3번 순서로 실행
1. select
/*================<< select >>================*/
select * from student_mst;
/* 전체 컬럼 조회 */
select
*
from
student_mst;
/* 지정 컬럼 조회 */
select
student_id,
student_name,
mentor_id
from
student_mst;
/* 임시 컬럼 추가 */
select
1 as num,
'김준일' as name;
select
student_id,
student_name,
'김준일' as instructor_name
from
student_mst;
/* 컬럼명을 임시로 바꾸는 방법 as(alias) 알리아스 */
/* 생략 가능하나 보통 컬럼에는 생략하지 않고 테이블은 생략함 */
/* 자바와의 변수명 매칭을 위해 사용 */
select
sm.student_id as studentId
from
student_mst sm;
/* 조회조건 where */
select
*
from
student_mst
where
mentor_id = 1;
/* 서브쿼리 */
select
*
from
student_mst
where
mentor_id = (select
mentor_id
from
mentor_mst
where
mentor_name = '문자영');
select
student_id,
student_name,
mentor_id,
(select
mentor_name
from
mentor_mst
where
mentor_id = student_mst.mentor_id) as mentor_name
from
student_mst;
/* 그룹으로 묶어서 조회하기 */
/* 연산 시 사용 */
select
count(mentor_id),
min(student_id),
max(student_id),
avg(student_id),
sum(student_id),
mentor_id
from
student_mst
group by
mentor_id;
/* 중복 제거 */
select distinct
mentor_id
from
student_mst;
/* 그룹으로 조회한 결과에 조건주는 방법 */
select
count(mentor_id) as mentor_count,
min(student_id),
max(student_id),
avg(student_id),
sum(student_id),
mentor_id
from
student_mst
group by
mentor_id
/* group으로 묶인 것에 조건을 줄 때 사용*/
having
mentor_count = 5;
/* 정렬 */
select
*
from
student_mst
/* 기본은 오름차순 정렬, desc를 사용시 내림차순 정렬 */
order by
mentor_id,
student_id desc;
/* 전체 조합 실습 */
select
count(*) as student_count,
mentor_id
from
student_mst
where
student_id > 2
group by
mentor_id
having
student_count = 1
order by
mentor_id desc;
c. U : update / set 데이터 수정
1. update
/*================<< update >>================*/
select * from student_mst;
update student_mst
set
student_name = '김재영',
mentor_id = 15
where
student_id = 5;
/* 멘토 아이디가 10인 학생들의 멘토 아이디를 1로 바꿔라 */
update student_mst
set
mentor_id = 1
where
mentor_id = 10;
d. D : delete / from 데이터 삭제
1. delete
/*================<< delete >>================*/
/*학생id 4번, 이름 손지호를 지워라*/
delete
from
student_mst
where
student_id = 4
and student_name = '손지호';
select * from student_mst;
select * from university_mst;
/* 대학id의 9를 지워라*/
delete
from
university_mst
where
university_id = 9;
/*대학id 5보다 큰값을 지워라*/
delete
from
university_mst
where
university_id > 5;
/*대학id 1,2,3,4,5값을 지워라*/
delete
from
university_mst
where
university_id in(1,2,3,4,5);