I have to make a M:1 relationship using SQL Server but I’ve been wondering if the column representing the “many” side should be foreign key and the column representing “one” side should be primary key.
The purpose is retrieving information from the table with the column representing “one” side using the table with the column representing “many” side.
Do you thing this idea is reasonable and would work successfully?
I have to make a M:1 relationship using SQL Server but I’ve been wondering
Share
The column representing the many side is indeed a primary key of a table, and the column representing the one side is also a primary key of another table. But there is a new column in one of them, is a foreign key that connect each others(this column is the column that makes the relation many to one, and that is the column you should care of).
So, you have to make them into two tables. Consider the following Many to one example:
This would be represented with two tables:
Countries:CountryIDprimary key,Name.Cities:CityIDprimary key1,CountryIdforeign key referencesCountries(ID),Name.And that is how you should create a many to one relation as follows:
CountryIDandCityIDare primary keys in the two tables.CountryIDwhich represents the one side is a foreign key in the many side(citiestable). That is the column that you should1:You should have a composite key instead of a primary table in the
Citiesunless you want a city to be in more than one Country at the same time.