Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a One to One relationship should i drop one of the table's id column?

I have the following 2 tables in MySQL: Customer(Id, Firstname, Lastname...) Bonus(Id, CustomerId, Value, ...)

The relation is One-To-One, every customer has only one bonus.(the CustomerId is unique in the Bonus Table)

Q: Should I drop the Id column of the Bonus table? (I want to know why or why not)

like image 616
Vlad Avatar asked Jun 16 '10 09:06

Vlad


People also ask

Where does the foreign key go in one-to-many relationship?

If there is a one-to-many relationship between two entities, add the key of the entity on the “one” side (the parent) into the child table as a foreign key.

What is the point of one-to-one relationship?

One-to-one relationships are frequently used to indicate critical relationships so you can get the data you need to run your business. A one-to-one relationship is a link between the information in two tables, where each record in each table only appears once.

How do you solve a one-to-many relationship?

To implement a one-to-many relationship in the Teachers and Courses table, break the tables into two and link them using a foreign key. We have developed a relationship between the Teachers and the Courses table using a foreign key.

How do you store a one-to-many relationship in a database?

To define a one-to-many relationship between two tables, the child table has to reference a row on the parent table. The steps required to define it are: Add a column to the child table that will store the value of the primary identifier.


1 Answers

I would remove the Bonus.Id coulmn and make Bonus.CustomerId the PK. Doing this will remove the need to have a unique constraint on the Bonus.CustomerId column, since it will now be a PK. Anyone looking at the table will see the one-to-one more clearly without the Bonus.Id coulmn. You won't need an index on Bonus.CustomerId, the PK index will be all you need, so less disk space and memory cache wasted. Also, if you ever need a FK to the Bonus table, you you would use the CustomerId value (the new PK), which can be used to get back to Customer or Bonus tables, not just Bonus.

like image 138
KM. Avatar answered Sep 20 '22 02:09

KM.