Re: Function PostgreSQL 9.2 - Mailing list pgsql-general

From David G. Johnston
Subject Re: Function PostgreSQL 9.2
Date
Msg-id CAKFQuwZYvQ3DhSau3R3KpetapEQhefKvH=Dnkr_NJT9mSM=Rvg@mail.gmail.com
Whole thread Raw
In response to Re: Function PostgreSQL 9.2  ("drum.lucas@gmail.com" <drum.lucas@gmail.com>)
List pgsql-general
On Mon, May 2, 2016 at 5:44 PM, drum.lucas@gmail.com <drum.lucas@gmail.com> wrote:
This is what I've done:


-- 1 - Creating the Sequence:

    CREATE SEQUENCE users_code_seq  
    INCREMENT 1  
    MINVALUE 1
    MAXVALUE 9223372036854775807  
    START 1000;
    CACHE 1;

-- 2 - Setting the DEFAULT

    ALTER TABLE public.users ALTER COLUMN code SET DEFAULT NEXTVAL('users_code_seq');

-- 3 - Setting the column as NOT NULL;

    ALTER TABLE public.users ALTER COLUMN code SET NOT NULL;

-- 4 - Setting the trigger

            CREATE TRIGGER public.update_code_column
              BEFORE UPDATE OR INSERT
              ON public.users
              FOR EACH ROW
              EXECUTE PROCEDURE public.users_code_seq;

-- 5 - Creating a CONSTRAINT UNIQUE
        ALTER TABLE public.users
          ADD CONSTRAINT uc_users_code UNIQUE("code");


Is that right?
Am I missing something?


​The definition for "public.users_code_seq" - which is an odd name to choose, especially given you already have a sequence of the same name.

David J.
 

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: index question
Next
From: Sameer Kumar
Date:
Subject: Re: index question