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 1nrkpc-0000sh-9n for pgsql-novice@arkaria.postgresql.org; Thu, 19 May 2022 18:25:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nrkpa-0007Sm-1r for pgsql-novice@arkaria.postgresql.org; Thu, 19 May 2022 18:25:02 +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 1nrkpZ-0007SO-Nm for pgsql-novice@lists.postgresql.org; Thu, 19 May 2022 18:25:01 +0000 Received: from mail-oi1-x230.google.com ([2607:f8b0:4864:20::230]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nrkpS-0004JM-P6 for pgsql-novice@lists.postgresql.org; Thu, 19 May 2022 18:25:00 +0000 Received: by mail-oi1-x230.google.com with SMTP id m25so7466872oih.2 for ; Thu, 19 May 2022 11:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Zpuq4lrbR+0cYwYEogcXjJel8hnvN11ELdZw7kUhe88=; b=nNOfoKUG3Up+/qwDTOCaF9YFWFZqgYxlQo4dROm53f3ni58r/ZK65Oy3h2nrhx1cXV cKcETZwDkKeQUvVbnd9V315PD2v10e3UXm9QKco6zZjXw0Z/Ix0Jqn5ezJTaTQhnCdTE pA31pU5EsNdUV0jcG566ecm05W4IW+8myuPjFoY5pMDCqIjOo6OtVepSXtLvGmrh2b/2 ON07Gx6yPRK8kvxyKZ0C8EAXrw5PS2fZTdpR8yKObSYM9YUsNJH2mAduVycD38LexksQ YdJYeskyxENIROG22S93ELDnlgMvGb5uAK99pCbT8fXF1zcMBWf3ud3QxLhtrXOkPBUK rmng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Zpuq4lrbR+0cYwYEogcXjJel8hnvN11ELdZw7kUhe88=; b=STw5A9ZKtpejQh/9ijQTlYIHBOAB8ae2tCw5oBB5pFFwpX16Z79osTrm2TlbIDvX/g eq40zhPMyZywyuDny7/3YbJxM2lzFswu+h9ovV8rf3IsEhrF27Umf4bUw20kZoTStDuy 88sJkZNTMk6VUdOutkRwO6g1daqBjPxCuVmRLnCjS/Ht4hkc0TTrXW2/DcRb34g2FXyC OeDw1geOnU3nUrycCThPMCTUQ4OlCcSvGe7Q6LJvzW2Nb1ZvrN78mKWoW5b+YSjr2wau ht4EWjDAiQ8ztwohkmgHRSX9BVrAFI/KfC1c82knRza/24OT7f7JPtUCyv+xRbLdps0j og7w== X-Gm-Message-State: AOAM532NT0Sy/Zcj6wJ5xKq5mresKM1ASWvkNxQq9c5EU8Lg5APwZhq4 pi8n4i3PC79+gItAT6abwxcyPMlX+ajmdHmej14UcHjsGys= X-Google-Smtp-Source: ABdhPJxMDJxeSaVf0OW2omDdszb8SyKSt0ALn3CCeL9RBUG5781uGrDpjsUPcgWcOzCeM2gMzNREQ04qbGUpWkXpVUA= X-Received: by 2002:a05:6808:170b:b0:2fa:729a:a42e with SMTP id bc11-20020a056808170b00b002fa729aa42emr3593199oib.0.1652984692491; Thu, 19 May 2022 11:24:52 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Chris Tsongas Date: Thu, 19 May 2022 11:24:41 -0700 Message-ID: Subject: Re: Writing my first trigger To: pgsql-novice@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Upon looking at my own code, I realized there's no reason for me to be looking at the OLD values, I only care about the NEW: CREATE OR REPLACE FUNCTION update_employee() RETURNS TRIGGER AS $$ BEGIN IF (NEW."preferredFirstName" IS NOT NULL) THEN NEW."fullName" = NEW."preferredFirstName" || ' ' || NEW."lastName"; ELSE NEW."fullName" = NEW."firstName" || ' ' || NEW."lastName"; END IF; NEW."updatedAt" = now(); RETURN NEW; END; $$ LANGUAGE plpgsql; Any other feedback appreciated! On Thu, May 19, 2022 at 11:18 AM Chris Tsongas wrote: > > Working on my first trigger to create a fullName value from firstName, > optional preferredFirstName, and lastName fields, where the full name > uses the optional preferred first name if it exists, otherwise it uses > the first name and of course the required last name. > > Would be great to get feedback on the following code before I try > running it (note I already have an employee table, just including the > CREATE TABLE statement for clarity): > > CREATE TABLE employee ( > firstName text NOT NULL, > preferredFirstName text, > lastName text NOT NULL, > fullName text, > ); > > CREATE OR REPLACE FUNCTION update_employee() RETURNS TRIGGER AS $$ > BEGIN > IF (OLD."preferredFirstName" IS NOT NULL) THEN > NEW."fullName" = OLD."preferredFirstName" || ' ' || OLD."lastName"; > ELSE > NEW."fullName" = OLD."firstName" || ' ' || OLD."lastName"; > END IF; > NEW."updatedAt" = now(); > RETURN NEW; > END; > $$ LANGUAGE plpgsql; > > CREATE TRIGGER fullName > INSTEAD OF INSERT OR UPDATE ON employee > FOR EACH ROW EXECUTE FUNCTION update_employee();