본문 바로가기
개발 수업/DB

[Oracle/SQL] 게시판 CRUD(Create, Read, Update, Delete)

by 오늘 하루s 2023. 6. 14.
728x90
더보기

Day31~32. 230613~14

게시판 CRUD(Create, Read, Update, Delete)

게시판 table 생성

create table 테이블명(
 컬럼명 데이터타입(크기)[default 기본값][constraint 제약조건명] 제약조건
)[tablespace 테이블스페이스];
1
2
3
4
5
6
7
8
9
create table board(
    b_no      number(4constraint board_no_pk primary key--글번호
    b_title   varchar2(100constraint board_title_uk UNIQUE--제목
    b_content varchar2(300constraint board_content_nn NOT NULL--글내용
    b_writer  varchar2(20), --작성자
    b_writeDate date default sysdate, --작성일
    b_hits    number(5default 0 constraint board_hits_ck CHECK(b_hits>=0--조회수
);
COMMENT ON COLUMN "SCOTT"."BOARD"."B_NO"IS 'Primary key of board table';
cs

 

board 테이블의 글번호용 seq_bno 시퀀스생성

1
2
3
create sequence seq_bno 
start with 1
increment by 1;
cs

 

글쓰기

1
2
3
4
5
6
7
8
9
10
11
insert into board
 values (seq_bno.NEXTVAL,'공지입니다','깨끗한 게시판환경을 조성합시다.','작성자1',sysdate,10);
 
insert into board(b_no,b_title,b_content,b_writer,b_writeDate,b_hits)
values (seq_bno.NEXTVAL,'','내용2','',null,null);
 
insert into board(b_no,b_title,b_content,b_writer)
values (seq_bno.NEXTVAL,'제목22','내용22','작성자22');
 
insert into board(b_no,b_title,b_content,b_writer)
values (seq_bno.NEXTVAL,'제목23','내용23','작성자22');
cs

 

특정게시글의 상세보기

1) 조회수 증가

=>b_no가 1인 글의 조회수 증가 b_no가 1인 글의 조회수 증가

1
2
3
update board
set b_hits=b_hits+1
where b_no=1;
cs

 

2) 상세보기

1
select * from board where b_no=1;
cs

 

목록 조회(가장 최신글부터)

1
2
select * from board
order by b_writedate desc;
cs

 

목록조회+현재페이지에 따른 게시물개수 반영

1
2
3
select * from
where b_no between? AND ?
order by b_no desc;

cs

 

수정하기

1
2
3
update board
set b_title='수정제목',b_content='수정내용'
where b_no=21;
cs

 

삭제하기

1
2
delete from board
where b_no=21;
cs

 


댓글게시판 table 생성

1
2
3
4
5
6
7
8
9
10
11
create table replyboard(
    r_no      number(4constraint replyboard_rno_pk primary key--댓글번호
    b_no      number(4),  --원글번호
    r_content varchar2(270constraint replyboard_rcontent_nn NOT NULL--댓글내용
    r_writer  varchar2(20), --댓글작성자
    r_writeDate date default sysdate, --댓글작성일
   
    constraint replyboard_bno_fk foreign key(b_no)
     references board(b_no)
     [on delete cascade--부모가 제거되면 자식도 제거
);
cs

 

replyboard 테이블의 글번호용 seq_rno 시퀀스생성

1
2
3
create sequence seq_rno 
start with 1
increment by 1;
cs

 

board테이블의 1번 원글에 대한 댓글을 작성

1
2
insert into replyboard(r_no,b_no,r_content,r_writer,r_writeDate)
 values (seq_rno.NEXTVAL,1,'동참합니다','댓글작성자1',sysdate);
cs

 

1번 원글상세페이지에서 댓글목록조회(최신댓글부터)

1
2
3
4
select r_no,b_no,r_content,r_writer,r_writeDate
from replyboard
where b_no=1
order by r_writeDate desc;
cs

 

728x90

'개발 수업 > DB' 카테고리의 다른 글

[Oracle/SQL] 부모,자식 table(insert,update,delete)  (0) 2023.06.14
[Oracle/SQL] 테이블 변경  (0) 2023.06.14
[Oracle/SQL] table 제약조건  (0) 2023.06.14
[Oracle/SQL] 테이블 DDL  (0) 2023.06.13
[Oracle/SQL] 다중테이블 insert  (0) 2023.06.13