Data Integrity(데이터 무결성)

(1) 무엇을 지킬것인가? 어떻게 지킬것인가

1) 실제(Entity) 무결성

 - 중복된 데이터 방지

 - primary Key, Unique

 

2) 영역(domain) 무결성

 - Check 

 

3) 참조(Reference) 무결성

 - Foreign Key /*자식 테이블에 참조 당함*/

 

(1) 칼럼(필드)의 속성

1) NN(Not Null) 속성

- Primary Key, not null

2) ND(Not Diplicated) 속성

- Primary Key, Unique

3) NC(Not Change) 속성 //변경 할수 없는 것

(3) Primary Key

1) 하나의 테이블에 단 한개만 설정

2) 여러 개의 필드르 ㄹ묶어서 설정 가능

 =============실습=============

1) testuser1으로 접속

create table tblExam()

/*nn속성을 하게하는 방법 not null*/

create table tblExam(

 id    number        not null,

 name  varchar2(10)   null

)

insert into tblExam(name) values('임꺽정'); 

/*nn속성으로 id 없이 넣기 불가능하다*/

/*id는 중복값 안됨*/


/*pimary key 넣기*/

/*1. 테이블 만들때 처음에 생성, 

2 이미 테이블 만들어진 상태에서 기본키 수정으로 넣기*/

create table tblExam(

 id    number         null,

 name  varchar2(10)   null

);


select * from tblExam;


alter table tblExam ADD constraint pk_id primary key(id);


insert into tblExam(id, name) values(null, '홍길동');

insert into tblExam(id, name) values(1, '홍길동');

insert into tblExam(id, name) values(1, '임꺽정'); 

/*임꺽정 무결성 제약조건에 위배됨 */


desc user_constraints;

select constraint_name, table_name from user_constraints;                                 /*어떤 제약이 걸려있는지 보고싶으면 이 쿼리문 이용*

(4) Uuique

1) 하나의 테이블에 여러 개 설정 가능

2) 중복 방지

create table tblExam(

 id    number    ,

 name  varchar2(10),

 constraints uk_id unique(id)

);

select constraint_name, table_name from user_constraints;

 /*어떤 제약이 걸려있는지 보고싶으면 이 쿼리문 이용*/


purge recyclebin; /*휴지통 비우기*/

/*절대로 중복을 허락하지 않음 unique 하지만 null값은 안막아짐*/

insert into tblExam(id,name) values(1, '홍길동'); 

insert into tblExam(name) values('임꺽정'); 


select * from tblExam;

(5) 필드 수정 , 추가, 삭제

1) 필드 수정

alter table tblExam modify name varchar2(20);

 /*말로만 수정이지 원래는 삭제했다가 다시만드는것*/

/*varchar 가변길이 문자열의 길이가 일정하지 않을때 

최대길이를 기준*/

2) 필드 추가

alter table tblExam ADD zipcode char(7);

/*char 고정길이 문자열의 길이가 일정할때 

한글자든 두글자를 입력하던 메모리를 7글자로 고정 성능상 좋음*/

3) 필드 삭제

alter table tblExam Drop column zipcode; 

/*삭제할때는 어떤것을 삭제할것인지 써야됨*/

(6) Default

create table tblExam(

 id    number   default 0 ,

 name  varchar2(10),

point number(3,2)

);


insert into tblExam  ( point) values( 3.14);


/*name에 값을 입력하지 않으면 '무명씨'라고 자동입력되게 처리*/

alter table tblExam modify name default '무명씨';

select * from tblExam;


(7) SEQUENCE

- 자동으로 카운팅하여 일련번호를 리턴함

- 중복값 들어갈 수 없음

===========================================

Create sequence seq_num;

-SEQUENCE를 사용하기 위해서는 정수형만 가능하다.

insert into tblExam(id, name, point) values(seq_num.nextVal, '박소원');

(8) Check

drop table tblExam;

/*city에 입력할 수 있는 도시는 단 서울, 경기, 인천 뿐이다 .*/

create table tblExam(

id number,

name varchar2(10),

city varchar2(10),

constraints ck_city check(city = '서울' or city = '경기'

 or city = '인천')

)

insert into tblExam(id, name, city) values(seq_num.nextVal, '박소원', '서울');

insert into tblExam(id, name, city) values(seq_num.nextVal, '홍길동', '제주'); 

/*체크 제약조건이 위배 왜냐면 서울,경기, 인천만 해놈*/

(9) Foreign key

Create table tblDept(

deptno char(3),

dname varchar2(10)

);

insert into tblDept(deptno, dname) values('100', '영업부');

insert into tblDept(deptno, dname) values('101', '마케팅부');

create table tblEmp(

empno number,

ename varchar2(10),

hiredate date,

deptno char(3)

);

insert into tblEmp (empno, ename, hiredate, deptno) 

valuse(1, '홍길동', sysdate, '100');/*deptno를 기본키로 설정해줘야됨*/

alter table tblDept add constraints pk_deptno Primary key(deptno);


alter table tblemp add constraints pk_deptno_emp Primary key(deptno);


/*테이블 안에 조건이 안맞는 것이 있으면 테이블 변경이 안됨*/

alter table tblEmp add constraints fk_dept_emp_deptno foreign                 key(deptno) references tblDept(deptno);    


update tblEmp set deptno = '101' where empno=2;

'DB > Oracle' 카테고리의 다른 글

[Oracle] Trigger  (0) 2017.07.03
[Oracle] View 란?  (0) 2017.07.03
[Oracle] Transaction  (0) 2017.07.03
[Oracle 과제] Join에 대한 문제  (0) 2017.06.26
[Oracle] Join  (0) 2017.06.26

+ Recent posts