(오라클/oracle) 제약 타입/ constraint type in the oracle.



CONSTRAINT TYPE (제약 조건)


제약 조건의 종류
(types of constraint)

  • primary key
  • foreign key
  • not null
  • unique
  • check

example:

create table temp(
    employee_id number(6),
    first_name varchar2(20),
    last_name varchar2(20) constraint last_name_not_null_key not null,
    
    email varchar2(25),
    constraint email_unique_key unique(email),
    
    phone_number varchar2(20),
    hire_date date default sysdate,
    job_id varchar2(10),
    
    salary number(8,2),
    constraint wage_check check (salary>=0),
    
    commission_pct number(2,2),
    manager_id number(6),
    
    department_id number(4),
    constraint depart_fk foreign key(department_id)  references departments(department_id)
);


Q>설정된 테이블의 제약 조건 확인 하는 방법 
   (the method to check the constraint that is selected in the table)

이때 나는 hr 계정에서 적는것을 말하는것이다.
user_constraint 을 쓰면 이미 hr 테이블에 있는 values 의 조건을 보여준다.

A>select * from user_constraint

Q>alter을 이용해서 제약조건 넣는법

A>alter Table Table_name
   add constraint constraint_name
   foreign key (column_name)
      references referred_table (referred_column)


그럼 이것은 무엇인가?

select constraint_name, table_name from user_constraints 
where table_name = 'TABLENAME(must be in captial/대문자로 무조건)'

이것은 어떤 특정한 선택된 테이블 안에서 조건을 볼수있게 해주는것이다.

"primary key"

Only one primary key in the one table.


| a | b | c | d | e |

set a,b,c into one group for one primary key in one table -> no error
set both a , b for one primary key in a table -> error!

Q. when to use outline and inline constraint?
A.  when you have set a, b, c into one group for one primary key in a table, you use OUTLINE CONSTRAINT.

example:

create table users(

family name varchar
first-name varchar
address varchar
constraint constraint primary key //outline constraint
)

Be aware!
If you want to use primary key, you have to declare primary key first.

example:

| a | b | c | d | e |
Let's say e is a primary key, and it's a last column in your design,
but you must declare 'e' first as primary key is a primitive.

create table users(

e number constraint e_constraint_name primary key
a number
b number
c varchar2
d varchar2

)


The sequence of declaring the constraint type:

1. primary key
2. unique
3. 'judge with the "where" string
4. the list of column list / The frequently used column.



IF you want to change the primary key -> use alter.

example:

alter table users,
      add constraint e_contraint_name primary key(The column wish to change.)


"foreign key"

대충 이렇게 보면 된다.
DATA 4가 foreign key 을 써서 TABLE 1 에 있는 DATA 1 을 가져와서, DATA 4 에게 준다. 물론 이 DATA 1 은 unique 이나 primary key여야 한다.

self-referred ( 자기 자신에게 refer 되었다) 이말임 ㅎㅎㅎ ㅋㅎㅎㅎ;;;

공식:


column_name | data_type |  constraint |  foreign key | name for this fk | references | the parent table name | (the referred column) 

example:

wage number(8,2) constraint foreign key wage_fk references TABLE 1 (wage)


"not null / null " 

아무것도 안주면 null 이 기본 속성 값으로 변한다

컬럼 명 -> 컬럼 제약 조건 null-> ' 데이타 아무것도 없음'
만약에 컬럼 명에서 컬럼 제약 조건 not null 을 쓰면, '데이타 아무것도 없으면 안됨'

Whenever you write DB program, you must be caution with the null key, as the null key is excluded from ALL operation / arithmetic calculation.

You will never know the error , as it doesn't show in the surface of programming.


example:

create table integrity_Test(
   nullcol1 varchar2(10) not null,
   nullcol2 varchar2(10) null
)


"unique"

해당 컬럼에 중복된 데이터 입력 방지 (null values is okay~ but not repeated values)

example:

create table test(
     unicol1 varchar(10) unique not null,
     unicol2 varchar(10) unique,
     unico3 varchar(10),
     unico4 varchar(10) not null,
     constraints unique_column UNIQUE (unico3, unico4)
);

그럼 unico3 와 unico4 컬럼에서 중복된 데이터가 입력 방지 된다.

alter table test
add constraint unique_name unique (the selected column);

은 alter을 이용한 unique 설정 변경을 하는것이다.

"check"

입력되는 값을 체크하여, 일정한 조건에 해당되는 값만 입력될 수 있게 한다.

example:

gender varchar2(4) not null
constraint check_gender check (gender in('남자', '여자'))

wages number(10,2) not null
constraint check_wages check (wage >= 10000)

10000은 최저시급이라 하자. ㅎㅎ


"default" - is not constraint type, but it is used as constraint type.
제약 조건의 타입은 아니나 그렇게 행동을 해서 잠깐 보자.

기본값을 설정하여 값이 이볅되지 않을 경우 설정된 값이 자동으로 입력되게 한다.


example:

1) gender varchar2 (4) default 'boy'
2) alter table test
              modify in_date date default sysdate

sysdate- the date that is recorded in the system-> cannot be altered by the 3rd party.



댓글

이 블로그의 인기 게시물

c++ 랜덤 숫자 생성하기 / 컴퓨터 난수 시드 설정

(오라클) Sequence/시퀸스의 활용과 개념

(오라클) 뷰 + 조인 예제