Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

681
Views
Postgresql: crea una tabla con subclases separadas

No estoy seguro de cómo se puede crear una tabla en postgresql con subclases separadas. He representado (una versión muy simplificada de) mi problema a continuación en un diagrama ER, que muestra las dos subclases y los atributos de cada subclase.

ingrese la descripción de la imagen aquí

Para las columnas comunes a todas las filas (id, common1, common2), es claramente muy simple (como se muestra en el código a continuación).

 create table Music ( id serial, common1 int not null, common2 boolean not null, --<what to put here???> );

Sin embargo, no estoy seguro de cuál es la mejor manera de considerar el problema de la subclase. ¿Alguien sabe a dónde ir desde aquí?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La herencia de la tabla de Postgres funcionaría así:

 create table music ( id serial primary key, common1 int not null, common2 boolean not null ); create table symphony ( layers int not null, at1 text not null ) inherits(music); create table concerto ( lead text not null, strings integer not null ) inherits(music);

Considere si tenemos una fila en cada tabla.

 insert into concerto (common1, common2, lead, strings) values (1, true, 'a', 5); insert into symphony (common1, common2, layers, at1) values (2, false, 3, 'b'); insert into music (common1, common2) values (3, true);

Son todas filas de música.

 -- Fetches id, common1, and common2 from all rows. select * from music

Si solo desea consultar filas en música, especifique only music .

 -- Fetches id, common1, and common2 from only the one row in music. select * from only music

Si desea utilizar columnas sinfónicas, debe consultar symphony.

 -- Fetches id, common1, common2, layers, at1 only from symphony select * from symphony

Intentalo


Una estructura más tradicional usaría unir tablas así:

 create table music ( id serial primary key, common1 int not null, common2 boolean not null ); create table music_symphony ( music_id integer references music(id), layers int not null, at1 text not null ); create table music_concerto ( music_id integer references music(id), lead text not null, strings integer not null ); insert into music (id, common1, common2) values (1, 1, true); insert into music_concerto(lead, strings) values ('a', 5); insert into music (id, common1, common2) values (2, 2, false); insert into music_symphony (music_id, layers, at1) values (2, 3, 'b'); insert into music (id, common1, common2) values (3, 3, true); -- Fetch all symphonies select * from music m join music_symphony ms on ms.music_id = m.id

Intentalo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!