0

Given two tables like so

CREATE TABLE participants(
    id SERIAL PRIMARY KEY,
    Name TEXT NOT NULL,
    Title TEXT NOT NULL
);

CREATE TABLE meetings (
    id SERIAL PRIMARY KEY,
    Organizer TEXT NOT NULL,
    StartTime DATE NOT NULL,
    EndTime DATE NOT NULL,
    Participants INT[],
);

I want Participants column of 'meetings' table to contain set of integers which are all primary keys (specific participant) from 'participants' table.

How do I define this field in 'meetings' table ?

4
  • 1
    Why can't you create a meetings_participants table? Commented Feb 2, 2022 at 10:55
  • @SalmanA what do you mean ? Im quite new to SQL could you explain further ? Commented Feb 2, 2022 at 10:56
  • Personally, I would avoid using array features of PostgreSQL and create a many-many table that stores (meeting, participant) pairs. Foreign key and unique constraints could easily be added. Commented Feb 2, 2022 at 10:58
  • @SalmanA could you write an example from my tables ? I could probably grasp it if I see it in code Commented Feb 2, 2022 at 10:59

2 Answers 2

3

The old fashioned way is to create a many-many table, with a couple of commonsense constraints:

CREATE TABLE meetings_participants(
    meeting_id int not null,
    participant_id int not null,
    primary key (meeting_id, participant_id),
    foreign key(meeting_id) references meetings(id),
    foreign key(participant_id) references participants(id)
)

Now it is easy to add and remove people to meetings be inserting or deleting rows or query meetings that e.g. have 4 or more participants.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you'! So if i understand correctly, I remove participants column from my meetings table. I have just meetings table and participants table, and this unifying table holds meeting and participant pairs per row ?
Exactly. This is a many-to-many/bridge table. In future it'll allow you to store additional information in that table (e.g. which participant is/are the host, which participant actually attended the meeting etc).
1

A more common approach is to create a junction table for the meeting participants.

CREATE TABLE participants (
  participant_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  Name TEXT NOT NULL,
  Title TEXT NOT NULL
);

CREATE TABLE meetings (
  meeting_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  Organizer TEXT NOT NULL,
  StartTime DATE NOT NULL,
  EndTime DATE NOT NULL
);

CREATE TABLE meeting_participants(
  meeting_id INT NOT NULL, 
  participant_id INT NOT NULL, 
  PRIMARY KEY (meeting_id, participant_id), 
  FOREIGN KEY (meeting_id) REFERENCES meetings(meeting_id), 
  FOREIGN KEY (participant_id) REFERENCES participants(participant_id)
);

Which is then used to join the 2 tables.
For example:

SELECT m.*, p.*
FROM meeting_participants mp
JOIN meetings m USING(meeting_id)
JOIN participants p USING(participant_id)
WHERE m.Organizer = 'John Doe';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.