Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a junction table (join table) also be used for a one-to-many relationship?

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)
)
like image 863
pez_dispenser Avatar asked May 09 '09 18:05

pez_dispenser


People also ask

How do you join tables with many-to-many relationships?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.

What type of relationship requires the use of a junction table?

The correct answer is C. The many-to-many relationship is the only relationship type that uses a junction table.

How do you join one-to-many tables?

The table on the "one" side of the "one-to-many" relationship should have a primary key column. The other table should have a foreign-key defined pointing to the primary key on the first table. To return results from both tables you'd add an INNER JOIN clause to join both tables.

What is a one to one relationship between tables a many to one relationship?

A one-to-many relationship is the most common kind of relationship. In this kind of relationship, a row in table A can have many matching rows in table B. But a row in table B can have only one matching row in table A. For example, the "Publishers" and "Titles" tables have a one-to-many relationship.


1 Answers

Yes, it is still possible to store and enforce one-to-many relationship in a junction table.

In your example you are not enforcing any constraints on the UserOrders junction table, so a single order can belong to two users (assuming that's incorrect). To enforce that you could make OrderKey be the primary key of the UserOrders junction table (or have a unique constraint on that column). Technically that will just become a many-to-one relationship between UserOrders and Users, while having one-to-one relationship between Orders and UserOrders.

I can only think about one reason for designing the many-to-one relationship using junction table - if you plan to allow the many-to-many relationship in future and don't want to deal with data migration. But in the mean time you will pay the cost of storing and joining with additional table.

like image 59
m_vitaly Avatar answered Oct 19 '22 05:10

m_vitaly