테이블 생성
create table tbl_member{
userid varchar2(50),
userpw varchar2(50),
username varchar2(100),
tel varchar2(50)
};
primary key 설정
alter table tbl_member add constraint
pk_member primary key (user_id);
데이터이동.
insert into tbl_member(uerid,userpw,usename,tel)
select * from user00.tbl_member
컬럼에 필드값 입력
insert INTO TBL_MEMBER (userid, userpw, username,tel)
VALUES ('u05','u05','윤종원','010-2965-4190');
group by
select deptno, count(ename) from emp group by deptno;
- emp 테이블의 deptno별로 ename의 개수를 카운트한다.
order by
select * from tbl_member order by userid;
- userid 로 정렬
- index full scan / table full scan
삭제
delete tbl_member where userid = 'u05';
시퀀스
- 유일(unique) 한 값을 생성해 주는 오라클 객체
- 시퀀스를 생성하면 기본키와 같이 순차적으로 증가하는 컬럼을 자동적으로 생성할 수 있다.
create sequence seq_bbs
start with 1 -- 시작 1
increment by 1 - 증가값 1
maxvalue 99999999 - 최대값 99,999,999
nocycle;
insert into tbl_bbs(bbsno, title, content, writer)
values(seq_bbs.nextval, '제목','내용','글쓴이');
- nextval : 현재 시퀀스 값의 다음값 반환
- currval : 현재값 반환
rownum
- sql문 실행 후 데이터마다 붙는 번호.
select rownum, bbsno, title, writer from tbl_bbs order by bbsno desc;
오라클 힌트
출처 : http://theone79.tistory.com/entry/%EC%98%A4%EB%9D%BC%ED%81%B4-hint-%EC%82%AC%EC%9A%A9%EB%B2%95
/*+INDEX_DESC(tbl_bbs pk_bbs) */
- 지정한 테이블의 인덱스 스캔을 실행하도록 유도(역순으로)
- 해당 쿼리가 인덱스 범위 스캔의 사용 시, 인덱스 컬럼의 값을 사용하여 역순으로 실행.
select /*+INDEX_DESC(tbl_bbs pk_bbs) +/ rownum,bbsno,title,writer from tbl_bbs where rownum <=10
tbl_bbs 테이블 생성
create table tbl_bbs(
bbsno number(50),
title varchar2(400) not null,
content varchar2(2000) not null,
writer varchar2(200),
regdate date default sysdate,
viewcnt number default 0
);
- primary key
alter table tbl_bbs add constraint pk_bbs
primary key (bbsno);
- 시퀀스 생성
create sequence seq_bbs
start with 1
increment by 1
maxvalue 99999999
nocycle;
- 데이터 삽입
insert into tbl_bbs(bbsno, title, content, writer)
values(seq_bbs.nextval, '제목','내용','글쓴이');
- 시퀀스 이용 데이터 재귀 복사
insert into tbl_bbs(bbsno, title, content, writer)
select seq_bbs.nextval, title, content, writer from tbl_bbs;
-> 실행할때마다 현재 데이터의 2배가 insert.
- 인라인 뷰,오라클 힌트 이용 게시판 페이지
select * from (
select /*+INDEX_DESC(tbl_bbs pk_bbs)*/
rownum rn, bbsno, title, content, writer, regdate, viewcnt
from tbl_bbs
where rownum <= 1*10 -- 1은 페이지번호를 뜻한다.
)
where rn > (1*10)-10 -- 1은 페이지번호를 뜻한다.
'프로그램 > DB' 카테고리의 다른 글
ERD (entity-relationship diagram) 링크. (0) | 2012.02.21 |
---|