Problem with the bridge table in SQLite database

0

I am learning SQL, using SQLite. I'm seeing how many-to-many relationships work between tables that use primary key and foreign key.

I have three tables:

contacts , which has the following columns: contact_id (primary key), first_name, last_name, email and phone.

groups , which has the following columns: group_id (primary_key) and name.

contact_group , (my relational table) with columns: contact_id (primary key and associated with foreign key to contact_id of contacts ) and group_id (primary key and associated with foreign key to group_id of groups ).

I can enter data in contacts tables in this way:

insert into contacts (first_name, last_name, email, phone)
values
('jon','sanchez', '[email protected]','4433');

And in groups like this:

insert into groups (name)
values
('clase')

But how do I fill in the relational table contact_group ? How do I tell you which contact is in each group? How do I say that Jon belongs to the Class group?

    
asked by MAP 18.10.2018 в 08:44
source

1 answer

0

In the end I solved the problem by manually filling in the relational table, giving the ids of contact_id and group_id in the following way:

insert into contact_groups (contact_id, groups_id)
values
(3,2)

In this way, I relate that the contact with id = 3 will be in the group with id = 2.

Afterwards to see that everything is correct and they have related well I have made a display of the information such that:

select *
from groups natural join contact_groups natural join contacts

To make it clearer I leave here the tables created and the result of the join.

Table groups :

Table contacts :

The relational table of contact_groups after manually entering the relationships:

And finally the result of the join, where it can be seen that the relational table has made the connections well:

    
answered by 18.10.2018 / 11:28
source