데이터베이스 질문드립니다. 0 8 1,475

by 김모씨 [SQL Query] [2019.07.08 14:39:40]



--회원 테이블
create table team_join (
userid varchar2(50) not null primary key,  -- 아이디
passwd varchar2(50) not null, -- 비밀번호
age number not null, -- 나이
name varchar2(50) not null, -- 이름
email varchar2(50) not null, -- 이메일
address varchar2(50) not null, -- 주소
zipcode varchar2(50) not null, -- 우편번호
hp varchar2(50) not null, -- 전화번호
gender varchar2(50) not null, -- 성별
point number not null, --포인트
join_date date default sysdate --가입일자
);
select*from team_join;

drop table team_join;

insert into team_join values('admin','1234',12,'관리자','kim@naver.com','서울시 강동구','11111111','1111-111','남',100000,'2019/1/1');
-------------------------------------------------------------------------------

?-- cascade constraints 제약조건까지 모두 삭제
drop table bboard cascade constraints;

--관리자 게시판 테이블
create table bboard (
bno number not null, --게시물번호
title varchar2(200) not null, --제목
content clob, --본문
writer varchar2(50) not null, --작성자
regdate date default sysdate, --작성일자
viewcnt number default 0, --조회수
primary key(bno)
);
----------------------------------------------------------------------------------
insert into bboard (bno,title,content,writer) values
(1,'제목','내용','관리자임');

drop table bboard;

select * from bboard;


----------------------------------------------------------------------------------------
--댓글 테이블
drop table reply cascade constraints;

create table reply (
rno number not null primary key,
bno number default 0,
replytext varchar2(1000) not null,
replyer varchar2(50) not null,
regdate date default sysdate,
updatedate date default sysdate
);

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

--foreign key 제약조건 추가
alter table reply add constraint fk_bboard
foreign key(bno) references bboard(bno);

--시퀀스 생성
drop sequence reply_seq;
create sequence reply_seq
start with 1
increment by 1;

--첨부파일 테이블

drop table attach cascade constraints;

---------------------------------------------------------------------------
create table attach (
fullName varchar2(150) not null, --첨부파일 이름
bno number not null, --board 테이블의 글번호
regdate date default sysdate, --업로드 날짜
primary key(fullName) --uuid적용한 파일이름
);
-----------------------------------------------------------------------------
--bno 컬럼에 foreign key 설정
alter table attach add constraint fk_bboard_attach
foreign key(bno) references bboard(bno);


select bno,writer,title,regdate,viewcnt
from bboard
order by bno desc;

-- writer의 id대신 이름이 나오게 하려면 member테이블과 조인
select bno,title,writer,name,regdate,viewcnt
from bboard b, team_join t
where b.writer=t.userid
order by bno desc;

select * from team_join;

delete from reply;
delete from bboard;

commit;

--페이지 나누기 테스트를 위해 레코드 입력
declare --선언부
    i number := 1;
begin --실행부
    while i<=100 loop
        insert into bboard (bno,title,content,writer)
        values
        ( (select nvl(max(bno)+1,1) from bboard)
        ,'Amado 공지사항입니다.'||i, '내용'||i, 'admin');
        i := i+1; --조건 변경
    end loop;
end;
/

select * from bboard;

--레코드 갯수 확인
select count(*) from bboard;

commit;

-- from => where => select => order by 절 순서로 실행됨
-- rownum : 레코드의 출력 순번
select *
from (
    select rownum as rn, A.*
    from (
        select bno,title,writer,name,regdate,viewcnt
        from bboard b, team_join t
        where b.writer=t.userid
        order by bno desc 
    ) A    
) where rn between 1 and 10;

--------------페이지 나누기 공식 참조-----------------
11페이지
    where rn between A and B
    (현재페이지-1) x 페이지당 게시물수 + 1
    = (11-1) x 10 + 1 = 101

11페이지
    [이전] 11 12 .... 20 [다음]

    몇번째 블록
    ( 현재페이지-1) / 페이지블록단위 + 1
    (11-1)/10 + 1 => 2번째 블록 ( 몇페이지 ~ 몇페이지 )

  ( 현재블록-1) x 블록단위 + 1
  (2-1) x 10 + 1 = 11페이지
게시물 991개 / 10개 => 99.1 => 100페이지
                100페이지 / 10개 => 페이지블록 10개

--글쓰기를 하기 위한 시퀀스 삭제
drop sequence seq_bboard;

--100번부터 시작하는 시퀀스 생성
create sequence seq_bboard
start with 100
increment by 1;
commit;

-- board에 시퀀스를 추가

create sequence seq_bboard
start with 1
increment by 1;

commit;

--board 테이블에 필드 추가
alter table bboard add show char(1) default 'Y';

desc bboard;

select * from bboard;

select bno,title,writer,name,regdate,viewcnt
   ,(select count(*) from reply where bno=b.bno) cnt
        , show
from bboard b, team_join t
where b.writer=t.userid
        and show='Y'
order by bno desc;

update bboard set show='N' where bno=100;--해당게시물 번호로 처리
--위 update문은 실제 데이터가 지워지는건 아니지만 보여지지 않게 처리함.

commit;

update bboard set title='책상제품 교환 공지사항2', content='책상환불해드립니다.'  where bno=99;


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


    
    
commit;

 

 

독학으로 작성한 쿼리문인데

 

무결성위반? 그거라고 뜨길래 원인을 모르겠어서 올려봐요..

 

너무어려운데 어디가 문제인지 대충이라도 알 수 있을까요?

by jkson [2019.07.09 08:09:37]

뭐 실행할 때 무결성 제약 떠요? 위에 쿼리 순서대로 하신 거예요?


by 김모씨 [2019.07.09 10:45:30]

사실 잘모르겠어요... 저런식으로 쓰면되나싶어서 기입하면서 한건데

 

리스트 수정 삭제 목록가기 이 3개 누르니까

 

### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into bboard (bno,title,content,writer) values     (seq_bboard.nextval, ?,?, ?)
### Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (SPRING.SYS_C008066) violated

 

이런 문장이뜨네요


by jkson [2019.07.09 11:23:58]

bboard의 pk가 bno가 맞고 seq_bboard 시퀀스가 정상적이라면 bboard.nextval 값이 이미 bboard 테이블에 있다는 말입니다. 조회해보세요.

select seq_bboard.currval, seq_bboard.nextval from dual

select * from bboard where bno in ('','')--조회된 두 값 있는지 체크


by 김모씨 [2019.07.09 13:06:39]

위의 셀렉트문 해보니 

 

currval 이 1001 nextval 1001 이라뜨네요.

 

아래문장은 그대로 실행하니 에러는 안나오는데 컬럼들만뜨고 값은없네요..

 

이러면어떻게해야할까요... 책에도 안나와있어서..ㅜㅜ


by jkson [2019.07.09 13:38:49]

조회된 두 값을 조회해보라는 말이었습니다.(nextval과 같이 조회하면 currval에 nextval 값이 나오는군요ㅎㅎ)

select * from bboard where bno in ('1000','1001')


by 김모씨 [2019.07.09 14:05:30]

그러면 안되는건가요????


by jkson [2019.07.09 15:21:03]

pk는 데이터를 구분할 수 있는 유일키로 전체 데이터 중에 딱 하나만 존재해야 하는 값입니다.

1001이 bno로 있는데 또 1001을 입력하면 오류가 납니다.


by 김모씨 [2019.07.09 15:57:39]

그러면 어떻게해야 중복이안될까요..ㅜㅜ

 

계속 select * from bboard where bno in ('1001','1001') 이렇게 같은 숫자가 나오네요..

 

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입