Tomtom
Tomtom

Reputation: 9394

Foreignkey-Relation for two tables

I have the following two tables.

[Person]
Id [INTEGER] PRIMARY KEY
Firstname [VARCHAR]
Lastname [VARCHAR]
Birthday [DATETIME2]

[Address]
Id [INTEGER] PRIMARY KEY
Street [VARCHAR]
ZipCode [INTEGER]
City [VARCHAR]
Country [VARCHAR]

I'm not sure where to put the foreign-key.

Is it better to put a foreign key into the Person-Table or into the Address-Table? My first idea would be to put it into the Person-Table, because this is my "main"-table

Upvotes: 0

Views: 78

Answers (2)

gaepi
gaepi

Reputation: 404

It depends. A person has only one address or more than one ?

  • If only one, I think it's more logic to put IdAddress in the Person table because one person has one address, and not, one address has one person

  • If more than one, maybe you should create a join table with IdPerson and IdAddress.

Upvotes: 4

Kermit
Kermit

Reputation: 34055

Add the following:

[Address]
PersonId [INTEGER] (References [Person].[Id])

Upvotes: 0

Related Questions