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 1oQ2ip-0006VG-5k for pgsql-general@arkaria.postgresql.org; Mon, 22 Aug 2022 08:23:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oQ2in-0000iP-ND for pgsql-general@arkaria.postgresql.org; Mon, 22 Aug 2022 08:23:45 +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 1oQ2in-0000iF-Av for pgsql-general@lists.postgresql.org; Mon, 22 Aug 2022 08:23:45 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oQ2ik-0005Gr-77 for pgsql-general@lists.postgresql.org; Mon, 22 Aug 2022 08:23:44 +0000 Received: by mail-pl1-x633.google.com with SMTP id u22so9242589plq.12 for ; Mon, 22 Aug 2022 01:23:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:from:to:cc; bh=EcVbwE2mbgehirRRrpUoEs0xTq8sCbmJcNFr40JZSPE=; b=PMA/O25CnrCPSF7Gy0q3676sHukI0zZbqNEnLijWOUsu0UDFEhIkELfCD7lFI1bwA9 IDVzhH0xvaIiaZgC8POEKMWlTboNSakv43DzjQ1NHueT86tHGgjDWYkf2ebtjAR5LOjc 8KE7vkL4J7BkXAAjoSbGi4NM6TU42w2M4NzcLJNlFggGYe0OMuQZAei06nGBalkWw+9I IuL2qNV5lDA4Ll9+i1QPytFIE6jK+f5OpggPBMF932f89yINADvM1n5FwJCFKBy1M869 VEbXRnocbJBhUWa+3O21eSz8HhVw2Ag9eMcaqi/Pq+diGu6E9biXarQ0We4Vlnd30Khk FDoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:x-gm-message-state:from:to:cc; bh=EcVbwE2mbgehirRRrpUoEs0xTq8sCbmJcNFr40JZSPE=; b=8K6vZZaOTonMJIB3FCY+LCg0fhX9hQkivPdkU/92AzOrtlxhquwROBMo5eZR58Ju4n d+w2ahfcCU2Lt7UBwpxoI+nJP71RHtc/qNu7hhRiR7VAJhth7HyX6B5xHqHBxg5Vslib 2MDSn8UIODmd7D/U8rUcnE6iowID6P+qyKYgQPm2INK54NgC9yrvmEthF7+T9JGASoxo PCxZP8LGXYCfhkBE15VLTJTy1GYix2iIR4GLb99bSo43aeevkKUgM8HLDVfrblNhiN28 J7H592uGvYqNJRgyzDBQDxN+B464eCcoZ1Xp3SWkdjYuRYeScoSRrpgpSDOSQTUNny8I vxqw== X-Gm-Message-State: ACgBeo0KIsik+2bZZN4O5ipL3/iOkQWo1n/yVnKSSZkdE5MIvxmBv9DM qPnx37Khpxh3xvulmG3qpkAf5sIRxO8= X-Google-Smtp-Source: AA6agR5XvrFRPwD2xUCUTVzGMMq+zW5Ow/E5lhSEOS9BKsW74otpJ89bFOsZw+abXr9W515RziDzsw== X-Received: by 2002:a17:902:f650:b0:172:8ee1:7f40 with SMTP id m16-20020a170902f65000b001728ee17f40mr19793538plg.101.1661156619680; Mon, 22 Aug 2022 01:23:39 -0700 (PDT) Received: from [192.168.189.6] ([110.227.48.183]) by smtp.gmail.com with ESMTPSA id o4-20020a63e344000000b0041d6d37deb5sm6846149pgj.81.2022.08.22.01.23.38 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 22 Aug 2022 01:23:39 -0700 (PDT) From: JITEN KUMAR SHAH X-Google-Original-From: JITEN KUMAR SHAH Message-ID: <7af57a5d-459d-0533-2541-7a181fc9c274@GMAIL.COM> Date: Mon, 22 Aug 2022 13:53:35 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0 Thunderbird/91.11.0 Subject: Re: Creating constraint dynamically Content-Language: en-US To: pgsql-general@lists.postgresql.org References: <129838278.823301.1661153341365.ref@mail.yahoo.com> <129838278.823301.1661153341365@mail.yahoo.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/22/22 13:09, Wim Bertels wrote: > sivapostgres@yahoo.com schreef op ma 22-08-2022 om 07:29 [+0000]: >> ALTER TABLE public.tx_barcode_stock >> ADD CONSTRAINT "tx_barcode_stock_CK1" CHECK >> ( (branchcode = '1'::bpchar  and barcodeitem = 'Y'::bpchar and >> closingstock >= 0::numeric)  Or (branchcode = '1' and barcodeitem = >> 'N'::bpchar and closingstock >= 0::numeric )  Or (branchcode = >> '2'::bpchar  and barcodeitem = 'Y'::bpchar and closingstock >= >> 0::numeric)  Or (branchcode = '2' and  barcodeitem = 'N'::bpchar and >> closingstock >= 0::numeric ) ) NOT VALID; >> >> After creation, when we check what we find is  [ in PgAdmin ] >> ALTER TABLE public.tx_barcode_stock >>     ADD CONSTRAINT "tx_barcode_stock_CK1" CHECK (branchcode::bpchar = >> '1'::bpchar AND barcodeitem = 'Y'::bpchar AND closingstock >= >> 0::numeric OR branchcode::text = '1'::text AND barcodeitem = >> 'N'::bpchar AND closingstock >= 0::numeric OR branchcode::bpchar = >> '2'::bpchar AND barcodeitem = 'Y'::bpchar AND closingstock >= >> 0::numeric OR branchcode::text = '2'::text AND barcodeitem = >> 'N'::bpchar AND closingstock >= 0::numeric) >>     NOT VALID; >> >> We have only one bracket, in the final updated one. >> >> Since there are AND and OR conditions, without brackets the whole >> conditions becomes useless. > did you see > https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-PRECEDENCE > ? > > > mvg, > Wim You can use plpgsql function alter table tale_name add constraint xyz  check (func(table_name))