The best way to talk about database relationships is by exemplifying their use, certain information often depends on another for it to exist, and with that comes the need to create a way to link them. Database Relationships are just that, a way to link the data from one table to another from certain information.
To make it easier to understand we will use a practical example, imagine categorizing all products in a database. Each product must have a category. Then consider the following scenario:
Table: tbl_cat -> Fields: cod_cat, name
Table: tbl_produto -> Fields: cod_prod, cod_cat, Name, Price
Note that the field cod_cat that stores the category code is used to indicate, in the table product, which product belongs to which category. It features a one-to-many relationship where one category can be used for several products. If it were not possible to create relationships all table information categories would have to be included in the product table and it probably would bring future problems if were necessary to add more information related to the category.
As quoted above, the type of relationship used was the one to many, but there are still two other types of relationships with them one to one where there is a direct relationship between two primary keys and many many relationships where it is possible to use multiple records and the same relationship is no possibility of repeating the table.
Understanding how relationships are essential to model and optimize your database, so it is important to get all requirements and fully understand the role of each piece of information.
I hope you enjoyed these tips. Follow our blog and stay on top of all the news.
You might also like…