두 열을 모두 null로 선언하고 두 열 중 정확히 하나가 NULL이 아닌지 확인하는 검사 제약 조건을 만들 수 있습니다. 예를 들어
, 나는 교수 및 강사에 대해 별도의 테이블을 가지고 있고 클래스가 교수 또는 강사하지만, 나는이
SQL> ed
Wrote file afiedt.buf
1 create table professor(
2 professor_id number primary key
3*)
SQL>/
Table created.
SQL> create table lecturer(
2 lecturer_id number primary key
3 );
Table created.
SQL> create table class(
2 class_id number primary key,
3 lecturer_id number references lecturer(lecturer_id),
4 professor_id number references professor(professor_id),
5 check((lecturer_id is null and professor_id is not null) or
6 (lecturer_id is not null and professor_id is null))
7 );
Table created.
SQL> insert into professor values(1);
1 row created.
SQL> insert into lecturer values(20);
1 row created.
SQL> insert into class values(1, 20, null);
1 row created.
SQL> insert into class values(2, null, 1);
1 row created.
SQL> insert into class values(3, 20, 1);
insert into class values(3, 20, 1)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0014175) violated
처럼 뭔가를 할 수없는 모두를 할 수 있습니다 있도록 클래스를 모델링하고 싶다면
은 물론, 데이터 모델링 관점에서, 자주 LECTURER
또는 PROFESSOR
중 하나가되고 INSTRUCTOR
에 nullable이 아닌 외래 키와 CLASS
테이블을 만들 수있는 INSTRUCTOR_TYPE
단일 INSTRUCTOR
테이블을 만들 수있는 더 좋은 생각이 될 것입니다 표.
보기 만들기가 가장 좋습니다. – Aaron