Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks, A quick query on database design here....

I'm building a personal site which lists farms in Australia with details about the farm.

I'd like to set it up so that I can not only identify a primary crop associated with that farm, but that a list of secondary crops can be identified also.

Now I've read a fair bit so I have the basics of design down (sort of). I know about primary and foreign keys and how I would go about setting up and associating the tables for primary crop and the farm. I just can't seem to get my head round how I would set up the other.

I would be using Guids to identify each crop.

Do I just assign the row to the nvarchar(max)then add a comma-delimited string with a list of the crop Guids?

Any input would be most welcome.

Cheers
Posted

1 solution

You will need two tables to do this.

First table:
FarmID, PrimaryCropGuid, other fields.
FarmID is the primary key.

Second table:
FarmID, SecondargCropGuid.
FarmID is the foreign key pointing to the FarmID in first table.

Now if you want all secondary crops of a given farm then you can do:
SQL
SELECT SecondargCropGuid from SecondTable WHERE FarmID='GivenID';


You can also find all farms which grow secondary crops with:
SQL
SELECT FarmID from SecondTable WHERE SecondaryCropGuid='GivenGuiD';


-Saurabh
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900