sql - One column that is sequenced and shared across multiple tables? -
i new sql , have been researching this. think answer lies in relationships use direction on needs done.
i'll start hypothetical example. have 3 tables - trucks, cars, motorcycles. each table has own primary key (which have working fine), own columns of unique vehicle.
what i'm looking add column tables, ordernumber. ordernumber auto-increment primary key does. however, want increment if of 3 tables has new entry inserted. if first add 2 entries car table, ordernumber on first truck entry 3.
i'm sorry if trivial (or bad idea), use direction on this. if makes difference working in postgresql.
it possible. here sample
create sequence vehicle_id_seq; create table motorcycles ( id serial, .... order_id int4 default nextval('vehicle_id_seq') not null ); create table cars ( id serial, ..... order_id int4 default nextval('vehicle_id_seq') not null ); depending on schema, better have separate table orders. feels kinda work-aroundish, , better have simple solution.
the order table be:
create table orders ( order_id serial, (meta data order) ); then each of tables have foreign key constraint reference order.
Comments
Post a Comment