본문 바로가기
DataBase & DBMS

22.12.02 java - DB 연동 및 쿼리 연습

by SoulMania 2023. 2. 16.

notice.sql
0.00MB

--시퀀스 삭제
DROP SEQUENCE notice_seq;

--시퀀스 생성
CREATE SEQUENCE notice_seq
START WITH 1
INCREMENT BY 1
NOCYCLE;

--테이블 삭제
DROP TABLE notice;

--테이블 생성
CREATE TABLE notice(
nbno number(4) constraint pk_notice_nbno primary key, --글번호
title varchar2(1000) constraint nn_notice_title NOT NULL, --제목
centent VARCHAR2(2000) constraint nn_notice_content NOT NULL, --내용
cre_date date default SYSDATE, --작성일
writer varchar2(30) constraint nn_notice_writer NOT NULL, --작성자
rcnt NUMBER(5) default 0 constraint ck_notice_rcnt check(rcnt>=0), --조회수
empno number(30)
constraint fk_notice_empno references emp(empno) on delete cascade --작성자 아이디
);

--더미데이터
insert into notice(nbno, title, centent, cre_date, writer, rcnt, empno)
values(notice_seq.nextval, '제목1', '내용1', SYSDATE-10,'관리자',100,7900);
insert into notice(nbno, title, centent, cre_date, writer, rcnt, empno)
values(notice_seq.nextval, '제목2', '내용2', SYSDATE-9,'관리자',90,7900);
insert into notice(nbno, title, centent, cre_date, writer, rcnt, empno)
values(notice_seq.nextval, '제목3', '내용3', SYSDATE-8,'관리자',88,7900);
insert into notice(nbno, title, centent, cre_date, writer, rcnt, empno)
values(notice_seq.nextval, '제목4', '내용4', SYSDATE-7,'관리자',77,7900);
insert into notice(nbno, title, centent, cre_date, writer, rcnt, empno)
values(notice_seq.nextval, '제목5', '내용5', SYSDATE-6,'관리자',66,7900);

commit;

/*
update notice set cre_date='22/11/22' where nbno=2;
update notice set cre_date='22/11/23' where nbno=3;
update notice set cre_date='22/11/24' where nbno=4;
update notice set cre_date='22/11/25' where nbno=5;
update notice set rcnt=100 where nbno=1;
*/

commit;

--1.목록조회-최신글부터 출력
select nbno, title, cre_date, writer
from notice
order by nbno desc;

--2.상세조회-특정글번호
select nbno, title, centent, cre_date, writer, rcnt, empno
from notice
where nbno = 1;

--3.등록--여기에서 임시로 작성자 id는 7900으로 고정
insert into notice(nbno, title, centent, cre_date, writer, rcnt, empno)
values(notice_seq.nextval, '?제목1', '?내용1', SYSDATE,'?관리자', 0,7900);

--4.수정 - 특정글번호의 제목, 내용, 작성자
update notice set title='바뀐제목1', centent='변경후내용1', writer='변경후관리자'
where nbno=1;

commit;

--5.삭제 - 특정글번호
delete from notice where nbno=1;

--제것은 centent라고 오타로 컬럼명이 만들어져서 그냥 진행했습니다.