Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

395
Visualizações
PostgreSQL Create Sequence using new id

I want to create a sequence for each row created in the table account, like os_1, os_2, etc...

How can I get the id of this new row and insert it on the name of the sequence?

CREATE OR REPLACE FUNCTION public.create_os_seq() RETURNS TRIGGER AS $$
    #variable_conflict use_variable
    BEGIN
        --CREATE SEQUENCE seqname;
        EXECUTE format('CREATE SEQUENCE os_', NEW.id);
        return NEW;
    END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER create_os_seq AFTER INSERT ON account FOR EACH ROW EXECUTE PROCEDURE create_os_seq();

Table account

id INT AUTO_INCREMENT
nane VARCHAR

After creating a sequence i´ll put its number in the OS table

table os

id INT
account_id INT
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

From your question I assume you want to take care of self incrementing values?.. Postgres uses shortcut SERIAL instead of AUTO_INCREMENT, just create table like:

CREATE TABLE so79 (id bigserial primary key, col text); 

That will automatically create sequence for you and assign its value as default for column id. Basically will make it smth like AUTO_INCREMENT. You don't have to use trigger to increment values...

over 4 years ago · Santiago Trujillo Relatório

0

There is no way that creating a sequence for each row is a good idea. Instead, tell us what you're trying to do.

My guess is you need a waterline indicator, a high point you intend to increment on some action. Just use an integer.

CREATE TABLE foo (
  foo_id     serial,
  max_seen   int
);

Now you can do whatever with triggers on other tables and such to increment max_seen.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda