X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id C50929FA2C3 for ; Sat, 6 May 2006 14:55:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 05360-05 for ; Sat, 6 May 2006 14:55:37 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id E4D519F9C9E for ; Sat, 6 May 2006 14:55:36 -0300 (ADT) Received: (qmail 25966 invoked by uid 500); 6 May 2006 17:56:35 -0000 Date: Sat, 6 May 2006 12:56:35 -0500 From: Bruno Wolff III To: "kernel.alert kernel.alert" Cc: pgsql-sql@postgresql.org Subject: Re: problem with uniques and foreing keys Message-ID: <20060506175635.GA23459@wolff.to> Mail-Followup-To: Bruno Wolff III , "kernel.alert kernel.alert" , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/68 X-Sequence-Number: 25099 On Sat, May 06, 2006 at 12:27:41 -0500, "kernel.alert kernel.alert" wrote: > Hi list... > > Please i have a problem with this... > > I create the follow tables... > > -------------------------------------------------------- > > CREATE TABLE empresa ( > id_empresa integer NOT NULL primary key, > nombre varchar(45), > ); > CREATE TABLE casino ( > id_casino integer NOT NULL, > id_empresa integer REFERENCES empresa(id_empresa), > > nombre varchar(45), > > primary key(id_casino,id_empresa) > ); > CREATE TABLE maq_casino ( > id_empresa integer NOT NULL REFERENCES casino(id_empresa), > id_casino integer NOT NULL REFERENCES casino(id_casino), > > ubicacion_sala varchar(45) default NULL, > primary key(id_empresa,id_casino,id_tipo_maquina,id_maq_casino) > ); > > -------------------------------------------------------- > > When i'm gonna to create the last table i got this error: > > ERROR: no hay restriccion unique que coincida con las columnas dadas en la > tabla referida <> > > That in english is like .. there is no a unique constraint with columns > referred in casino table. > > > Please where is the problem... Unless there is a performance problem, id_empresa should not appear in the maq_casino table at all, since it is derivable from id_casino. Is id_empresa really allowed to be NULL in casino? If there is an actual performance problem and you really need to have it in the mag_casino table, then you want to add another unique key (id_casino, id_empresa) in casino and then change casino_mag to have a foreign key reference on (id_casino, id_impresa) to casino instead of the individual references you have now. If id_empresa in casino_mag is not supposed to be tied to the value of id_casino (which I doubt is the case), then it should be referencing empresa instead of casino.