Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYHF7-0007gy-3a for pgsql-novice@arkaria.postgresql.org; Mon, 11 May 2020 22:49:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jYHF5-0007XB-Vu for pgsql-novice@arkaria.postgresql.org; Mon, 11 May 2020 22:49:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYHF5-0007X3-PN for pgsql-novice@lists.postgresql.org; Mon, 11 May 2020 22:49:47 +0000 Received: from mout.gmx.net ([212.227.15.18]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jYHEz-0003rK-92 for pgsql-novice@lists.postgresql.org; Mon, 11 May 2020 22:49:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1589237379; bh=E33K5GYyqRdq+9vT+Y+R6tqLcNPp40gCIuUJiQLqSTo=; h=X-UI-Sender-Class:Date:From:To:Subject:In-Reply-To:References; b=BUqyunC5tb6dSEUXoiwRtPArJU7DWmEhFcL7lk9wNquoULJr1UBtKWx7b2z/Zvs37 qvKj8dEACe3IkVZ0TANCWHXgBCIQLN0TF00hx4W78NN+4ogHOunrfxUlGEXzVqULkH yTIKikL1Ji02Y4JnhS+Rau3BlV8pXT287EK/nRcY= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mail.defcon1.lan ([93.15.31.113]) by mail.gmx.com (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MS3mz-1jf7pA1H0l-00TRgT for ; Tue, 12 May 2020 00:49:39 +0200 Date: Tue, 12 May 2020 00:49:38 +0200 From: Bzzzz To: pgsql-novice@lists.postgresql.org Subject: Re: Restrictions for a specific situation in my DB Message-ID: <20200512004938.3634d392@msi.defcon1.lan> In-Reply-To: References: Organization: Anyone, anywhere BUT in banana demokratik republik of france and UERSS. Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:BqSW2M1Rb3BmtSGzlhXfu9IKrBTTqMH0NvDsHAvjtTNjdobb7FJ SAAtWMHpiAcnlID1ZOYNtx7r/OJOXyqvXlMsuvsu+vmbZuvksxPQyKT7cj9/j45bsIszswR k2nUIBLxOjaijU3cwS7sYy1fV+1t9LfGgtvEbYCWib4TxTnxs/e50IGIHNpHeKSBPMlIW0r Evksuvbhyh9i/fpbHN1sA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:fGSytHkukKU=:XlA6P9vR3xFcFRNR8bpU9D PoyDwjs7ez5+XT4XegtlJ7c1lENG5Bs96yaFj5YNnNA8ajG6kMAZZcKFlCu+MdJ4qevDu7NWv e66hEmxTq99ZfOqzPgNZPGMt22XzN3kHKvIJl9TYW+FjcLYIZgpA+Ao1N9T7S1AJ/2b3zSxL9 cPadmPiNOmdUFvjv3c1X45QZRS8UiwBhvVGaBZwSqFtzUPDXt3z74OFWYt4VUOaNa0a793i4K HTWxtp7pmm4N8VVBU5sXEtwuWNQSg4GgeVWr290W86xLEKtg85Yy4h6w9sJqYnKbGdt5jVeif hYewc0xO3gu3VIhytx3tRHxfj4FmkMsUJvBPQmIiQQZNERieNnGpCePivDrJaB9vKsCtKS9Jb ENT+YIbBXhP5DzmTEoTMV3bRE7BNkdJlRpAfwJk6M7GepTBieoIReP5nXN9kRKGaZAednI6Zt 1U9hbrl078gMOtZwk+za5dy14ZRS5vbDhlai/c/6aahFh2KSi7C41JBCIewtaUvYhi45Rkm4c xQeaarWfV5flkZDyYfj7N+TLOU9n7CoSYsavdcOQnB1k/8wRHF9cSvfZc/zOiULvKu7CZQwJA LWb6fCtP0vCDDqIi8brusJ3mlEOycIo0l/u5a5B/Y5HWX/KjMxHuziRGe8VVXqeYAxTkhZ1Cp UuLfnUxmMdxmQoDJGx7XkdTbHlNwPdjMKqalYRiXRHcIC64yU/d3Je1aFMAfWsJkTdwqlriQ0 SXvqiUC2jwdou3t2eqZ+ww6EsMj8U6G8USlt+o5ROA4x7kS1tjUxWNcbPI4UPa2tvhZt5axlR 7qAGxMLa9P8SOhTx+3wmNon7Hsc+y3mZyYwjK7k7gA2pvR0xlXpo4j7PoI+puuAGXGQND6R1b qXosE+TngllBe7ZRz5HoFPMjNBUHq3beSawPrESnBl3Na9eaT04EfYOEoup4rrWLImRPODBEi mAG5sDU17qQ/XhQ23lowRnZbORZNCHwlq4A8lkHeek1wJ9TehGGOM3vKjaqubPUfrqLVVyaYb +nktWoT7kHmsmyl3s0A48nDGzgJs4rOd4c9Hh1h5p7RTRHV8YCEBwSXfXdsSoIPAyu0nghpdc aD0RiGci4e+IyZn0KXIg6LD9FukvQF4BAdgBRvIkRL8iaZxRU3HmwdGT479QZ6XnzniDhzTfD KYOpPtcVlk1BEg5pgU9Ip7ufT8US9SZNbg8c9GC5VwsY4YuHqZv6x6IEqzhWdxQxXvHkG6t6b BY08GsEoCYOkrA8qH List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, 11 May 2020 17:33:25 -0500 JORGE MALDONADO wrote: > Hi, > > I have a table with the following structure: > > ------------------------------------------------------------------------= ------ > > FIELD TYPE COMMENTS > ------------------------------------------------------------------------= ------ > id serial primary key > m1 integer id of record in another table > m2 integer id of record in another table > > So, "m1" and "m2" are foreign keys. > I have read that it is a good practice to define an index for each > foreign key so "m1" and "m2" are also regular and independent indexes. > > Now, the combination of "m1" and "m2" together cannot be duplicated. > So for example, the following entries > > --------------------------------------------------------------------- > id m1 m2 COMMENTS > --------------------------------------------------------------------- > 1 2 10 > 2 8 3 > 3 18 1 > 4 2 10 This is invalid. > 5 13 8 This is invalid. > 6 18 18 This is invalid. > > Maybe the case for records 4 and 5 can be achieved by setting 2 > restrictions: > Restriction 1: "m1 + m2" fields > Restriction 2: "m2 + m1" fields > > But, is this a good approach? I forgot to say that your m1 and m2 existing indexes must also be of the unique kind. Jean-Yves