473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Mandatory on one side of one to many?

Production (one) to events (many)

Events must have a production (easy, non null FK)

A Production doesn't make (business) sense without an event. How to enforce
a mandatory relationship on the one side?

Any ideas?

Cheers, Mike MacSween
Nov 13 '05 #1
4 1912
Is this a SQL Server or Access or database theory question?

ANSI SQL92 allows DEFERRABLE constraints, which make it possible to
implement these circular references. Unfortunately, SQL Server doesn't
support this type of constraint. You could add an INSERT trigger to the
Production table to insert an Event automatically. Or just insert to both
tables in a transaction each time.

--
David Portas
SQL Server MVP
--
Nov 13 '05 #2
"David Portas" <RE****************************@acm.org> wrote in message
news:EJ********************@giganews.com...

Thanks David
Is this a SQL Server or Access or database theory question?

ANSI SQL92 allows DEFERRABLE constraints, which make it possible to
implement these circular references. Unfortunately, SQL Server doesn't
support this type of constraint. You could add an INSERT trigger to the
Production table to insert an Event automatically. Or just insert to both
tables in a transaction each time.
SQL Server 2000

Even if the inserts are wrapped in a transaction presumably the constraint
would still apply?

All I could think of was a transaction which lifted the constraint, did the
inserts, then re-applied the constraint. But seems a bit bodgy.

What database engine(s) do implement DEFERRABLE constraints?

Cheers, Mike MacSween

--
David Portas
SQL Server MVP
--

Nov 13 '05 #3
> Even if the inserts are wrapped in a transaction presumably the constraint
would still apply?
Correct. The constraint is applied at statement level.
All I could think of was a transaction which lifted the constraint, did
the
inserts, then re-applied the constraint.
You can use the ALTER TABLE... CHECK / NOCHECK clause to do that but there
isn't much point. SQL Server's NOCHECK doesn't defer the constraint, it
disables it. The values aren't checked and the constraint may be violated.
The ALTER statement is scoped to the database so this opens the table to
constraint violations from other connections for the duration. Better do
without the constraint and put all inserts through an SP that updates both
tables appropriately.
What database engine(s) do implement DEFERRABLE constraints?


Postgres and Oracle do. I expect there are others.

A possibile compromise is to use a nullable FK on Production. This obviously
prevents FK violations while allowing nulls.

CREATE SCHEMA AUTHORIZATION dbo

CREATE TABLE Production (prod_code VARCHAR(10) PRIMARY KEY, event_code
INTEGER NULL, FOREIGN KEY (prod_code,event_code) REFERENCES Events
(prod_code,event_code))

CREATE TABLE Events (prod_code VARCHAR(10) NOT NULL REFERENCES Production
(prod_code), event_code INTEGER NOT NULL, PRIMARY KEY
(prod_code,event_code))

--
David Portas
SQL Server MVP
--
Nov 13 '05 #4
"Mike MacSween" <mi******************@btinternet.com> wrote in message news:<41**********************@news.aaisp.net.uk>. ..
Production (one) to events (many)

Events must have a production (easy, non null FK)

A Production doesn't make (business) sense without an event. How to enforce
a mandatory relationship on the one side?


Logically, the constraint you want goes something like this:

create constraint ProductionValid
not exists ((Production over { ID })
minus (Events over { Production_ID } rename { Production_ID ID }))

It's a not quite a foreign key, because it targets a non-key column of
the Events table, but the idea is the same. (Inclusion dependency is
the formal term, of which foreign keys are a special case). From an
implementation standpoint, I know of only one system that would
actually allow you to enforce such a constraint. I could be wrong
because a system that allowed deferred event handlers would allow this
as well, but I know of only one that allows this as well. The system
is Dataphor, and you could use the following transition constraint
definitions to enforce it in this product:

create table Production { ID : Integer, ..., key { ID } };

create table Event { ID : Integer, Production_ID : Integer, ..., key {
ID } };

create reference Event_Production
Event { Production_ID } references Production { ID };

alter table Production
{
create transition constraint ProductionValid
on insert exists (Event where Production_ID = new.ID)
on update (old.ID = new.ID) or exists (Event where Production_ID =
new.ID)
};

alter table Event
{
create transition constraint ProductionValid
on update (old.Production_ID = new.Production_ID) or
exists (Event where Production_ID = old.Production_ID)
on delete
exists (Event where Production_ID = old.Production_ID)
};

The transition constraints enforce the requirement that a given
production have at least one event at all times. The system infers
that because the transition constraint definitions reference the
database, validation must be deferred to transaction commit. Dataphor
is the only system I know of that would allow such a constraint to be
enforced server-side. Every other solution requires client-side
enforcement, I'm afraid.

Hope this helps,
Bryn Rhodes
Alphora
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
6
by: Frans Englich | last post by:
Hello, In my use of getopt.getopt, I would like to make a certain parameter mandatory. I know how to specify such that a parameter must have a value if it's specified, but I also want to make...
1
by: Chris West | last post by:
I have an schema that defines an element as mandatory, but I also want the XML instance to have some content for that mandatory element. I can use a regular expression to guarantee content, but...
4
by: MT | last post by:
Hi all, this sounds like an easy enough thing to do, but after spending 45 minutes searching google and various javascript sites I can't find out how to make a textfield (textbox or whatever you...
1
by: Le Tubs | last post by:
Hi I am wondering if anybody has come accross this problem before, basically I have to validate a xml schema (not the data, this is done at a later stage). The only problem is that there are...
6
by: Ruso | last post by:
I have this function, which should change a value of the texfield(s) depending on the radio buttons selection. function foo(obj) { if (!array) { flag++; } if (array != obj.value ) {...
4
by: plumba | last post by:
Let me explain.... I have a form (most of which you guys have helped me with!!). The initial problem I was having is that it was holding the data in the fields after submit, I resolved this by...
2
by: AR123 | last post by:
Hi I have set up a form. What I want to to is with the fields: Company Postcode Agency Number Policy Number I want these to be mandatory however if someone fills in the company postcode for...
2
by: BD | last post by:
I'm working with a data model which is managed in the AllFusion ERwin tool. Within the modeler, some of the FK relationships are 'non-mandatory' - which is to say that for a given child value, a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.