I have used column-based relations a lot in my projects like:
CREATE TABLE `user` (
id INT AUTO_INCREMENT PRIMARY KEY,
usergroup INT
);
CREATE TABLE `usergroup` (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
);
however, at work it seems some people do it using table-based relations like this:
CREATE TABLE `user` (
id INT AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE `usergrouprelation` (
userid INT,
usergroupdid INT
);
CREATE TABLE `usergroup` (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
);
and I am wondering here what are the pros and cons of both approach? And what is the official term for this?
The relationships are different.
In your first example, a one to many relationship. One group can have many users. (a user can only be in one group)
In your second example, a many to many relationship. Many groups can have many users. (a user can be in more than one group and groups can have more than one user)
That’s the difference between the two, it’s common practice to use an intermediate table to break up a many to many relationship.