views:

856

answers:

3

According to the definition, a Junction Table (bridge table/link table) is used for many-to-many relationships, when used like this:

CREATE TABLE Users
(
UserLogin varchar(50) PRIMARY KEY,
UserPassword varchar(50) NOT NULL,
UserName varchar(50) NOT NULL
)


CREATE TABLE Permissions
(
PermissionKey varchar(50) PRIMARY KEY,
PermissionDescription varchar(500) NOT NULL
)


--This is the junction table.
CREATE TABLE UserPermissions
(
UserLogin varchar(50) REFERENCES Users (UserLogin),
PermissionKey varchar(50) REFERENCES Permissions (PermissionKey),
PRIMARY KEY (UserLogin, PermissionKey)
)

But couldn't it also be used just as easily for a one-to-many relationships, as in this example in which one user is associated with many orders:

(I don't understand databases well so please correct me if I have misunderstood something.)

CREATE TABLE Users
(
UserLogin varchar(50) PRIMARY KEY,
UserPassword varchar(50) NOT NULL,
UserName varchar(50) NOT NULL
)


CREATE TABLE Orders
(
OrderKey varchar(50) PRIMARY KEY,
OrderDescription varchar(500) NOT NULL
)


--This is the junction table.
CREATE TABLE UserOrders
(
UserLogin varchar(50) REFERENCES Users (UserLogin),
OrderKey varchar(50) REFERENCES Orders (OrderKey),
PRIMARY KEY (UserLogin, OrderKey)
)
+4  A: 

There is not any reason why a junction table couldn't be used for a one-to-many relationship. The question is usually one of performance. Why make the database join an additional table when it is unnecessary?

j0tt
+3  A: 

Yes, but then you leave the check that it's not many to many to the application, instead of enforcing it inside the database.

Vitaly Polonetsky
Are you saying this is bad?
pez_dispenser
Sometimes it can be handy if you want to have the same db tables structure but decide the type of the relation in application: one to many or many to many. (as a parameter)There are times when you develop one to many relation knowing that it's very likely to change to many to many.
Vitaly Polonetsky
I see. And if you wanted to enforce one-to-many or many-to-many in the database, you instead use some type of constraint?
pez_dispenser
@jt if you're talking about joint table then you can't enforce one-to-many relation by using only the table. Otherwise you mean enforcing it by "foreign key" contraint. In one to many relation the "many" side has a field with the constraint that means that each row has to point to the "one" side (has one side's id in the foreign-key column and it won't allow another values there).
Vitaly Polonetsky
+1  A: 

Once you've built a table, it really doesn't have a type of "Junction" table, "associative" table, "join" table -- it's just a table.

We use these terms to describe a specific reason why an entity (and resulting table) was initially created. Associative entities are created, initially, to resolve a many-to-many situation. But these tables quite often have attributes of their own (such as the time of the association, a reason for the association, etc.). So SQL Server, Oracle or your code has no reason to know why a table was created...just that it's a table.

From a technical point of view, there really isn't any difference between an associative table and any other table.

So these tables can fill any role that any other table can fulfill. There are no rules around how other tables can also be related to them.

Karen Lopez
Thanks .
pez_dispenser