Home > Software engineering >  Explanation on how to express one to many relationships
Explanation on how to express one to many relationships

Time:03-01

Imagine I have a user who can own many pieces of art (one-to-many).

Is there a table in between that holds this information? Or does the art entity have an attribute that can be called "owner" which is a foreign key for the primary key in user?

CodePudding user response:

If one piece of art can be owned by only one person, then you don't need any other tables.

SQL> create table owner
  2    (id_owner    number       constraint pk_own primary key,
  3     name        varchar2(20) not null
  4    );

Table created.

SQL> create table piece_of_art
  2    (id_art      number       constraint pk_art primary key,
  3     name        varchar2(20) not null,
  4     id_owner    number       constraint fk_art_own
  5                                references owner (id_owner)
  6                              not null
  7    );

Table created.

SQL>
  • Related