If two entities are engaged in a many-to-many relationship with both entities participating totally, how many tables are needed

database-designerd

I was thinking three tables, one for entity A, second for entity B and third for the relationship and describe the total participation in the third table by importing primary keys from first and second table and terming them as not NULL.
Am I on right track?

Consider a relation between students and courses, each student must be associated with at least one course (can be more than one too) and each course must be taken by at least one student (can be taken by more than one students too). This would result in a many to many relationship with total participation from both sides.

CREATE TABLE student ( sid INTEGER, age INTEGER, PRIMARY KEY (sid) )
CREATE TABLE COURSE ( cid INTEGER, timings DATETIME, PRIMARY KEY (cid) ) 
CREATE TABLE TAKES ( sid INTEGER, cid INTEGER, FOREIGN KEY (sid), 
    FOREIGN KEY (cid), instructor CHAR(12), PRIMARY KEY (sid,cid) )

Best Answer

Using a third table to hold links between two independent tables is the correct method when designing a simple many-to-many relationship. You don't specifically need flag the fields as NOT NULL because they form the Primary Key and so cannot be NULL anyway.

One note, however, you've stated in your example that a Student record MUST be associated with at least one Course record, and vice versa. Your table design would not enforce this requirement, it would only allow the possibility that Student and Course records could be linked - not that they must have at least one linked record.

Your example would not enforce total participation on both sides, and I am not sure that you even could enforce it. It raises the question of which comes first, the Student or Course record given that each of those record types requires at least one link to the other record type.

Without a Student record, no Course could be created because it doesn't have a linked record, and vice versa - it's the classic "which came first" chicken or egg scenario!