Forum OpenACS Q&A: Response to SQL: how do you do this?

Collapse
Posted by Michael Bryzek on
Circular referential constraints certainly have their uses. The way to create the constraints in Oracle is as you describe, though you can just add the constraint later:
create table foo_vf_quantity (
       id                       integer primary key,
       quantity                 number not null, 
       cost                     number,
       notes                    varchar(2000),
       vf                       integer not null,
       last_user_modifying      not null references users,
       last_modified            date not null
);

create table foo_vf (
       id                       integer primary key,
       from_v                   not null references user_groups,
       to_f                     not null references f,
       default_q                references foo_vf_quantity,
       selected_q               references foo_vf_quantity,
       unique (from_v, to_f)
);

alter table foo_vf_quantity add foreign key (vf) references foo_vf(id);
Regarding selected and default columns - without understanding the problem you are modelling, it seems odd to have both a default and a selected value in the foo_vf table, esp. w/out any explicit constraints on their values.

Again, without understanding much about what you're really trying to do, another possible way to structure the tables would be to add a status column to the foo_vf_quantity table. Now, you can remove the reference from foo_vf to the quantity table. This schema would look like:


create table foo_vf (
       id                       integer primary key,
       from_v                   not null references user_groups,
       to_f                     not null references f,
       unique (from_v, to_f)
);

create table foo_vf_quantity (
       id                       integer primary key,
       quantity                 number not null, 
       cost                     number,
       notes                    varchar(2000),
       vf                       integer not null references foo_vf,
       last_user_modifying      not null references users,
       last_modified            date not null,
       status                   varchar(10) check (status in ('default', 'selected'))
);