how to prefix a string before sequence generated by postgresql?

前端 未结 1 1087
轻奢々
轻奢々 2020-12-19 06:11

This is a sequence

CREATE SEQUENCE technician_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

It generates

1

相关标签:
1条回答
  • 2020-12-19 06:23

    Here are a couple ways:

    -- Referencing the sequence directly:
    CREATE SEQUENCE test_seq;
    
    SELECT 'AAAA'||nextval('test_seq')::TEXT;
     ?column? 
    ----------
     AAAA1
    
    SELECT 'AAAA'||nextval('test_seq')::TEXT;
     ?column? 
    ----------
     AAAA2
    
    
    -- Using a DEFAULT
    CREATE TABLE abc 
        (val TEXT NOT NULL DEFAULT 'AAAA'||nextval('test_seq'::regclass)::TEXT, 
        foo TEXT);
    
    INSERT INTO abc (foo) VALUES ('qewr');
    
    SELECT * FROM abc;
      val  | foo  
    -------+------
     AAAA3 | qewr
    

    These assume that you have carefully decided how to proceed, based on the comments to your original question, as asked by the others.

    0 讨论(0)
提交回复
热议问题