Post List

2015년 1월 9일 금요일

Constraint 제약조건 (primary, foreign , unique,check, default)

1.무결성 제약조건 6가지
 1) primarty key
 2)foreign key
 3)unique
 4)check
 5)default 정의
 6)null 값허용


2.primary key(기본키)
 1)crate 시 생성
 ㄱ)  create table usertb1 (userid nchar(8) not null primary key, ........)--제약조건 자동생성됨
 ㄴ) create table usertb1(userid nchart(8) not null constraint pk_userid primary key, ....) -- 제약조건 이름 강제 설정

2) alter (수정)시 생성
alter table usertb1
add constraint pk_userid --제약조건 이름 설정
primary key (userid) --userid를 기본키로 설정

단, userid(기본키 설정할려는 항목)이  null일경우 에러가남
그럴경우 먼저. not null로 변경해줘야함

예) alter table usertb1
     alter column userid nchar(8) not null

cf)identity 속성으로 지정한경우 자동 not null 임

3.foreign key(외래키)
 1) craete 시 생성
ㄱ)create table buytb1 ( num int not null primary key, userid  nchar(8) not null foreign key reperences usertb1(userid), ...)
ㄴ)create table buytb1 ( num int not null primary key, userid nchar(8) not null  constraint fk_usertb1_buytb1 foreign key reperences usertb1(userid)..)

2)alter 사용
alter table buytb1
add constraint fk_usertb1_buytb1--제약조건 이름 설정
foreign key (userid) - 참조키 설정(buytb1의 항목)
references usertb1(userid) -- 참조할 기본키
on update cascade-- usertb1의 기본키가 변경될시 buytb1의 userid도 자동변경
on delete cascade -- 기준테이블에 삭제일어날경우 외래키테이불도 삭제가 일어나도록 설정


단, 만약 usertb1 의 userid 와 buytb1의 userid 에 일치하지 않은 항목이 있으면. 에러남
그런경우 동일하지 않은 기존데이터를 무시하고 키설정
alter table buytb1 with nocheck -- with nocheck 속성 추가


4.unique 제약조건
중복되지 않는 유일한 값을 입력해야함 (null허용 , 단 중복되면 안됨으로 1개의 null만 허용)

1)create 시 생성
create table usertb1 (userid ............addr nchar(30) null unique)
create table usertb1(userid........ addr nchar(30) null constraint ak_addr unique)
create table usertb1(userid.... addr nchar(30) null, constraint ak_addr unique(addr)) -- 먼저 생성후 별도로 제약조건 추가

2)alter 사용
alter table usertb1
add constraint ak_addr --제약조건 이름 설정

unique(addr)


5.check 제약조건
check 제약조건은 입력되는 데이터를 점검하는 기능을 수행한다
(예 전화번호 국번, 출생년도 조절)

1) 예제1
출생년도가 1900년 이후 그리고 현재의 연도 이전
alter table usertb1
add constraint ck_birthYear
check
(birthyear >= 1900 and birthyear <= year(getdate()))

2)예제2
전화번호 국번 제약
alter table usertb1
add constraint ck_mobile1
check
(mobile1 in ('010', '011', '016', '017', '018', '019'))

3) with nocheck 옵션
전화번호 국번 제약조건을 걸때 이미 012라는 번호가 들어있는경우
국번체크 제약조건에 위배되지만 . 무시하고 넘어갈때 사용
alter table usertb1
with nocheck
add constraint ck_mobile1
check(mobile in ('010', '011', '016', '017', '018', '019'))

6.default 정의
default 정의는 데티너를 입력하지않았을때 자동으로 입력되는 디폴드 값을 정의 하는 방법이다
1)create시 정의
create table usertb1( userid .... birthYear int not null default year(getdate()), addr nchar(2) not null default '서울')

2)alter사용(for 문 사용해야함)
alter table usrtb1
add constraint cd_addr
default year(getdate()) for birthYear
go

3)insert시 default사용
insert into usertb1 valuse('wjn',.......default, defalut) --디폴트값사용
insert into usertb1 (userid, name ) values('wtw'.'우태운') --열이름이 명시되지않으면 default로 설정된값이 해당열에 자동입력됨
insert into usertb1valuse('hyh',......'1965','경기') --  값이 직접 명기되면 default 값은 무시됨


7.제약조건 삭제

alter table usertb1
drop constraint 제약조건이름

단,pk는 fk먼저 삭제해야함




cf)현재 table의  constraint 보기  :  exec sp_help table이름

댓글 없음:

댓글 쓰기