X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5F942D818C for ; Sun, 20 Nov 2005 15:32:18 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 21319-05 for ; Sun, 20 Nov 2005 19:32:18 +0000 (GMT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id D0D40D7F20 for ; Sun, 20 Nov 2005 15:32:15 -0400 (AST) Received: from mx1.mail.ru (mx1.mail.ru [194.67.23.121]) by svr2.postgresql.org (Postfix) with ESMTP id 03CFCF0B23 for ; Sun, 20 Nov 2005 19:32:15 +0000 (GMT) Received: from [195.225.128.130] (port=25586 helo=[10.168.6.33]) by mx1.mail.ru with asmtp id 1Eduv7-000PS9-00 for pgsql-sql@postgresql.org; Sun, 20 Nov 2005 22:32:13 +0300 Date: Sun, 20 Nov 2005 22:32:09 +0300 From: "Grigory O. Ptashko" X-Mailer: The Bat! (v3.60.07) Professional Reply-To: "Grigory O. Ptashko" X-Priority: 3 (Normal) Message-ID: <1229661878.20051120223209@bk.ru> To: pgsql-sql@postgresql.org Subject: Please help to wite the constraint. MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=3.478 required=5 tests=[RCVD_IN_BL_SPAMCOP_NET=1.332, REPTO_OVERQUOTE_THEBAT=2.146, UPPERCASE_25_50=0] X-Spam-Score: 3.478 X-Spam-Level: *** X-Archive-Number: 200511/176 X-Sequence-Number: 23427 Hello, everybody! I don't whether it is possible to do the following but anyway I can't. I need to write a constraint as described below. Here are four tables: CREATE TABLE countries (id SERIAL, name VARCHAR(255), PRIMARY KEY (id) ); CREATE TABLE countries_names (id INT NOT NULL, id_lang INT NOT NULL, name VARCHAR(255), PRIMARY KEY (id, id_lang), FOREIGN KEY (id) REFERENCES countries (id), FOREIGN KEY (id_lang) REFERENCES lang (id) ); CREATE TABLE contact_info_fields (id SERIAL, name VARCHAR(255) NOT NULL, PRIMARY KEY (id) ); CREATE TABLE contact_info_records (id_user INT NOT NULL, id_ci_field INT NOT NULL, id_lang INT NOT NULL, value TEXT, PRIMARY KEY (id_user, id_ci_field, id_lang), FOREIGN KEY (id_user) REFERENCES users (id), FOREIGN KEY (id_ci_field) REFERENCES contact_info_fields (id), FOREIGN KEY (id_lang) REFERENCES lang (id) ); The last table contains contact information records of different types. These types are taken from the table contact_info_fields. In particular, there can be the type 'country' say with id=1. Then the contact_info_records table can contain the following info: id_ci_field=1 and the VALUE field must contain a country's name but ONLY if it exists in the countries table (column 'name'). So it turns out to be a wierd foreign key. Is it possible to write such a constraint? Thanks!