Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between one-to-one and one-to-many relationship in database

This is probably a basic(dumb) question but when having a one-to-one relationship in a database the other table has a foreign key ID(in this example). And in a one-to-many relationship the table contains many foreign keys.

But the database does not know whether this is a one-to-one or one-to-many relationship right? The relationships that I make in an ER-Diagram is only to indicate where it should be foreign keys when making the actual tables?

I do not completely grasp the idea of the relationships, even though I have read many tutorials about this.

Thanks in advance.

like image 780
LuckyLuke Avatar asked Jan 20 '11 19:01

LuckyLuke


People also ask

What is the difference between one-to-many and many-to-many relationships in a database?

One-to-many: A record in one table is related to many records in another table. Many-to-many: Multiple records in one table are related to multiple records in another table.

What is a one-to-one relationship in database?

A one-to-one relationship is a link between the information in two tables, where each record in each table only appears once.

What are the 3 types of relationships in a database?

There are 3 different types of relations in the database: one-to-one. one-to-many, and. many-to-many.

What is one-to-many relationship in database with example?

In a one-to-many relationship, one record in a table can be associated with one or more records in another table. For example, each customer can have many sales orders. In this example the primary key field in the Customers table, Customer ID, is designed to contain unique values.


3 Answers

In a sense, all the relationships we talk about are not known to the database, they are constructs we have invented to better understand how to design the tables.

The big difference in terms of table structure between one-to-one and one-to-many is that in one-to-one it is possible (but not necessary) to have a bidirectional relationship, meaning table A can have a foreign key into table B, and table B can have a foreign key into the associated record in table A. This is not possible with a one-to-many relationship.

One-to-one relationships associate one record in one table with a single record in the other table. One-to-many relationships associate one record in one table with many records in the other table.

like image 191
Zack Bloom Avatar answered Oct 23 '22 20:10

Zack Bloom


To enable one-to-one relationship you need to add unique constraint to foreign key. It is not possible to have two foreign keys for each table as it will be impossible to create records.

like image 23
Danil Avatar answered Oct 23 '22 22:10

Danil


Im having trouble understanding what the actual question is.

Your analysis is for the most part correct, in that if you have a 2 tables, and table2 has a foreign key to table one, it could be either a one-to-one or a many-to-one.

Your sentence "And in a one-to-many relationship the table contains many foreign keys."

The table of the 'many' side still contains one column that is a foreign key, its just that more than one row can have the same foreign key value (many rows point to one parent).

Also note that you can put the foreign key on the parent table, to the child, instead of the other way around. In this way, you can prevent one-to-many if you want to do that. Also note that in this way, more than one parent can share a child, which might or might not be what you want.

like image 35
hvgotcodes Avatar answered Oct 23 '22 21:10

hvgotcodes