Home > database >  Which relationship should be among tables?
Which relationship should be among tables?

Time:09-27

I have a table Expenses and relative table ExpensesCaterory.

The Expenses table can contains a lot of rows of expenses with diferent catergory.

Does it mean that relationship between table are one(expenses) to many(categories)?

Is it correct on the schema?

enter image description here

CodePudding user response:

It is one-to-many relationship but should be the other way around, one category to many expenses. Think of it as one ExpenseCaterory can have many Expense while one Expense can only be in one ExpenseCaterory, as the image in this wiki:

ExpensesCaterory

1 | Taxi Expense
2 | Electricity Expense
3 | Food Expense
4 | Rent Expense

Expenses

1 | 2000 | Taxi Expense
2 | 360 | Taxi Expense
3 | 8500 | Food Expense
4 | 435 | Taxi Expense
3 | 12000| Electricity Expense

Here, you can see that "one" category Taxi Expense is mapped/related to "many" expenses namely 2000, 360, and 435.

The idea is basically that a record from the "one" side (here is ExpensesCaterory) is related to one or more records in the "many" side (here is Expenses).

Related reference:

  •  Tags:  
  • sql
  • Related