Oracle

210831_sequence

요옫 2021. 8. 31. 10:21

--시퀀스 기본으로 생성, 1부터 1씩 증가하는 시퀀스 생성됨
create sequence seq1;

--전체 시퀀스 확인
select * from seq;

--다음 시퀀스 값을 발생해서 콘솔에 출력..nextval(dual은 콘솔을 의미)
select seq1.nextval from dual;

--현재 마지막 발생한 시퀀스값..currval
select seq1.currval from dual;

--seq1 시퀀스 삭제
drop sequence seq1;

--10부터 5씩 증가하는 시퀀스 생성..cache 값은 없애기(no cache)
create sequence seq1 start with 10 increment by 5 nocache;

--다음 시퀀스 발생
select seq1.nextval from dual;

--시퀀스 수정,startwith는 수정불가,maxvalue 지정후 그값까지 나오면 다시 처음부터 오도록 하고자 할때
alter sequence seq1 increment by 10 maxvalue 100 cycle;


-----
Q1.시작값5, 증가값2, 끝값30, 노캐시, 사이클
create sequence seq1 start with 5 increment by 2 maxvalue 30 nocache cycle;
Q2.시작값1, 증가값2, 노캐시
create sequence seq2 increment by 2 nocache;
Q3.시작값1, 증가값1, 노캐
create sequence seq3 nocache;

--출력
select seq1.nextval,seq2.nextval,seq3.nextval from dual;

--전체삭제 
drop sequence seq1;
drop sequence seq2;
drop sequence seq3;

 

 

---------------------------------

 

 

(예제)

--시퀀스 생성
create sequence seq1 nocache;

--테이블 생성
create table person(num number(5) primary key, name varchar2(20),
job varchar2(30), gender varchar2(10), age number(5), hp varchar2(20), birth date);

--birth를 ipsaday로 변경
alter table person rename column birth to ipsaday;

--구조확인
desc person;

--조회연습위한 insert..10개 이상
--데이터는 각자.. 직업은 엔지니어,교사,프로그래머,개그맨,세무사,변호사
--다 하고 마지막엔 commit 해주기
insert into person values(seq1.nextval,'박명수','개그맨','남자','56','010-1111-1111','1997-04-05');
insert into person values(seq1.nextval,'유재석','엔지니어','남자','50','010-2222-2222','1968-07-24');
insert into person values(seq1.nextval,'정형돈','교사','남자','47','010-3333-3333','1971-12-31');
insert into person values(seq1.nextval,'강호동','프로그래머','남자','53','010-4444-4444','1958-06-01');
insert into person values(seq1.nextval,'이효리','개그맨','여자','30','010-5555-5555','1980-02-09');
insert into person values(seq1.nextval,'김혜수','세무사','여자','29','010-6666-6666','1997-04-18');
insert into person values(seq1.nextval,'전지현','변호사','여자','37','010-7777-7777','1989-02-15');
insert into person values(seq1.nextval,'이나희','엔지니어','여자','26','010-8888-8888','1993-08-02');
insert into person values(seq1.nextval,'김민정','교사','여자','28','010-9999-9999','1993-11-05');
insert into person values(seq1.nextval,'박승연','프로그래머','여자','42','010-1212-1212','1987-03-10');
insert into person values(seq1.nextval,'황수희','세무사','여자','23','010-2323-2323','1998-10-27');

--저장
commit;

--전체 데이터 확인
select * from person;

--이름의 오름차순
select * from person order by name asc;

--나이의 역순으로 출력
select * from person order by age desc;

--컬럼명 대신 컬럼번호 써도 된다 (첫번째 열번호1)
select * from person order by 5 asc;  --나이순으로 정렬

--gender의 오름차순을 주고 이름으로 한번더 오름차순
select * from person order by gender asc, name asc;
select * from person order by gender, name ;  --asc는 생략가능하므로 결과는 같음

--직업만 출력
select job from person;

--직업 출력..중복제거
select distinct job from person;  --distinct는 select 뒤에 위치해야 함


--Q.이름이 강호동인 사람 출력
select * from person where name='강호동';

--Q.성이 김씨인 사람 출력
select * from person where name like '김%';

--Q.이름의 두번째 글자가 '명'인 사람 출력
select * from person where name like '_명%';

--Q.이름이 '희'로 끝나는 사람 출력
select * from person where name like '__희';

--Q.핸드폰이 010인 사람
select * from person where hp like '010%';

--Q.핸드폰 마지막 번호가 6666인 사람
select * from person where hp like '_%6666';

--Q.나이가 30~40인 사람 출력(>=)
select * from person where age>=30 and age<=40;

--Q.나이가 30~40인 사람 출력(between)
select * from person where age between 30 and 40;

--Q.직업이 교사이거나 개그맨인 사람(or연산자)
select * from person where job='교사' or job='개그맨';

--Q.직업이 교사이거나 개그맨인 사람 (in연산자)
select * from person where job in '교사' or job in '개그맨';

--Q.교사,개그맨을 제외한 직업 출력 (not in)
select * from person where job not in '교사' or job not in '개그맨';

--Q.성이 김씨이거나 이씨인 사람 조회
select * from person where name like '김%' or name like '이%';

-Q.여자이면서 나이가 30세 이상인 사람 조회
select * from person where gender='여자' and age>=30;

'Oracle' 카테고리의 다른 글

210831_서브쿼리  (0) 2021.08.31
210831_to char  (0) 2021.08.31
210831_그룹함수  (0) 2021.08.31
210830_  (0) 2021.08.30
error  (0) 2021.08.25