1 : N 또는 M : N 관계의 수는 관계의 정규 형식을 결정하지 않습니다. 문제는 실제로 정규화와는 아무런 관련이 없습니다.
일부 테이블. . .
귀하의 디자인
팔로우 기준을하실 수 있습니다. 국가별로 ISO 3166-1을 따를 것입니다.
create table countries (
iso_country_code char(3) primary key,
country_name varchar(75) not null unique
);
insert into countries (iso_country_code, country_name) values
('USA', 'United States of America'),
('GBR', 'United Kingdom of Great Britain and Northern Ireland'),
('MKD', 'Macedonia (the former Yugoslav Republic of)'),
('ZZZ', 'Unknown country'); -- 'ZZZ' is reserved for a user-assigned value.
경주는 CDC/HL7 경주 코드를 따를 것입니다. 다른 표준이 있습니다. 그 중 하나가 더 적절할 수 있습니다. http://www.cdc.gov/nchs/data/dvs/race_ethnicity_codeset.pdf
대부분의 응용 프로그램은 각 사람마다 여러 개의 경주 코드를 허용합니다. 에 대한 실제 사실을 무시합니다.이 질문입니다.
create table races (
cdc_unique_id char(6) primary key,
cdc_race_concept varchar(50) not null unique
);
insert into races (cdc_unique_id, cdc_race_concept) values
('2056-0', 'Black'),
('2106-3', 'White'),
('2076-8', 'Native Hawaiian or other Pacific islander'),
('zzzz-z', 'Unknown');
create table persons (
person_id integer primary key,
person_full_name varchar(25) not null,
iso_country_code char(2) not null
default 'ZZZ'
references countries (iso_country_code)
on update cascade
on delete set default,
cdc_unique_id char(6) not null
default 'zzzz-z'
references races (cdc_unique_id)
on update cascade
on delete set default
);
이 세 표는 모두 5NF 이상입니다.
디자인에 잠재적 인 문제가있을 수 있습니다. 은 국가 및 경쟁의 임의 구문 구문 을 허용한다는 점에서 다릅니다. 국가와 인종 대신에 우리는 미국에서 도시와 주에 관해 이야기하고 있다고 상상해보십시오. 도시와 주를 임의로 구분하면 "San Francisco, AL"이 허용됩니다. 그러나 알라바마에는 "샌프란시스코"라는 도시가 없습니다.
그래서 임의의 구문 분석을 허용하면 나쁜 결정이 될 수 있습니다.
상사의 디자인은
-- The same as the table above.
create table countries (
iso_country_code char(3) primary key,
country_name varchar(75) not null unique
);
insert into countries (iso_country_code, country_name) values
('USA', 'United States of America'),
('GBR', 'United Kingdom of Great Britain and Northern Ireland'),
('MKD', 'Macedonia (the former Yugoslav Republic of)'),
('ZZZ', 'Unknown country'); -- 'ZZZ' is reserved for a user-assigned value.
-- Also the same as the table above.
create table races (
cdc_unique_id char(6) primary key,
cdc_race_concept varchar(50) not null unique
);
insert into races (cdc_unique_id, cdc_race_concept) values
('2056-0', 'Black'),
('2106-3', 'White'),
('2076-8', 'Native Hawaiian or other Pacific islander'),
('zzzz-z', 'Unknown');
-- This table is new.
create table person_types (
iso_country_code char(3) not null
default 'ZZZ'
references countries (iso_country_code)
on update cascade
on delete set default,
cdc_unique_id char(6) not null
default 'zzzz-z'
references races (cdc_unique_id)
on update cascade
on delete set default,
primary key (iso_country_code, cdc_unique_id)
);
insert into person_types values
('USA', '2016-3'),
('USA', '2056-0'),
('GBR', '2016-3'),
('GBR', '2056-0'),
이 "person_types"표는 데이터베이스 설계하지 않는 사실 를 기록합니다. 은 백인과 흑인 인 이 미국과 영국 출신 일 수 있다고 기록합니다. 사실을 기록하는 것이 중요한 경우 은에 "person_types"가 포함되어야합니다.
또한이 표는 다른 의견에 언급 된 문제에 영향을받지 않습니다. 당신은 반복적으로 iso_country_code와 cdc_unique_id가 모두 null 인 행을 추가 할 수 없습니다 (not null
제약 조건), iso_country_code 및 cdc_unique_id (primary key
제약 조건) 등을 복제 할 수 없습니다.
개념적으로, 정규화하기 전에 저장할 사실을 결정합니다. 정규화는 스키마에없는 속성을 사용하면 도움이 될 수 없습니다. 그것은 다른 데이터베이스 디자인 작업입니다.
-- Structurally identical to the table above.
-- Constraints are slightly different.
--
create table persons (
person_id integer primary key,
person_full_name varchar(25) not null,
iso_country_code char(2) not null
default 'ZZZ',
cdc_unique_id char(6) not null
default 'zzzz-z',
constraint person_types_fk foreign key (iso_country_code, cdc_unique_id)
references person_types (iso_country_code, cdc_unique_id)
on update cascade
on delete set default
);
이 네 개의 테이블 모두 적어도 5NF에 있습니다.
차이점은 한 세트의 테이블이 다른 테이블보다 정규화되어 있지 않다는 점입니다.
차이점은 테이블의 한 세트는 다른 하나가이 아니라는 사실을 기록한다는 것입니다.
* "Person 테이블에는 PersonType에 대한 하나의 외래 키 열만 있습니다."* person_type_id와 같은 새로운 특성과 몇 가지 새로운 기능 종속성을 도입 할 경우에만. * 그 *는 정규화가 아닙니다. 그것은 대용 키를 사용하고 있습니다. (대리 키는 정규화가 아닌 데이터 압축과 관련이 있습니다.) 정규화는 새로운 특성을 도입하지 않습니다. 나는 하나의 후보 키 (country_code, race_code)를 갖는 person_type {country_code, race_code} 관계가 이미 5NF에 있다고 생각합니다. –
* "내 상사는 PersonType을 사용하여 어떤 국가/민족 조합이 유효한지 제한 할 수 있다고 주장합니다."* 사실입니다. 그리고 그 제약 조건을 강제하는 것이 요구된다면, 네 번째 테이블이 필요합니다. 그러나 실제로는 모국과 인종의 부적합한 조합이 아닙니다. 이것이 실제 상황에 적용되는지는 알 수 없습니다. –
* "네이티브 국가와 인종의 잘못된 조합은 실제로 존재하지 않습니다"* 당신이 옳습니다. 기본적으로 잘못된 조합은 없습니다. 그것은 비즈니스 규칙/논리의 이상이며 내 생각에 그렇지 않으면 구현 될 수 있습니다. Currentyl 우리의 init scrips는 "person_type"에 항목을 만들지 만 가능한 항목 (count (countries) * count (race))의 최대 항목까지 추가 항목을 만들지 않습니다. – xxtesaxx