괴발개발
[강의 정리] D-53 , DB(increment, limit, offset, 집계함수) 본문
[목차]
[1]. auto_increment
- 생성법
- 이미 생성된 테이블에 추가
- 기본값 수정 -> 나중에 변경해주고 싶을 때
[2]. limit , offset
- limit
- offset
- limit 하나로 해결
- 속도향샹!
- 데이터 미리 확보! - select * from ( 여기서 데이터 확보!) i limit 4,5
- join을 이용한 데이터 확보
[3]. 집계함수
- count
- max
- min
- avg
- sum
+ 퀴즈! 두 개를 한번에 볼 수 없을까!
[1]. auto_ increment
[정의]
<속성> - alter
자동으로 증가하는 속성!
[특징]
1. key 설정이 되어 있어야 한다. 이 속성 사용 되는 컬럼은 -why? 일단 넣으면 증가하고, 증가하면 중복은 없으니까
[사용]
ex) 1. 대표적으로 게시글이 있다.
1. 이미 생성된 table에
alter table [ 테이블 이름] modify [ 들어갈 컬럼] primary key auto_increment;
2. 초기값 설정
alter table [] auto_increment = 100;
1. 생성법
create table auto_inc(
no int(10) auto_increment primary key
, name varchar(10) not null
);
-- 내용 추가
insert into auto_inc(name)values('kim');
insert into auto_inc(name)values('lee');
insert into auto_inc(name)values('park');
2. 이미 생선된거에 만들기
-- 일단 auto_increment 안만들어줬음
create table test(
no int(10)
,name varchar(10) not null
);
insert into test(no, name) values(1,'a');
alter table test modify no int(10)primary key auto_increment;
** -- auto_increment 가 설정되는 컬럼은 key가 있어야 한다.
3. 기본값 수정
alter table test auto_increment = 100;
-- 확인 작업
insert into test(name) values('d');
insert into test(name) values('e');
select *from test;
[2] limit , offset => paging 해주는것!
[정의]
데이터가 너무 많을 때, 한번에 보여주고 자료를 잘라서 보여줌
[특징]
장점 : 많은 양의 데이터를 paging 하여 보여줄 때 유리하다.
1. limit : 특정 갯수를 불러옴
2. offset : 시작할 index를 지정
3. limit 하나로 해결! :
limit o,n : offset, n
- 1) limit : 특정 갯수를 불러온다.(무조건 처음 부터)
select *from employees limit 5;
-- 2) offset : 시작할 index를 지정
-- 0 번 인덱스 부터 5개 가져와
select *from employees limit 5 offset 0;
-- 5번 인덱스 부터 5개 가져와
select * from employees limit 5 offset 5;
-- 3) limt 하나로 해결 하기
-- limit o ,n : offset, n
-- 0 번 인덱스 부터 5개 가져와
select *from employees limit 0, 5;
-- 5번 인덱스 부터 5개 가져와
select * from employees limit 5, 5;
4. 속도 향샹!
1. 데이터 확보 해놓기!
select
i.emp_no
,i.first_name
,i.family_name
from (select * from employees order by emp_no) i limit 4,5; -- 데이터 확보가 이미 된거지!, 확보된거 꺼내오는거지
2. join를 이용한 데이터 확보! - 조금 복잡하지만 속도는 더 나음!
-- 2. join을 이용한 데이터 확보 방안 -- 조금 복잡한데 속도는 나옴.
select
e.emp_no
,e.first_name
,e.family_name
from
(select emp_no from employees limit 4,5) i join employees e on i.emp_no = e.emp_no ;
[3]. 집계함수
- count
- max
- min
- avg
- sum
-- 집계함수 : 여러행 또는 테이블 전체 행으로 부터 하나의 결과값을 반환하는 함수
-- group by 에서 데이터를 압축 할 때 자주 사용 하였다.
-- AVG(), COUNT(), MAX(), MIN(), SUM( ) 등이 있다.
-- 1) COUNT () : 검색된 행의 수를 반환
-- null 이 없는 컬럼을 대상으로 하는 것이 좋다.
select count(deptno) as cnt from dept; -- // null이 없는걸로!
-- 2) MAX() : 특정 행에 대한 최대값을 반환
select max(salary) from employees e;
-- 3) MIN () : 특정 행에 대한 최소값을 반환
select min(salary) from employees e;
-- 최대 급여를 받는 사람의 성, 이름 , 급여 추출
select * from employees;
select
family_name
,first_name
, salary
from employees where salary= (select max(salary) as salary from employees );
-- 최소 급여를 받는 사람의 성, 이름 , 급여 추출
select
family_name
,first_name
, salary
from employees where salary= (select min(salary) as salary from employees );
-- 4) AVG : 평균
select avg(salary) from employees;
-- 반올림 = round(값 , 반올림 자리수)
select round(avg(salary),1) from employees where depart_no = 'dev002' ;
-- 5) sum : 합계
select sum(salary) from employees;
+ 퀴즈! 두 개를 한번에 볼 수 없을까!
-- 최대 / 최소를 한번에 볼 수 없나?
select
family_name
,first_name
, salary
from employees
where salary= (select max(salary) as salary from employees )
union all
select
family_name
,first_name
, salary
from employees where salary= (select min(salary) as salary from employees );
select * from employees
where salary in (
(select max(salary) as salary from employees ) , (select min(salary) as salary from employees )
) ;
'BACK END > DB' 카테고리의 다른 글
[강의정리] AWS - 서버 연결 + 덤프 (0) | 2023.05.29 |
---|---|
db구조 (0) | 2023.04.30 |
[강의 정리] DB (set) (0) | 2023.03.22 |
[강의 정리] DB(index, view) (0) | 2023.03.22 |
emp, dept (0) | 2023.03.22 |
Comments