X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 467D3D1B45E for ; Sat, 24 Apr 2004 13:54:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 55076-09 for ; Sat, 24 Apr 2004 13:54:23 -0300 (ADT) Received: from biglumber.com (biglumber.com [207.228.252.42]) by svr1.postgresql.org (Postfix) with SMTP id B537ED1B44C for ; Sat, 24 Apr 2004 13:54:21 -0300 (ADT) Received: (qmail 7138 invoked from network); 24 Apr 2004 16:54:21 -0000 Received: from unknown (HELO localhost) (207.228.252.42) by 0 with SMTP; 24 Apr 2004 16:54:21 -0000 From: "Greg Sabino Mullane" To: pgsql-sql@postgresql.org Subject: Re: Design Problem... X-PGP-Key: 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8 X-Request-PGP: http://www.biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 In-Reply-To: <000001c4287b$db326d20$0202a8c0@buildways> Date: Sat, 24 Apr 2004 16:54:21 -0000 X-Mailer: JoyMail 1.47 Message-ID: <725e4370e635c7b3c8ff293b53a284e9@biglumber.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200404/266 X-Sequence-Number: 17641 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 > The problem is that it is possible that the service can be switched to a > different business unit, and then possibly back to the original later on. First, you will get more responses if you do not create a new topic in the middle of an existing thread. Here is a simplified answer to your problem. Basically, you need to create a link between a business and a service, and note when that link was created. You can grab the highest creation time for a service to see which business currently owns it. Depending on how often things change around, you may want to simply have a trigger on the bs_map table that updates a "business" column in the services table, rather than having to compute the max creation time constantly. CREATE TABLE business ( id SERIAL UNIQUE, bname VARCHAR ); CREATE TABLE service ( id SERIAL UNIQUE, sname VARCHAR ); CREATE TABLE bs_map ( business INTEGER NOT NULL, service INTEGER NOT NULL, assigned TIMESTAMPTZ NOT NULL DEFAULT now() ); ALTER TABLE bs_map ADD CONSTRAINT "bs_map_business_fk" FOREIGN KEY (business) REFERENCES business(id) ON DELETE RESTRICT ON UPDATE CASCADE; ALTER TABLE bs_map ADD CONSTRAINT "bs_map_service_fk" FOREIGN KEY (service) REFERENCES service(id) ON DELETE RESTRICT ON UPDATE CASCADE; - -- Greg Sabino Mullane greg@turnstep.com PGP Key: 0x14964AC8 200404241255 -----BEGIN PGP SIGNATURE----- iD8DBQFAipwPvJuQZxSWSsgRApPVAJwPvc2aTadzTfKBJIge+2bh+IQ7qwCeN2rZ MTilGUtbg0y4DOAENUzXc80= =Jw5D -----END PGP SIGNATURE-----