The issue is the following:
I have a table (let's name it table A
) and another table (table B
). What I need is to share the sequence of the table B
and use it in A
, so that I have 2 different tables, but every time I insert a row into one of the tables, it would behave like the 2 tables were one table, like this:
table A: table B:
id| id|
4| 1|
5| 2|
6| 3|
8| 7|
I could not find out how to do this, so instead I created an on-update trigger which updates the id
of the row I inserted in the table A
and replaces with the next value of the sequence of the table B
, the code is the following:
CREATE OR REPLACE FUNCTION credo.atz_id_reserva()
RETURNS trigger AS
$BODY$
BEGIN
IF (TG_OP = 'INSERT') THEN
UPDATE credo.reserva_pto_cta
SET id = (select nextval('credo.documento_id_seq'));
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION credo.atz_id_reserva()
OWNER TO postgres;
Did I do something wrong, is anything missing?
But that is so simple:
CREATE SEQUENCE shared_seq;
CREATE TABLE a (
id bigint PRIMARY KEY DEFAULT nextval('shared_seq'),
...
);
CREATE TABLE b (
id bigint PRIMARY KEY DEFAULT nextval('shared_seq'),
...
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.