473,473 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Conditional foreign key?

We have a list of customers, some of whom have purchased feature X and some of
whom have not. If a customer has paid for featurex, they can use it, and a
strict relationship between cust_items.items_id and items.id, but only if
they are signed up to use featurex, otherwise I want cust_items.items_id to
be NULL.

Currently, I have tables defined similar to:

create table Customer (
id serial unique not null,
name varchar(30) unique not null,
FeatureX bool not null
);

Create table cust_items (
id serial unique not null,
customer_id integer not null references customer(id),
name varchar(30) not null,
type varchar not null,
items_id integer default null references featurex(id),
cust_active bool not null
);

// type is one of "book", "tape", or "featurex"

Create table items (
id serial not null unique,
title varchar(30)
);
I want to say
"If the cust_items.type='featurex' then (
(customer.featurex must be true)
AND
(cust_items.items_id must be in
(select id from items)
)";

I'm just stumped as to how to say this.
I've tried, with the above table defs,
CREATE RULE check_customer ON UPDATE to cust_items
WHERE NEW.type='featurex' AND
NEW.customer_id IN
(SELECT customer.id FROM customer
WHERE featurex=TRUE
)
DO ... ? <too many tries to count>

Any pointers, hints, or info on this kind of statement?

Thanks,

Ben

--
"I kept looking around for somebody to solve the problem.
Then I realized I am somebody"
-Anonymous
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
1 1610
Benjamin Smith wrote:
We have a list of customers, some of whom have purchased feature X and some of
whom have not. If a customer has paid for featurex, they can use it, and a
strict relationship between cust_items.items_id and items.id, but only if
they are signed up to use featurex, otherwise I want cust_items.items_id to
be NULL.

Currently, I have tables defined similar to:

create table Customer (
id serial unique not null,
name varchar(30) unique not null,
FeatureX bool not null
);

Create table cust_items (
id serial unique not null,
customer_id integer not null references customer(id),
name varchar(30) not null,
type varchar not null,
items_id integer default null references featurex(id),
cust_active bool not null
);

// type is one of "book", "tape", or "featurex"

Create table items (
id serial not null unique,
title varchar(30)
);
I want to say
"If the cust_items.type='featurex' then (
(customer.featurex must be true)
AND
(cust_items.items_id must be in
(select id from items)
)";

I'm just stumped as to how to say this.
I've tried, with the above table defs,
CREATE RULE check_customer ON UPDATE to cust_items
WHERE NEW.type='featurex' AND
NEW.customer_id IN
(SELECT customer.id FROM customer
WHERE featurex=TRUE
)
DO ... ? <too many tries to count>

Any pointers, hints, or info on this kind of statement?

This is a trigger job not a rule one.
Regards
Gaetano Mendola


Nov 23 '05 #2

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

Similar topics

0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
2
by: geoff | last post by:
The table creation script(at the end of this post) works fine on 4.0.1-alpha-win, but the foreign key constraints fail on 4.0.15-win. I am starting the server with the same command for both...
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
31
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific...
28
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
3
by: B Love | last post by:
I would like to display a drop-down list on a form conditional on the value of another field on the same form. I am not sure if this is good form or not. I am using Access97, but have access to...
10
by: Shawn Chisholm | last post by:
Hi, I am trying to deal with a deadlock situation caused by foreign key references on insert and I was wondering if anyone knows what order the foreign keys are locked (or evaluated) in for a...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
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,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.