Null 허용 열입니다 다룰 수있는이 (아래)이 정상화의 유일한 부분. Chris Date의 이해에서 열이 NULL을 허용하면 관계는 1NF가 아닙니다.
엄격하게 관계형 모델을 따라 가려고한다면, 당신은 이것을 주장으로 처리 할 수있을 것이라고 생각합니다. 그러나 대부분의 SQL 플랫폼은 어설 션을 지원하지 않습니다. SQL에서는이 라인을 따라 뭔가를 찾고 있다고 생각합니다. 나는 이것을 PostgreSQL에서 테스트했다.
create table users (
user_id integer primary key
);
create table accounts (
user_id integer not null references users (user_id),
account_id integer not null unique,
primary key (user_id, account_id)
);
create table assets (
user_id integer not null references users (user_id),
asset_id integer not null unique,
account_id integer null,
primary key (user_id, asset_id),
foreign key (user_id, account_id) references accounts (user_id, account_id)
);
-- Insert 3 users.
insert into users values (1), (2), (3);
-- User 1 has two accounts, user 2 has 3 accounts, user 3 has none.
insert into accounts values
(1, 100),
(1, 101),
(2, 102),
(2, 103),
(2, 104);
-- User 1 has 1 asset not assocated with an account.
insert into assets values (1, 200, null);
-- User 1 has 1 asset associated with account 101
insert into assets values (1, 201, 101);
-- User 1 tries to associate an asset with account 102, which doesn't belong to user 1.
insert into assets values (1, 202, 102);
[Fails with foreign key violation]
-- User 2 has two assets not associated with an account.
insert into assets values
(2, 500, null),
(2, 501, null);
"모든 계정이 단일 사용자와 연결되어 있습니다"- 두 명의 사용자가 동일한 계정 ID 번호를 가질 수 없다는 의미입니까? (자산에 대해 동일한 질문입니다.) –