Re: Which database design is better
From: David Browne (meat_at_hotmail.com)
Date: 10/03/04
- Next message: Steve Kass: "Re: Which database design is better"
- Previous message: Hugo Kornelis: "Re: CHARINDEX in user-defined function"
- In reply to: Jacco Schalkwijk: "Re: Which database design is better"
- Next in thread: Mark Wilden: "Re: Which database design is better"
- Reply: Mark Wilden: "Re: Which database design is better"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 3 Oct 2004 16:12:14 -0500
"Jacco Schalkwijk" <jacco.please.reply@to.newsgroups.mvps.org.invalid> wrote
in message news:esi4p0VqEHA.952@TK2MSFTNGP10.phx.gbl...
>> But what's wrong with the circular foreign key reference? It faitfully
>> models what's going on here.
>
> Good question, I had to think about that.
>
> If you have the circular foreign key reference, that doesn't guarantee
> that the Employee actually works in the store, to which he/she is assigned
> as the key employee. It only guarantees that the EmployeeID actually
> exists in the Employee table. And you can't have a foreign key on both the
> StoreID and EmployeeID column from the Store table referencing the
> Employee table.
>
First off, it's still better to guarantee at least that the key employee
exists in the employee table,
Second, you can make the relationship include the store number to guarantee
that the key employee belongs to the correct store.
David
alter table store drop constraint fk_store_key_employee
drop table employee
drop table store
go
create table store
(
id int primary key,
key_employee int null
)
create table employee
(
id int not null,
store int not null references store,
constraint pk_employee
primary key(id,store)
)
go
alter table store
add constraint fk_store_key_employee
foreign key (key_employee,id) references employee(id,store)
go
insert into store (id) values (1)
insert into store (id) values (2)
insert into employee (id,store) values (1,1)
insert into employee (id,store) values (2,1)
insert into employee (id,store) values (3,2)
insert into employee (id,store) values (4,2)
update store
set key_employee = 2
where id = 1
--succeeds
update store
set key_employee = 2
where id = 2
--fails
- Next message: Steve Kass: "Re: Which database design is better"
- Previous message: Hugo Kornelis: "Re: CHARINDEX in user-defined function"
- In reply to: Jacco Schalkwijk: "Re: Which database design is better"
- Next in thread: Mark Wilden: "Re: Which database design is better"
- Reply: Mark Wilden: "Re: Which database design is better"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|